Skip to content

Commit

Permalink
Support measuring the cost of hooks and events
Browse files Browse the repository at this point in the history
  • Loading branch information
vweevers committed Nov 19, 2022
1 parent 07beef5 commit 331f0a6
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
2 changes: 1 addition & 1 deletion benchmarks/batch-put.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ exports.run = function (factory, stream, options) {
function report () {
console.log(
'Wrote', options.n, 'entries in',
Math.floor((Date.now() - startTime) / 1e3) + 's,',
((Date.now() - startTime) / 1e3).toFixed(2) + 's,',
(Math.floor((totalBytes / 1048576) * 100) / 100) + 'MB'
)

Expand Down
2 changes: 2 additions & 0 deletions lib/meta.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ function id (meta, peers, invert, opts) {
}
} else if (path.length > 2) {
parts.push(`${path.slice(2).join('.')}.${k}=${v}`)
} else if (path.length === 2 && path[0] === 'options' && (path[1] === 'events' || path[1] === 'hooks')) {
parts.push(`${path[1]}.${k}=${v}`)
} else if (typeof v === 'boolean') {
parts.push(v ? k : `!${k}`)
} else if (typeof v === 'number') {
Expand Down
50 changes: 49 additions & 1 deletion lib/run.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,50 @@ module.exports = function (benchmark, targetId, options) {

mkdirp.sync(path.dirname(csvFile))

const addHooksAndEvents = (db) => {
// Test the cost of adding X amount of hook functions
// E.g. level-bench run batch-put ../memory-level --hooks [ --prewrite=1 ]
// TODO: docs
if (options.hooks) {
for (const name of ['prewrite']) {
const count = parseInt(options.hooks[name] || 0, 10)

if (!Number.isInteger(count) || count < 0) {
throw new RangeError(`The "hooks.${name}" option must be >= 0`)
}

if (count > 0 && (!db.hooks || !db.hooks[name])) {
throw new Error(`Database does not support ${name} hook`)
}

for (let i = 0; i < count; i++) {
db.hooks[name].add(function () {})
}
}
}

// Test the cost of adding X amount of event listeners
// E.g. level-bench run batch-put ../memory-level --events [ --write=1 ]
// TODO: docs
if (options.events) {
for (const name of ['write', 'batch', 'put', 'del']) {
const count = parseInt(options.events[name] || 0, 10)

if (!Number.isInteger(count) || count < 0) {
throw new RangeError(`The "events.${name}" option must be >= 0`)
}

if (count > 0 && (!db.supports.events || !db.supports.events[name])) {
throw new Error(`Database does not support ${name} event`)
}

for (let i = 0; i < count; i++) {
db.on(name, function () {})
}
}
}
}

let factory

if (name === 'rave-level') {
Expand All @@ -103,6 +147,7 @@ module.exports = function (benchmark, targetId, options) {
leader.getMany(['x'], function (err) {
if (err) throw err
const follower = target(loc)
addHooksAndEvents(follower)
follower.open((err) => cb(err, follower))
})
})
Expand All @@ -113,6 +158,7 @@ module.exports = function (benchmark, targetId, options) {

factory = (cb) => {
const db = dbFactory(options.location)
addHooksAndEvents(db)
db.open((err) => cb(err, db))
}
}
Expand All @@ -131,7 +177,9 @@ module.exports = function (benchmark, targetId, options) {
class: undefined,

db: options.db,
benchmark: options.benchmark
benchmark: options.benchmark,
hooks: options.hooks || undefined,
events: options.events || undefined
}
})

Expand Down

0 comments on commit 331f0a6

Please sign in to comment.