From 59ca0cc1ce3631a3aa4fe16958cbfd7377dd4b5a Mon Sep 17 00:00:00 2001 From: David-Emmanuel DIVERNOIS Date: Tue, 12 Nov 2024 13:37:42 +0100 Subject: [PATCH] Run garbage collection before each benchmark test --- benchmarks/basic.bench.ts | 121 ++++++++++++------ benchmarks/gc.ts | 6 + .../cellxBench.bench.ts | 3 +- .../js-reactivity-benchmarks/dynamic.bench.ts | 5 +- .../kairo/avoidable.bench.ts | 3 +- .../kairo/broad.bench.ts | 5 +- .../kairo/deep.bench.ts | 3 +- .../kairo/diamond.bench.ts | 5 +- .../kairo/mux.bench.ts | 3 +- .../kairo/repeated.bench.ts | 3 +- .../kairo/triangle.bench.ts | 3 +- .../kairo/unstable.bench.ts | 3 +- .../molBench.bench.ts | 3 +- .../js-reactivity-benchmarks/sBench.bench.ts | 3 +- vitest.config.ts | 2 + 15 files changed, 116 insertions(+), 55 deletions(-) create mode 100644 benchmarks/gc.ts diff --git a/benchmarks/basic.bench.ts b/benchmarks/basic.bench.ts index aa8570a..eed7cff 100644 --- a/benchmarks/basic.bench.ts +++ b/benchmarks/basic.bench.ts @@ -1,6 +1,7 @@ import { bench, describe } from 'vitest'; import type { ReadableSignal, Unsubscriber } from '../src'; import { computed, derived, DerivedStore, readable, Store, writable } from '../src'; +import { setup } from './gc'; class StoreClass extends Store {} class DoubleStoreClass extends DerivedStore> { @@ -10,73 +11,117 @@ class DoubleStoreClass extends DerivedStore> { } describe('creating base stores', () => { - bench('writable', () => { - writable(0); - }); - bench('readable', () => { - readable(0); - }); + bench( + 'writable', + () => { + writable(0); + }, + { setup } + ); + bench( + 'readable', + () => { + readable(0); + }, + { setup } + ); - bench('new StoreClass', () => { - new StoreClass(0); - }); + bench( + 'new StoreClass', + () => { + new StoreClass(0); + }, + { setup } + ); }); describe('creating derived stores', () => { const baseStore = writable(0); - bench('computed', () => { - computed(() => 2 * baseStore()); - }); + bench( + 'computed', + () => { + computed(() => 2 * baseStore()); + }, + { setup } + ); - bench('derived', () => { - derived(baseStore, (a) => 2 * a); - }); + bench( + 'derived', + () => { + derived(baseStore, (a) => 2 * a); + }, + { setup } + ); - bench('new DoubleStoreClass', () => { - new DoubleStoreClass(baseStore, 0); - }); + bench( + 'new DoubleStoreClass', + () => { + new DoubleStoreClass(baseStore, 0); + }, + { setup } + ); }); describe('updating derived stores', () => { const baseStore1 = writable(0); computed(() => 2 * baseStore1()).subscribe(() => {}); let count1 = 0; - bench('computed', () => { - count1++; - baseStore1.set(count1); - }); + bench( + 'computed', + () => { + count1++; + baseStore1.set(count1); + }, + { setup } + ); const baseStore2 = writable(0); derived(baseStore2, (a) => 2 * a).subscribe(() => {}); let count2 = 0; - bench('derived', () => { - count2++; - baseStore2.set(count2); - }); + bench( + 'derived', + () => { + count2++; + baseStore2.set(count2); + }, + { setup } + ); const baseStore3 = writable(0); new DoubleStoreClass(baseStore3, 0).subscribe(() => {}); let count3 = 0; - bench('DoubleStoreClass', () => { - count3++; - baseStore3.set(count3); - }); + bench( + 'DoubleStoreClass', + () => { + count3++; + baseStore3.set(count3); + }, + { setup } + ); }); describe('updating writable stores', () => { const storeWithoutSubscriber = writable(0); let count1 = 0; - bench('without subscriber', () => { - count1++; - storeWithoutSubscriber.set(count1); - }); + bench( + 'without subscriber', + () => { + count1++; + storeWithoutSubscriber.set(count1); + }, + { setup } + ); const storeWithSubscriber = writable(0); storeWithSubscriber.subscribe(() => {}); let count2 = 0; - bench('with subscriber', () => { - count2++; - storeWithSubscriber.set(count2); - }); + bench( + 'with subscriber', + () => { + count2++; + storeWithSubscriber.set(count2); + }, + { setup } + ); }); diff --git a/benchmarks/gc.ts b/benchmarks/gc.ts new file mode 100644 index 0000000..6c9c583 --- /dev/null +++ b/benchmarks/gc.ts @@ -0,0 +1,6 @@ +const gc = globalThis.gc; +export const setup = gc + ? () => { + gc(); + } + : () => {}; diff --git a/benchmarks/js-reactivity-benchmarks/cellxBench.bench.ts b/benchmarks/js-reactivity-benchmarks/cellxBench.bench.ts index eb6f691..1804de2 100644 --- a/benchmarks/js-reactivity-benchmarks/cellxBench.bench.ts +++ b/benchmarks/js-reactivity-benchmarks/cellxBench.bench.ts @@ -3,6 +3,7 @@ import { bench, expect } from 'vitest'; import { batch, computed, writable } from '../../src'; import type { ReadableSignal } from '../../src'; +import { setup } from '../gc'; // The following is an implementation of the cellx benchmark https://github.com/Riim/cellx/blob/master/perf/perf.html @@ -89,5 +90,5 @@ const expected: Record = { for (const layers in expected) { const params = expected[layers]; - bench(`cellx${layers}`, () => cellx(+layers, params[0], params[1]), { throws: true }); + bench(`cellx${layers}`, () => cellx(+layers, params[0], params[1]), { throws: true, setup }); } diff --git a/benchmarks/js-reactivity-benchmarks/dynamic.bench.ts b/benchmarks/js-reactivity-benchmarks/dynamic.bench.ts index 313b24a..3bf866d 100644 --- a/benchmarks/js-reactivity-benchmarks/dynamic.bench.ts +++ b/benchmarks/js-reactivity-benchmarks/dynamic.bench.ts @@ -3,6 +3,7 @@ import { bench, expect } from 'vitest'; import type { ReadableSignal, WritableSignal } from '../../src'; import { computed, writable } from '../../src'; +import { setup } from '../gc'; // from https://github.com/milomg/js-reactivity-benchmark/blob/main/src/util/pseudoRandom.ts @@ -322,8 +323,6 @@ for (const config of perfTests) { expect(counter.count).toBe(config.expected.count); } }, - { - throws: true, - } + { throws: true, setup } ); } diff --git a/benchmarks/js-reactivity-benchmarks/kairo/avoidable.bench.ts b/benchmarks/js-reactivity-benchmarks/kairo/avoidable.bench.ts index 0944032..5597567 100644 --- a/benchmarks/js-reactivity-benchmarks/kairo/avoidable.bench.ts +++ b/benchmarks/js-reactivity-benchmarks/kairo/avoidable.bench.ts @@ -2,6 +2,7 @@ import { bench, expect } from 'vitest'; import { computed, writable } from '../../../src'; +import { setup } from '../../gc'; function busy() { let a = 0; @@ -32,5 +33,5 @@ bench( expect(computed5()).toBe(6); } }, - { throws: true } + { throws: true, setup } ); diff --git a/benchmarks/js-reactivity-benchmarks/kairo/broad.bench.ts b/benchmarks/js-reactivity-benchmarks/kairo/broad.bench.ts index 05c7e75..9beb5cb 100644 --- a/benchmarks/js-reactivity-benchmarks/kairo/broad.bench.ts +++ b/benchmarks/js-reactivity-benchmarks/kairo/broad.bench.ts @@ -1,8 +1,9 @@ // adapted from https://github.com/milomg/js-reactivity-benchmark/blob/main/src/kairo/broad.ts import { bench, expect } from 'vitest'; -import { computed, writable } from '../../../src'; import type { ReadableSignal } from '../../../src'; +import { computed, writable } from '../../../src'; +import { setup } from '../../gc'; const loopCount = 50; @@ -35,5 +36,5 @@ bench( } expect(callCounter).toBe(atleast); }, - { throws: true } + { throws: true, setup } ); diff --git a/benchmarks/js-reactivity-benchmarks/kairo/deep.bench.ts b/benchmarks/js-reactivity-benchmarks/kairo/deep.bench.ts index d38a9af..180cc1b 100644 --- a/benchmarks/js-reactivity-benchmarks/kairo/deep.bench.ts +++ b/benchmarks/js-reactivity-benchmarks/kairo/deep.bench.ts @@ -3,6 +3,7 @@ import { bench, expect } from 'vitest'; import type { ReadableSignal } from '../../../src'; import { computed, writable } from '../../../src'; +import { setup } from '../../gc'; const len = 50; @@ -35,5 +36,5 @@ bench( } expect(callCounter).toBe(atleast); }, - { throws: true } + { throws: true, setup } ); diff --git a/benchmarks/js-reactivity-benchmarks/kairo/diamond.bench.ts b/benchmarks/js-reactivity-benchmarks/kairo/diamond.bench.ts index e75bec8..c1c0758 100644 --- a/benchmarks/js-reactivity-benchmarks/kairo/diamond.bench.ts +++ b/benchmarks/js-reactivity-benchmarks/kairo/diamond.bench.ts @@ -3,6 +3,7 @@ import { bench, expect } from 'vitest'; import type { ReadableSignal } from '../../../src'; import { computed, writable } from '../../../src'; +import { setup } from '../../gc'; const width = 5; @@ -31,7 +32,5 @@ bench( } expect(callCounter).toBe(atleast); }, - { - throws: true, - } + { throws: true, setup } ); diff --git a/benchmarks/js-reactivity-benchmarks/kairo/mux.bench.ts b/benchmarks/js-reactivity-benchmarks/kairo/mux.bench.ts index 2144f04..94b7e35 100644 --- a/benchmarks/js-reactivity-benchmarks/kairo/mux.bench.ts +++ b/benchmarks/js-reactivity-benchmarks/kairo/mux.bench.ts @@ -2,6 +2,7 @@ import { bench, expect } from 'vitest'; import { computed, writable } from '../../../src'; +import { setup } from '../../gc'; const heads = new Array(100).fill(null).map(() => writable(0)); const mux = computed(() => { @@ -27,5 +28,5 @@ bench( expect(splited[i]()).toBe(i * 2 + 1); } }, - { throws: true } + { throws: true, setup } ); diff --git a/benchmarks/js-reactivity-benchmarks/kairo/repeated.bench.ts b/benchmarks/js-reactivity-benchmarks/kairo/repeated.bench.ts index 537ea54..b6a2b35 100644 --- a/benchmarks/js-reactivity-benchmarks/kairo/repeated.bench.ts +++ b/benchmarks/js-reactivity-benchmarks/kairo/repeated.bench.ts @@ -2,6 +2,7 @@ import { bench, expect } from 'vitest'; import { computed, writable } from '../../../src'; +import { setup } from '../../gc'; const size = 30; @@ -34,5 +35,5 @@ bench( } expect(callCounter).toBe(atleast); }, - { throws: true } + { throws: true, setup } ); diff --git a/benchmarks/js-reactivity-benchmarks/kairo/triangle.bench.ts b/benchmarks/js-reactivity-benchmarks/kairo/triangle.bench.ts index 8ff75f5..27d23ce 100644 --- a/benchmarks/js-reactivity-benchmarks/kairo/triangle.bench.ts +++ b/benchmarks/js-reactivity-benchmarks/kairo/triangle.bench.ts @@ -3,6 +3,7 @@ import { bench, expect } from 'vitest'; import type { ReadableSignal } from '../../../src'; import { computed, writable } from '../../../src'; +import { setup } from '../../gc'; const width = 10; @@ -41,7 +42,7 @@ bench( } expect(callCounter).toBe(atleast); }, - { throws: true } + { throws: true, setup } ); function count(number: number) { diff --git a/benchmarks/js-reactivity-benchmarks/kairo/unstable.bench.ts b/benchmarks/js-reactivity-benchmarks/kairo/unstable.bench.ts index 5a5c11f..744f3d5 100644 --- a/benchmarks/js-reactivity-benchmarks/kairo/unstable.bench.ts +++ b/benchmarks/js-reactivity-benchmarks/kairo/unstable.bench.ts @@ -2,6 +2,7 @@ import { bench, expect } from 'vitest'; import { computed, writable } from '../../../src'; +import { setup } from '../../gc'; const head = writable(0); const double = computed(() => head() * 2); @@ -33,5 +34,5 @@ bench( } expect(callCounter).toBe(atleast); }, - { throws: true } + { throws: true, setup } ); diff --git a/benchmarks/js-reactivity-benchmarks/molBench.bench.ts b/benchmarks/js-reactivity-benchmarks/molBench.bench.ts index 0e32652..91dbfb8 100644 --- a/benchmarks/js-reactivity-benchmarks/molBench.bench.ts +++ b/benchmarks/js-reactivity-benchmarks/molBench.bench.ts @@ -2,6 +2,7 @@ import { bench } from 'vitest'; import { batch, computed, writable } from '../../src'; +import { setup } from '../gc'; function fib(n: number): number { if (n < 2) return 1; @@ -42,5 +43,5 @@ bench( }); } }, - { throws: true } + { throws: true, setup } ); diff --git a/benchmarks/js-reactivity-benchmarks/sBench.bench.ts b/benchmarks/js-reactivity-benchmarks/sBench.bench.ts index 9f23e23..a331f22 100644 --- a/benchmarks/js-reactivity-benchmarks/sBench.bench.ts +++ b/benchmarks/js-reactivity-benchmarks/sBench.bench.ts @@ -3,6 +3,7 @@ import { bench } from 'vitest'; import type { ReadableSignal, WritableSignal } from '../../src'; import { computed, writable } from '../../src'; +import { setup } from '../gc'; // Inspired by https://github.com/solidjs/solid/blob/main/packages/solid/bench/bench.cjs @@ -30,7 +31,7 @@ defineBench(updateComputations1to4, COUNT * 4, 1); defineBench(updateComputations1to1000, COUNT * 4, 1); function defineBench(fn: (n: number, sources: any[]) => void, n: number, scount: number) { - bench(fn.name, () => fn(n, createDataSignals(scount, []))); + bench(fn.name, () => fn(n, createDataSignals(scount, [])), { throws: true, setup }); } function onlyCreateDataSignals() { diff --git a/vitest.config.ts b/vitest.config.ts index 4188569..99d199b 100644 --- a/vitest.config.ts +++ b/vitest.config.ts @@ -1,6 +1,8 @@ import { defineConfig } from 'vitest/config'; import JsonArrayReporter from './benchmarks/jsonArrayReporter'; +process.env.NODE_OPTIONS = `${process.env.NODE_OPTIONS ?? ''} --expose-gc`; + export default defineConfig({ test: { setupFiles: ['test.ts'],