From 94694a63850064f57b287696ed243edc7e77dff2 Mon Sep 17 00:00:00 2001 From: Mathias Jakobsen Date: Fri, 21 Jun 2024 11:47:27 +0100 Subject: [PATCH] Merge pull request #19057 from overleaf/mj-benchmark-seed-random [web] Add quick and dirty PRNG for seeding benchmark runs GitOrigin-RevId: 079f9b565f17b44d7062f6b93c26f694e486c6b9 --- services/web/scripts/lezer-latex/random.mjs | 19 +++++++++++++++++++ .../lezer-latex/test-incremental-parser.mjs | 10 ++++++++-- 2 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 services/web/scripts/lezer-latex/random.mjs diff --git a/services/web/scripts/lezer-latex/random.mjs b/services/web/scripts/lezer-latex/random.mjs new file mode 100644 index 0000000000..0c07468b59 --- /dev/null +++ b/services/web/scripts/lezer-latex/random.mjs @@ -0,0 +1,19 @@ +// Super quick and dirty LCG PRNG + +const m = 0xffffffff +let X = Math.floor(Math.random() * (m - 1)) +const a = 16807 +const c = 0 + +// Should probably be a large-ish number +export function seed(i) { + if (i < 0) { + throw new Error('Seed must be a positive integer') + } + X = i & m +} + +export function random() { + X = (a * X + c) % m + return X / m +} diff --git a/services/web/scripts/lezer-latex/test-incremental-parser.mjs b/services/web/scripts/lezer-latex/test-incremental-parser.mjs index 11cef79682..a32894e16e 100644 --- a/services/web/scripts/lezer-latex/test-incremental-parser.mjs +++ b/services/web/scripts/lezer-latex/test-incremental-parser.mjs @@ -5,10 +5,16 @@ import * as path from 'path' import { fileURLToPath } from 'url' import { TreeFragment } from '@lezer/common' import minimist from 'minimist' +import { seed, random } from './random.mjs' const argv = minimist(process.argv.slice(2)) const NUMBER_OF_OPS = argv.ops || 1000 const CSV_OUTPUT = argv.csv || false +const SEED = argv.seed + +if (SEED) { + seed(SEED) +} const __dirname = path.dirname(fileURLToPath(import.meta.url)) @@ -108,13 +114,13 @@ function writeTextAt(document, position, text) { function randomInsertions(document, num) { return timedChanges(document, num, currentDoc => - insertAt(currentDoc, Math.floor(Math.random() * currentDoc.length), 'a') + insertAt(currentDoc, Math.floor(random() * currentDoc.length), 'a') ) } function randomDeletions(document, num) { return timedChanges(document, num, currentDoc => - deleteAt(currentDoc, Math.floor(Math.random() * currentDoc.length), 1) + deleteAt(currentDoc, Math.floor(random() * currentDoc.length), 1) ) }