All files / benchmark/src profiling.ts

100% Statements 25/25
80% Branches 8/10
100% Functions 8/8
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152                                                                              1x                                                                       69x                     68x 17x                 57x 57x 51x   51x                   233x 50x     183x               29x 29x 29x   29x 29x 29x 29x   29x   29x                     1x     1x 1x      
import type { ProfilerOnRenderCallback } from "react";
 
export type BenchmarkScenarioId =
  | "initial-render"
  | "search-filter"
  | "table-scroll"
  | "chart-toggle"
  | "gallery-toggle";
 
export interface BenchmarkScenario {
  id: BenchmarkScenarioId;
  label: string;
  description: string;
}
 
export interface ProfilerSample {
  id: string;
  phase: "mount" | "update" | "nested-update";
  actualDuration: number;
  baseDuration: number;
  startTime: number;
  commitTime: number;
}
 
export interface RenderSummary {
  commitCount: number;
  lastActualDuration: number;
  averageActualDuration: number;
  maxActualDuration: number;
  lastBaseDuration: number;
}
 
export interface InteractionMeasure {
  scenario: BenchmarkScenarioId | string;
  label: string;
  duration: number;
  startedAt: number;
}
 
export const BENCHMARK_SCENARIOS: readonly BenchmarkScenario[] = [
  {
    id: "initial-render",
    label: "Initial render",
    description: "Regenerates the portfolio data and records the resulting commit cost."
  },
  {
    id: "search-filter",
    label: "Search/filter",
    description: "Applies the standard enterprise search benchmark."
  },
  {
    id: "table-scroll",
    label: "Large table scroll",
    description: "Scrolls the account grid deep into the data set."
  },
  {
    id: "chart-toggle",
    label: "Heavy chart toggle",
    description: "Toggles the revenue concentration chart."
  },
  {
    id: "gallery-toggle",
    label: "Image gallery toggle",
    description: "Hides or reveals the synthetic gallery images."
  }
];
 
export function toProfilerSample(
  id: string,
  phase: ProfilerSample["phase"],
  actualDuration: number,
  baseDuration: number,
  startTime: number,
  commitTime: number
): ProfilerSample {
  return {
    id,
    phase,
    actualDuration,
    baseDuration,
    startTime,
    commitTime
  };
}
 
export function summarizeProfilerSamples(samples: readonly ProfilerSample[]): RenderSummary {
  if (samples.length === 0) {
    return {
      commitCount: 0,
      lastActualDuration: 0,
      averageActualDuration: 0,
      maxActualDuration: 0,
      lastBaseDuration: 0
    };
  }
 
  const total = samples.reduce((sum, sample) => sum + sample.actualDuration, 0);
  const max = samples.reduce((largest, sample) => Math.max(largest, sample.actualDuration), 0);
  const last = samples[samples.length - 1];
 
  return {
    commitCount: samples.length,
    lastActualDuration: last.actualDuration,
    averageActualDuration: total / samples.length,
    maxActualDuration: max,
    lastBaseDuration: last.baseDuration
  };
}
 
export function formatDuration(duration: number): string {
  if (!Number.isFinite(duration) || duration <= 0) {
    return "0.0 ms";
  }
 
  return `${duration.toFixed(1)} ms`;
}
 
export function measureInteraction<T>(
  scenario: BenchmarkScenarioId | string,
  label: string,
  action: () => T
): { result: T; measure: InteractionMeasure } {
  const marker = `${scenario}-${Math.round(performance.now() * 1000)}`;
  const start = `${marker}-start`;
  const end = `${marker}-end`;
 
  performance.mark(start);
  const result = action();
  performance.mark(end);
  performance.measure(label, start, end);
 
  const entry = performance.getEntriesByName(label).at(-1);
 
  return {
    result,
    measure: {
      scenario,
      label,
      duration: entry?.duration ?? 0,
      startedAt: entry?.startTime ?? performance.now()
    }
  };
}
 
export const createProfilerCallback = (
  publish: (sample: ProfilerSample) => void
): ProfilerOnRenderCallback => {
  return (id, phase, actualDuration, baseDuration, startTime, commitTime) => {
    publish(toProfilerSample(id, phase, actualDuration, baseDuration, startTime, commitTime));
  };
};