Skip to content

Commit

Permalink
Add basic benchmark suite (#451)
Browse files Browse the repository at this point in the history
  • Loading branch information
jsumners committed Aug 30, 2023
1 parent fa386a9 commit b4a1fb6
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 1 deletion.
105 changes: 105 additions & 0 deletions benchmark.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
'use strict'

// We do not expect amazing numbers from `pino-pretty` as the whole purpose
// of the module is a very slow operation. However, this benchmark should give
// us some guidance on how features, or code changes, will affect the
// performance of the module.

const bench = require('fastbench')
const {
prettyFactory
} = require('./index')

const max = 10
const tstampMillis = 1693401358754

/* eslint-disable no-var */
const run = bench([
function basicLog (cb) {
const pretty = prettyFactory({})
const input = `{"time":${tstampMillis},"pid":1,"hostname":"foo","msg":"benchmark","foo":"foo","bar":{"bar":"bar"}}\n`
for (var i = 0; i < max; i += 1) {
pretty(input)
}
setImmediate(cb)
},

function objectLog (cb) {
const pretty = prettyFactory({})
const input = {
time: tstampMillis,
pid: 1,
hostname: 'foo',
msg: 'benchmark',
foo: 'foo',
bar: { bar: 'bar' }
}
for (var i = 0; i < max; i += 1) {
pretty(input)
}
setImmediate(cb)
},

function coloredLog (cb) {
const pretty = prettyFactory({ colorize: true })
const input = `{"time":${tstampMillis},"pid":1,"hostname":"foo","msg":"benchmark","foo":"foo","bar":{"bar":"bar"}}\n`
for (var i = 0; i < max; i += 1) {
pretty(input)
}
setImmediate(cb)
},

function customPrettifiers (cb) {
const pretty = prettyFactory({
customPrettifiers: {
time (tstamp) {
return tstamp
},
pid () {
return ''
}
}
})
const input = `{"time":${tstampMillis},"pid":1,"hostname":"foo","msg":"benchmark","foo":"foo","bar":{"bar":"bar"}}\n`
for (var i = 0; i < max; i += 1) {
pretty(input)
}
setImmediate(cb)
},

function logWithErrorObject (cb) {
const pretty = prettyFactory({})
const err = Error('boom')
const input = `{"time":${tstampMillis},"pid":1,"hostname":"foo","msg":"benchmark","foo":"foo","bar":{"bar":"bar"},"err":{"message":"${err.message}","stack":"${err.stack}"}}\n`
for (var i = 0; i < max; i += 1) {
pretty(input)
}
setImmediate(cb)
},

function logRemappedMsgErrKeys (cb) {
const pretty = prettyFactory({
messageKey: 'message',
errorLikeObjectKeys: ['myError']
})
const err = Error('boom')
const input = `{"time":${tstampMillis},"pid":1,"hostname":"foo","message":"benchmark","foo":"foo","bar":{"bar":"bar"},"myError":{"message":"${err.message}","stack":"${err.stack}"}}\n`
for (var i = 0; i < max; i += 1) {
pretty(input)
}
setImmediate(cb)
},

function messageFormatString (cb) {
const pretty = prettyFactory({
messageFormat: '{levelLabel}{if pid} {pid} - {end}{msg}'
})
const input = `{"time":${tstampMillis},"pid":1,"hostname":"foo","msg":"benchmark","foo":"foo","bar":{"bar":"bar"}}\n`
for (var i = 0; i < max; i += 1) {
pretty(input)
}
setImmediate(cb)
}
], 10000)

run(run)
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
"dateformat": "^4.6.3",
"fast-copy": "^3.0.0",
"fast-safe-stringify": "^2.1.1",
"joycon": "^3.1.1",
"help-me": "^4.0.1",
"joycon": "^3.1.1",
"minimist": "^1.2.6",
"on-exit-leak-free": "^2.1.0",
"pino-abstract-transport": "^1.0.0",
Expand All @@ -49,6 +49,7 @@
},
"devDependencies": {
"@types/node": "^20.1.0",
"fastbench": "^1.0.1",
"pino": "^8.0.0",
"pre-commit": "^1.2.2",
"rimraf": "^3.0.2",
Expand Down

0 comments on commit b4a1fb6

Please sign in to comment.