Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stop button #20

Merged
merged 1 commit into from
Dec 23, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 30 additions & 12 deletions src/benchmark.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { JSONSchema7 } from 'json-schema';
import type { Signal } from '@lumino/signaling';
import type { Signal, ISignal } from '@lumino/signaling';
import { Statistic } from './statistics';
import { reportTagCounts } from './utils';
import { layoutReady } from './dramaturg';
Expand All @@ -24,6 +24,8 @@ export interface IScenario {

export interface IProgress {
percentage: number;
interrupted?: boolean;
errored?: boolean;
}

export interface IBenchmark<T extends IOutcomeBase = IOutcomeBase> {
Expand All @@ -43,7 +45,8 @@ export interface IBenchmark<T extends IOutcomeBase = IOutcomeBase> {
run: (
scenario: IScenario,
options: any,
progress?: Signal<any, IProgress>
progress?: Signal<any, IProgress>,
stopSignal?: ISignal<any, void>
) => Promise<T>;
/**
* Configuration schema for rendering by rjsf.
Expand Down Expand Up @@ -95,6 +98,7 @@ export interface IOutcomeBase<T extends IMeasurement = IMeasurement> {
tags: Record<string, number>;
totalTime: number;
type: string;
interrupted: boolean;
}

export interface ITimingOutcome<T extends ITimeMeasurement = ITimeMeasurement>
Expand All @@ -115,7 +119,7 @@ export async function profile(
scenario: IScenario,
options: ProfilerInitOptions,
mode: 'micro' | 'macro',
afterMicroStep: (step: number) => void,
afterMicroStep: (step: number) => boolean,
n = 3,
inSuite = false
): Promise<IProfileMeasurement> {
Expand Down Expand Up @@ -147,7 +151,10 @@ export async function profile(
if (scenario.cleanup) {
await scenario.cleanup();
}
afterMicroStep(i);
const shouldContinue = afterMicroStep(i);
if (!shouldContinue) {
break;
}
}
} else {
profiler = new window.Profiler(options);
Expand Down Expand Up @@ -194,7 +201,7 @@ export async function benchmark(
scenario: IScenario,
n = 3,
inSuite = false,
afterStep?: (step: number) => void
afterStep?: (step: number) => boolean
): Promise<ITimeMeasurement> {
if (!inSuite && scenario.setupSuite) {
await scenario.setupSuite();
Expand All @@ -218,7 +225,10 @@ export async function benchmark(
await scenario.cleanup();
}
if (afterStep) {
afterStep(i);
const shouldContinue = afterStep(i);
if (!shouldContinue) {
break;
}
}
}
if (!inSuite && scenario.cleanupSuite) {
Expand All @@ -237,28 +247,36 @@ export const executionTimeBenchmark: IBenchmark<ITimingOutcome> = {
run: async (
scenario: IScenario,
options: ExecutionTimeBenchmarkOptions,
progress
progress,
stopSignal
): Promise<ITimingOutcome> => {
let stop = false;
const stopListener = () => {
stop = true;
};
stopSignal?.connect(stopListener);
const n = options.repeats || 3;
const start = Date.now();
if (scenario.setupSuite) {
await scenario.setupSuite();
}
await layoutReady();
const reference = await benchmark(scenario, n, true, i =>
progress?.emit({ percentage: (100 * (i + 1)) / n })
);
const reference = await benchmark(scenario, n, true, i => {
progress?.emit({ percentage: (100 * (i + 1)) / n });
return !stop;
});
await layoutReady();
if (scenario.cleanupSuite) {
await scenario.cleanupSuite();
}
progress?.emit({ percentage: 100 });
stopSignal?.disconnect(stopListener);
return {
reference: reference.times,
results: [reference],
tags: reportTagCounts(),
totalTime: Date.now() - start,
type: 'time'
type: 'time',
interrupted: stop
};
},
render: renderTimings
Expand Down
18 changes: 14 additions & 4 deletions src/jsBenchmarks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,14 @@ export const selfProfileBenchmark: IBenchmark<
run: async (
scenario: IScenario,
options: ProfileBenchmarkOptions,
progress
progress,
stopSignal
): Promise<IProfilingOutcome> => {
let stop = false;
const stopListener = () => {
stop = true;
};
stopSignal?.connect(stopListener);
const n = options.repeats || 3;
const start = Date.now();
if (scenario.setupSuite) {
Expand All @@ -141,20 +147,24 @@ export const selfProfileBenchmark: IBenchmark<
sampleInterval: options.sampleInterval
},
options.scale,
i => progress?.emit({ percentage: (100 * (i + 1)) / n }),
i => {
progress?.emit({ percentage: (100 * (i + 1)) / n });
return !stop;
},
n,
true
);
await layoutReady();
if (scenario.cleanupSuite) {
await scenario.cleanupSuite();
}
progress?.emit({ percentage: 100 });
stopSignal?.disconnect(stopListener);
return {
results: [result],
tags: reportTagCounts(),
totalTime: Date.now() - start,
type: 'profile'
type: 'profile',
interrupted: stop
};
},
isAvailable: () => typeof window.Profiler !== 'undefined',
Expand Down
69 changes: 56 additions & 13 deletions src/styleBenchmarks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,14 @@ export const styleRuleUsageBenchmark: IBenchmark<ITimingOutcome<IRuleResult>> =
run: async (
scenario: IScenario,
options: StyleRuleUsageOptions = {},
progress
progress,
stopSignal
): Promise<ITimingOutcome<IRuleResult>> => {
let stop = false;
const stopListener = () => {
stop = true;
};
stopSignal?.connect(stopListener);
const n = options.repeats || 3;
const start = Date.now();
const skipPattern = options.skipPattern
Expand Down Expand Up @@ -207,6 +213,9 @@ export const styleRuleUsageBenchmark: IBenchmark<ITimingOutcome<IRuleResult>> =

// Estimate impact of relevant rules on the scenario performance.
for (let i = 0; i < rules.length; i++) {
if (stop) {
break;
}
progress?.emit({ percentage: (100 * (i + 0.5)) / rules.length });
const rule = rules[i];
// Benchmark without the rule.
Expand All @@ -233,14 +242,14 @@ export const styleRuleUsageBenchmark: IBenchmark<ITimingOutcome<IRuleResult>> =
await scenario.cleanupSuite();
}

progress?.emit({ percentage: 100 });

stopSignal?.disconnect(stopListener);
return {
results: results,
reference: reference.times,
tags: reportTagCounts(),
totalTime: Date.now() - start,
type: 'time'
type: 'time',
interrupted: stop
};
},
sortColumn: 'elementsSeen',
Expand Down Expand Up @@ -280,10 +289,16 @@ export const styleSheetsBenchmark: IBenchmark<
run: async (
scenario: IScenario,
options: BenchmarkOptions = {},
progress
progress,
stopSignal
): Promise<ITimingOutcome<IStylesheetResult>> => {
const n = options.repeats || 3;
const start = Date.now();
let stop = false;
const stopListener = () => {
stop = true;
};
stopSignal?.connect(stopListener);
if (scenario.setupSuite) {
await scenario.setupSuite();
}
Expand All @@ -304,6 +319,10 @@ export const styleSheetsBenchmark: IBenchmark<
);
}
for (const style of styles) {
if (stop) {
break;
}

const sheet = style.sheet;
// Always increment the sheet index.
sheetIndex++;
Expand Down Expand Up @@ -336,13 +355,14 @@ export const styleSheetsBenchmark: IBenchmark<
if (scenario.cleanupSuite) {
await scenario.cleanupSuite();
}
progress?.emit({ percentage: 100 });
stopSignal?.disconnect(stopListener);
return {
results: results,
reference: reference.times,
tags: reportTagCounts(),
totalTime: Date.now() - start,
type: 'time'
type: 'time',
interrupted: stop
};
}
};
Expand All @@ -354,8 +374,14 @@ export const styleRuleBenchmark: IBenchmark<ITimingOutcome<IRuleResult>> = {
run: async (
scenario: IScenario,
options: StyleRuleBenchmarkOptions = {},
progress
progress,
stopSignal
): Promise<ITimingOutcome<IRuleResult>> => {
let stop = false;
const stopListener = () => {
stop = true;
};
stopSignal?.connect(stopListener);
const n = options.repeats || 3;
const skipPattern = options.skipPattern
? new RegExp(options.skipPattern, 'g')
Expand All @@ -370,6 +396,9 @@ export const styleRuleBenchmark: IBenchmark<ITimingOutcome<IRuleResult>> = {
const results: IRuleResult[] = [];
const rules = await collectRules(styles, { skipPattern });
for (let i = 0; i < rules.length; i++) {
if (stop) {
break;
}
progress?.emit({ percentage: (100 * i) / rules.length });
const rule = rules[i];
// benchmark without the rule
Expand All @@ -391,13 +420,14 @@ export const styleRuleBenchmark: IBenchmark<ITimingOutcome<IRuleResult>> = {
if (scenario.cleanupSuite) {
await scenario.cleanupSuite();
}
progress?.emit({ percentage: 100 });
stopSignal?.disconnect(stopListener);
return {
results: results,
reference: reference.times,
tags: reportTagCounts(),
totalTime: Date.now() - start,
type: 'time'
type: 'time',
interrupted: stop
};
},
interpretation: (
Expand Down Expand Up @@ -426,8 +456,14 @@ export const styleRuleGroupBenchmark: IBenchmark<
run: async (
scenario: IScenario,
options: StyleRuleGroupBenchmarkOptions = {},
progress
progress,
stopSignal
): Promise<ITimingOutcome<IRuleBlockResult>> => {
let stop = false;
const stopListener = () => {
stop = true;
};
stopSignal?.connect(stopListener);
const n = options.repeats || 3;
const skipPattern = options.skipPattern
? new RegExp(options.skipPattern, 'g')
Expand All @@ -450,6 +486,9 @@ export const styleRuleGroupBenchmark: IBenchmark<
randomization < randomizations + 1;
randomization++
) {
if (stop) {
break;
}
if (randomization !== 0) {
styles = shuffled(styles);
}
Expand All @@ -459,6 +498,9 @@ export const styleRuleGroupBenchmark: IBenchmark<
);

for (let blocks = minBlocks; blocks <= maxBlocks; blocks++) {
if (stop) {
break;
}
step += 1;
progress?.emit({ percentage: (100 * step) / total });
const rulesPerBlock = Math.round(allRules.length / blocks);
Expand Down Expand Up @@ -500,13 +542,14 @@ export const styleRuleGroupBenchmark: IBenchmark<
if (scenario.cleanupSuite) {
await scenario.cleanupSuite();
}
progress?.emit({ percentage: 100 });
stopSignal?.disconnect(stopListener);
return {
results: results,
reference: reference.times,
tags: reportTagCounts(),
totalTime: Date.now() - start,
type: 'time'
type: 'time',
interrupted: stop
};
},
render: renderBlockResult
Expand Down
Loading