blob_store.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. const crypto = require('node:crypto')
  2. const benny = require('benny')
  3. const { Blob } = require('overleaf-editor-core')
  4. const mongoBackend = require('../storage/lib/blob_store/mongo')
  5. const postgresBackend = require('../storage/lib/blob_store/postgres')
  6. const cleanup = require('../test/acceptance/js/storage/support/cleanup')
  7. const MONGO_PROJECT_ID = '637386deb4ce3c62acd3848e'
  8. const POSTGRES_PROJECT_ID = '123'
  9. async function run() {
  10. for (const blobCount of [1, 10, 100, 1000, 10000, 100000, 500000]) {
  11. await cleanup.everything()
  12. const blobs = createBlobs(blobCount)
  13. await insertBlobs(blobs)
  14. const randomHashes = getRandomHashes(blobs, 100)
  15. await benny.suite(
  16. `Read a blob in a project with ${blobCount} blobs`,
  17. benny.add('Mongo backend', async () => {
  18. await mongoBackend.findBlob(MONGO_PROJECT_ID, randomHashes[0])
  19. }),
  20. benny.add('Postgres backend', async () => {
  21. await postgresBackend.findBlob(POSTGRES_PROJECT_ID, randomHashes[0])
  22. }),
  23. benny.cycle(),
  24. benny.complete()
  25. )
  26. await benny.suite(
  27. `Read 100 blobs in a project with ${blobCount} blobs`,
  28. benny.add('Mongo backend', async () => {
  29. await mongoBackend.findBlobs(MONGO_PROJECT_ID, randomHashes)
  30. }),
  31. benny.add('Postgres backend', async () => {
  32. await postgresBackend.findBlobs(POSTGRES_PROJECT_ID, randomHashes)
  33. }),
  34. benny.cycle(),
  35. benny.complete()
  36. )
  37. await benny.suite(
  38. `Insert a blob in a project with ${blobCount} blobs`,
  39. benny.add('Mongo backend', async () => {
  40. const [newBlob] = createBlobs(1)
  41. await mongoBackend.insertBlob(MONGO_PROJECT_ID, newBlob)
  42. }),
  43. benny.add('Postgres backend', async () => {
  44. const [newBlob] = createBlobs(1)
  45. await postgresBackend.insertBlob(POSTGRES_PROJECT_ID, newBlob)
  46. }),
  47. benny.cycle(),
  48. benny.complete()
  49. )
  50. }
  51. }
  52. function createBlobs(blobCount) {
  53. const blobs = []
  54. for (let i = 0; i < blobCount; i++) {
  55. const hash = crypto.randomBytes(20).toString('hex')
  56. blobs.push(new Blob(hash, 42, 42))
  57. }
  58. return blobs
  59. }
  60. async function insertBlobs(blobs) {
  61. for (const blob of blobs) {
  62. await Promise.all([
  63. mongoBackend.insertBlob(MONGO_PROJECT_ID, blob),
  64. postgresBackend.insertBlob(POSTGRES_PROJECT_ID, blob),
  65. ])
  66. }
  67. }
  68. function getRandomHashes(blobs, count) {
  69. const hashes = []
  70. for (let i = 0; i < count; i++) {
  71. const index = Math.floor(Math.random() * blobs.length)
  72. hashes.push(blobs[index].getHash())
  73. }
  74. return hashes
  75. }
  76. module.exports = run