From 7b6434ef81d42956f441ac2b32c7eb9e8c3a088d Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Tue, 11 May 2021 15:53:01 +0100 Subject: [PATCH 1/4] add benchmark script for hashing --- services/clsi/test/bench/hashbench.js | 71 +++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 services/clsi/test/bench/hashbench.js diff --git a/services/clsi/test/bench/hashbench.js b/services/clsi/test/bench/hashbench.js new file mode 100644 index 0000000000..9bb79c9089 --- /dev/null +++ b/services/clsi/test/bench/hashbench.js @@ -0,0 +1,71 @@ +const ContentCacheManager = require('../../app/js/ContentCacheManager') +const fs = require('fs') +const crypto = require('crypto') +const path = require('path') +const os = require('os') +const async = require('async') +const _createHash = crypto.createHash + +const files = process.argv.slice(2) + +function test(hashType, filePath, callback) { + // override the default hash in ContentCacheManager + crypto.createHash = function (hash) { + if (hashType === 'hmac-sha1') { + return crypto.createHmac('sha1', 'a secret') + } + hash = hashType + return _createHash(hash) + } + fs.mkdtemp(path.join(os.tmpdir(), 'pdfcache'), (err, dir) => { + if (err) { + return callback(err) + } + const t0 = process.hrtime.bigint() + ContentCacheManager.update(dir, filePath, (x) => { + const t1 = process.hrtime.bigint() + const cold = Number(t1 - t0) / 1e6 + ContentCacheManager.update(dir, filePath, (x) => { + const t2 = process.hrtime.bigint() + const warm = Number(t2 - t1) / 1e6 + fs.rmdir(dir, { recursive: true }, (err) => { + if (err) { + return callback(err) + } + console.log( + filePath, + 'hashType', + hashType, + 'cold-start', + cold.toFixed(2), + 'ms', + 'warm-start', + warm.toFixed(2), + 'ms' + ) + callback(null, [hashType, cold, warm]) + }) + }) + }) + }) +} + +var jobs = [] +files.forEach((file) => { + jobs.push((cb) => { + test('md5', file, cb) + }) + jobs.push((cb) => { + test('sha1', file, cb) + }) + jobs.push((cb) => { + test('hmac-sha1', file, cb) + }) + jobs.push((cb) => { + test('sha256', file, cb) + }) +}) + +async.series(jobs, () => { + console.log('DONE') +}) From cbeba4af001e3aabba29df9f6c26a8d7283babe2 Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Wed, 12 May 2021 16:22:59 +0100 Subject: [PATCH 2/4] run the hash benchmark 10 times --- services/clsi/test/bench/hashbench.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/services/clsi/test/bench/hashbench.js b/services/clsi/test/bench/hashbench.js index 9bb79c9089..e87c3dd059 100644 --- a/services/clsi/test/bench/hashbench.js +++ b/services/clsi/test/bench/hashbench.js @@ -66,6 +66,13 @@ files.forEach((file) => { }) }) -async.series(jobs, () => { - console.log('DONE') -}) +async.timesSeries( + 10, + (n, cb) => { + console.log('run', n) + async.series(jobs, cb) + }, + () => { + console.log('DONE') + } +) From 939ad3af31ea6ff2007b630cd8259606f2edbc8a Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Mon, 17 May 2021 12:00:32 +0100 Subject: [PATCH 3/4] include UV_THREADPOOL_SIZE in benchmark logs --- services/clsi/test/bench/hashbench.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/services/clsi/test/bench/hashbench.js b/services/clsi/test/bench/hashbench.js index e87c3dd059..a8ac898eae 100644 --- a/services/clsi/test/bench/hashbench.js +++ b/services/clsi/test/bench/hashbench.js @@ -33,6 +33,8 @@ function test(hashType, filePath, callback) { return callback(err) } console.log( + 'uvthreads', + process.env.UV_THREADPOOL_SIZE, filePath, 'hashType', hashType, From 8c80ddd27a889fc6a736959676e19508edb5c02c Mon Sep 17 00:00:00 2001 From: Brian Gough Date: Mon, 17 May 2021 12:00:57 +0100 Subject: [PATCH 4/4] remove unnecessary console.log from hash benchmark --- services/clsi/test/bench/hashbench.js | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/services/clsi/test/bench/hashbench.js b/services/clsi/test/bench/hashbench.js index a8ac898eae..bbd9a02855 100644 --- a/services/clsi/test/bench/hashbench.js +++ b/services/clsi/test/bench/hashbench.js @@ -68,13 +68,6 @@ files.forEach((file) => { }) }) -async.timesSeries( - 10, - (n, cb) => { - console.log('run', n) - async.series(jobs, cb) - }, - () => { - console.log('DONE') - } -) +async.timesSeries(10, (n, cb) => { + async.series(jobs, cb) +})