From aa16f6bac44fc135b763cd33c52b9654b36770be Mon Sep 17 00:00:00 2001 From: Neal Beeken Date: Thu, 6 Feb 2025 14:56:15 -0500 Subject: [PATCH] chore(NODE-6733): add cpu bench test --- .../src/suites/single_bench/primes.mts | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 test/benchmarks/driver_bench/src/suites/single_bench/primes.mts diff --git a/test/benchmarks/driver_bench/src/suites/single_bench/primes.mts b/test/benchmarks/driver_bench/src/suites/single_bench/primes.mts new file mode 100644 index 00000000000..6b5906408cc --- /dev/null +++ b/test/benchmarks/driver_bench/src/suites/single_bench/primes.mts @@ -0,0 +1,49 @@ +import assert from 'node:assert/strict'; + +const findPrimesBelow = 1_000_000; +const expectedPrimes = 78_498; + +// byteLength of +// BSON.serialize({ primes: Buffer.from(new Int32Array(sieveOfEratosthenes(findPrimesBelow)).buffer) }).byteLength) +// a bin data of int32s +const byteLength = 314_010; + +export const taskSize = 3.1401000000000003; // ~3MB worth of work + +assert.equal(taskSize, byteLength * 10e-6); // taskSize should stay hardcoded, checking here the math is done right. + +/** @see https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes */ +export function sieveOfEratosthenes(n: number) { + // Create a boolean array "prime[0..n]" and initialize + // all entries as true. A value in prime[i] will + // become false if i is Not a prime + const prime = Array.from({ length: n + 1 }, () => true); + + // We know 0 and 1 are not prime + prime[0] = false; + prime[1] = false; + + for (let p = 2; p * p <= n; p++) { + // If prime[p] is not changed, then it is a prime + if (prime[p] === true) { + // Update all multiples of p as false + for (let i = p * p; i <= n; i += p) { + prime[i] = false; + } + } + } + + // Collecting all prime numbers + const primes = []; + for (let i = 2; i <= n; i++) { + if (prime[i] === true) { + primes.push(i); + } + } + + return primes; +} + +export async function run() { + assert.equal(sieveOfEratosthenes(findPrimesBelow).length, expectedPrimes); +}