apply.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. const text = require('../app/js/sharejs/types/text.js')
  2. const TEST_RUNS = 1_000_000
  3. const MAX_OPS_BATCH_SIZE = 35
  4. const KB = 1000
  5. function runTestCase(testCase, documentSizeBytes) {
  6. const initialText = 'A'.repeat(documentSizeBytes)
  7. console.log(`test: ${testCase.name}`)
  8. console.log(`opsBatchSize\topsPerSeconds ${documentSizeBytes / 1000}KB`)
  9. for (let i = 1; i <= MAX_OPS_BATCH_SIZE; i++) {
  10. const ops = testCase(documentSizeBytes, i)
  11. let timeTotal = 0
  12. for (let i = 0; i < TEST_RUNS; i++) {
  13. const start = performance.now()
  14. try {
  15. text.apply(initialText, ops)
  16. } catch {
  17. console.error(`test failed: ${testCase.name}, with ops:`)
  18. console.error(ops)
  19. return
  20. }
  21. const done = performance.now()
  22. timeTotal += done - start
  23. }
  24. const opsPerSeconds = TEST_RUNS / (timeTotal / 1000)
  25. console.log(`${i}\t${opsPerSeconds}`)
  26. }
  27. }
  28. const randomAdditionTestCase = (docSize, opsSize) =>
  29. Array.from({ length: opsSize }, () => ({
  30. p: Math.floor(Math.random() * docSize),
  31. i: 'B',
  32. }))
  33. const sequentialAdditionsTestCase = (docSize, opsSize) =>
  34. Array.from({ length: opsSize }, (_, i) => ({ p: i + docSize, i: 'B' }))
  35. const sequentialAdditionsInMiddleTestCase = (docSize, opsSize) =>
  36. Array.from({ length: opsSize }, (_, i) => ({
  37. p: Math.floor(docSize / 2) + i,
  38. i: 'B',
  39. }))
  40. const randomDeletionTestCase = (docSize, opsSize) =>
  41. Array.from({ length: opsSize }, (_, i) => ({
  42. p: Math.floor(Math.random() * (docSize - 1 - i)),
  43. d: 'A',
  44. }))
  45. const sequentialDeletionTestCase = (docSize, opsSize) =>
  46. Array.from({ length: opsSize }, (_, i) => ({
  47. p: docSize - 1 - i,
  48. d: 'A',
  49. }))
  50. const sequentialDeletionInMiddleTestCase = (docSize, opsSize) =>
  51. Array.from({ length: opsSize }, (_, i) => ({
  52. p: Math.floor(docSize / 2),
  53. d: 'A',
  54. }))
  55. for (const docSize of [10 * KB, 100 * KB]) {
  56. for (const testCase of [
  57. randomAdditionTestCase,
  58. sequentialAdditionsTestCase,
  59. sequentialAdditionsInMiddleTestCase,
  60. randomDeletionTestCase,
  61. sequentialDeletionTestCase,
  62. sequentialDeletionInMiddleTestCase,
  63. ]) {
  64. runTestCase(testCase, docSize)
  65. }
  66. }