hashbench.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import ContentCacheManager from '../../app/js/ContentCacheManager.js'
  2. import fs from 'node:fs'
  3. import crypto from 'node:crypto'
  4. import path from 'node:path'
  5. import os from 'node:os'
  6. import async from 'async'
  7. const _createHash = crypto.createHash
  8. const files = process.argv.slice(2)
  9. function test(hashType, filePath, callback) {
  10. // override the default hash in ContentCacheManager
  11. crypto.createHash = function (hash) {
  12. if (hashType === 'hmac-sha1') {
  13. return crypto.createHmac('sha1', 'a secret')
  14. }
  15. hash = hashType
  16. return _createHash(hash)
  17. }
  18. fs.mkdtemp(path.join(os.tmpdir(), 'pdfcache'), (err, dir) => {
  19. if (err) {
  20. return callback(err)
  21. }
  22. const t0 = process.hrtime.bigint()
  23. ContentCacheManager.update(dir, filePath, x => {
  24. const t1 = process.hrtime.bigint()
  25. const cold = Number(t1 - t0) / 1e6
  26. ContentCacheManager.update(dir, filePath, x => {
  27. const t2 = process.hrtime.bigint()
  28. const warm = Number(t2 - t1) / 1e6
  29. fs.rm(dir, { recursive: true, force: true }, err => {
  30. if (err) {
  31. return callback(err)
  32. }
  33. console.log(
  34. 'uvthreads',
  35. process.env.UV_THREADPOOL_SIZE,
  36. filePath,
  37. 'hashType',
  38. hashType,
  39. 'cold-start',
  40. cold.toFixed(2),
  41. 'ms',
  42. 'warm-start',
  43. warm.toFixed(2),
  44. 'ms'
  45. )
  46. callback(null, [hashType, cold, warm])
  47. })
  48. })
  49. })
  50. })
  51. }
  52. const jobs = []
  53. files.forEach(file => {
  54. jobs.push(cb => {
  55. test('md5', file, cb)
  56. })
  57. jobs.push(cb => {
  58. test('sha1', file, cb)
  59. })
  60. jobs.push(cb => {
  61. test('hmac-sha1', file, cb)
  62. })
  63. jobs.push(cb => {
  64. test('sha256', file, cb)
  65. })
  66. })
  67. async.timesSeries(10, (n, cb) => {
  68. async.series(jobs, cb)
  69. })