Skip to content

Instantly share code, notes, and snippets.

@josephg
Last active July 14, 2022 23:07
Show Gist options
  • Save josephg/bcb2e74e52dc9c4651249fdffc48d1cf to your computer and use it in GitHub Desktop.
Save josephg/bcb2e74e52dc9c4651249fdffc48d1cf to your computer and use it in GitHub Desktop.
bench npm jumprope
// Read in a patch file and check that the patches all apply correctly.
// Run with node --expose-gc (file) using input files from here:
// https://github.com/josephg/crdt-benchmarks
const fs = require('fs')
const assert = require('assert')
const zlib = require('zlib')
const v8 = require('v8')
const Rope = require('jumprope')
const filename = process.argv[2]
if (filename == null) {
console.error(`Usage: $ ${process.argv.join(' ')} file.json[.gz]`)
process.exit(1)
}
const {
startContent,
endContent,
txns
} = JSON.parse(
filename.endsWith('.gz')
? zlib.gunzipSync(fs.readFileSync(filename))
: fs.readFileSync(filename, 'utf-8')
)
// console.log('snapshot', v8.writeHeapSnapshot())
console.log(v8.getHeapStatistics())
console.log('heap', process.memoryUsage().heapUsed)
const run = () => {
gc()
const startMemory = v8.getHeapStatistics().used_heap_size
const r = new Rope(startContent)
for (let i = 0; i < txns.length; i++) {
// if (i > 20000) break
// if (i % 10000 == 0) console.log(i)
const {patches} = txns[i]
for (const [pos, delHere, insContent] of patches) {
// console.log(pos, delHere, insContent)
if (delHere > 0) r.del(pos, delHere)
if (insContent !== '') r.insert(pos, insContent)
}
}
gc()
console.log('RAM used:', v8.getHeapStatistics().used_heap_size - startMemory)
console.log(r.toString().length)
console.log(txns.length)
// assert.strictEqual(r.toString(), endContent)
}
console.log('applying', txns.length, 'txns...')
for (let i = 0; i < 10; i++) {
// Warmup
run()
}
console.time('apply')
run()
console.timeEnd('apply')
// gc()
console.log(v8.getHeapStatistics())
console.log('heap', process.memoryUsage().heapUsed)
// console.log('snapshot', v8.writeHeapSnapshot())
// assert.strictEqual(state.text.toSpans().join(''), endContent)
// assert.strictEqual(state.getText().toJSON(), endContent)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment