overleaf/services/clsi/test/bench/hashbench.js

74 lines
1.8 KiB
JavaScript
Raw Normal View History

2021-05-11 14:53:01 +00:00
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()
2021-07-13 11:04:48 +00:00
ContentCacheManager.update(dir, filePath, x => {
2021-05-11 14:53:01 +00:00
const t1 = process.hrtime.bigint()
const cold = Number(t1 - t0) / 1e6
2021-07-13 11:04:48 +00:00
ContentCacheManager.update(dir, filePath, x => {
2021-05-11 14:53:01 +00:00
const t2 = process.hrtime.bigint()
const warm = Number(t2 - t1) / 1e6
fs.rm(dir, { recursive: true, force: true }, err => {
2021-05-11 14:53:01 +00:00
if (err) {
return callback(err)
}
console.log(
'uvthreads',
process.env.UV_THREADPOOL_SIZE,
2021-05-11 14:53:01 +00:00
filePath,
'hashType',
hashType,
'cold-start',
cold.toFixed(2),
'ms',
'warm-start',
warm.toFixed(2),
'ms'
)
callback(null, [hashType, cold, warm])
})
})
})
})
}
2021-07-13 11:04:48 +00:00
const jobs = []
files.forEach(file => {
jobs.push(cb => {
2021-05-11 14:53:01 +00:00
test('md5', file, cb)
})
2021-07-13 11:04:48 +00:00
jobs.push(cb => {
2021-05-11 14:53:01 +00:00
test('sha1', file, cb)
})
2021-07-13 11:04:48 +00:00
jobs.push(cb => {
2021-05-11 14:53:01 +00:00
test('hmac-sha1', file, cb)
})
2021-07-13 11:04:48 +00:00
jobs.push(cb => {
2021-05-11 14:53:01 +00:00
test('sha256', file, cb)
})
})
async.timesSeries(10, (n, cb) => {
async.series(jobs, cb)
})