Skip to content

Commit

Permalink
Run garbage collection before each benchmark test
Browse files Browse the repository at this point in the history
  • Loading branch information
divdavem committed Nov 12, 2024
1 parent f5987d3 commit 59ca0cc
Show file tree
Hide file tree
Showing 15 changed files with 116 additions and 55 deletions.
121 changes: 83 additions & 38 deletions benchmarks/basic.bench.ts
Original file line number Diff line number Diff line change
@@ -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<number> {}
class DoubleStoreClass extends DerivedStore<number, ReadableSignal<number>> {
Expand All @@ -10,73 +11,117 @@ class DoubleStoreClass extends DerivedStore<number, ReadableSignal<number>> {
}

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 }
);
});
6 changes: 6 additions & 0 deletions benchmarks/gc.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const gc = globalThis.gc;
export const setup = gc
? () => {
gc();
}
: () => {};
3 changes: 2 additions & 1 deletion benchmarks/js-reactivity-benchmarks/cellxBench.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -89,5 +90,5 @@ const expected: Record<number, BenchmarkResults> = {

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 });
}
5 changes: 2 additions & 3 deletions benchmarks/js-reactivity-benchmarks/dynamic.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -322,8 +323,6 @@ for (const config of perfTests) {
expect(counter.count).toBe(config.expected.count);
}
},
{
throws: true,
}
{ throws: true, setup }
);
}
3 changes: 2 additions & 1 deletion benchmarks/js-reactivity-benchmarks/kairo/avoidable.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { bench, expect } from 'vitest';
import { computed, writable } from '../../../src';
import { setup } from '../../gc';

function busy() {
let a = 0;
Expand Down Expand Up @@ -32,5 +33,5 @@ bench(
expect(computed5()).toBe(6);
}
},
{ throws: true }
{ throws: true, setup }
);
5 changes: 3 additions & 2 deletions benchmarks/js-reactivity-benchmarks/kairo/broad.bench.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand Down Expand Up @@ -35,5 +36,5 @@ bench(
}
expect(callCounter).toBe(atleast);
},
{ throws: true }
{ throws: true, setup }
);
3 changes: 2 additions & 1 deletion benchmarks/js-reactivity-benchmarks/kairo/deep.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -35,5 +36,5 @@ bench(
}
expect(callCounter).toBe(atleast);
},
{ throws: true }
{ throws: true, setup }
);
5 changes: 2 additions & 3 deletions benchmarks/js-reactivity-benchmarks/kairo/diamond.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -31,7 +32,5 @@ bench(
}
expect(callCounter).toBe(atleast);
},
{
throws: true,
}
{ throws: true, setup }
);
3 changes: 2 additions & 1 deletion benchmarks/js-reactivity-benchmarks/kairo/mux.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(() => {
Expand All @@ -27,5 +28,5 @@ bench(
expect(splited[i]()).toBe(i * 2 + 1);
}
},
{ throws: true }
{ throws: true, setup }
);
3 changes: 2 additions & 1 deletion benchmarks/js-reactivity-benchmarks/kairo/repeated.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { bench, expect } from 'vitest';
import { computed, writable } from '../../../src';
import { setup } from '../../gc';

const size = 30;

Expand Down Expand Up @@ -34,5 +35,5 @@ bench(
}
expect(callCounter).toBe(atleast);
},
{ throws: true }
{ throws: true, setup }
);
3 changes: 2 additions & 1 deletion benchmarks/js-reactivity-benchmarks/kairo/triangle.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -41,7 +42,7 @@ bench(
}
expect(callCounter).toBe(atleast);
},
{ throws: true }
{ throws: true, setup }
);

function count(number: number) {
Expand Down
3 changes: 2 additions & 1 deletion benchmarks/js-reactivity-benchmarks/kairo/unstable.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -33,5 +34,5 @@ bench(
}
expect(callCounter).toBe(atleast);
},
{ throws: true }
{ throws: true, setup }
);
3 changes: 2 additions & 1 deletion benchmarks/js-reactivity-benchmarks/molBench.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -42,5 +43,5 @@ bench(
});
}
},
{ throws: true }
{ throws: true, setup }
);
3 changes: 2 additions & 1 deletion benchmarks/js-reactivity-benchmarks/sBench.bench.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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() {
Expand Down
2 changes: 2 additions & 0 deletions vitest.config.ts
Original file line number Diff line number Diff line change
@@ -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'],
Expand Down

0 comments on commit 59ca0cc

Please sign in to comment.