stressTest.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. /* eslint-disable */
  2. // TODO: This file was created by bulk-decaffeinate.
  3. // Sanity-check the conversion and remove this comment.
  4. /*
  5. * decaffeinate suggestions:
  6. * DS101: Remove unnecessary use of Array.from
  7. * DS102: Remove unnecessary code created because of implicit returns
  8. * DS104: Avoid inline assignments
  9. * DS202: Simplify dynamic range loops
  10. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  11. */
  12. // N requests in parallel
  13. // send P correct words and Q incorrect words
  14. // generate incorrect words by qq+random
  15. import fs from 'node:fs'
  16. import async from 'async'
  17. import request from 'request'
  18. // created with
  19. // aspell -d en dump master | aspell -l en expand | shuf -n 150000 > words.txt
  20. const WORDS = 'words.txt'
  21. const wordlist = fs
  22. .readFileSync(WORDS)
  23. .toString()
  24. .split('\n')
  25. .filter(w => w.match(/^[a-z]+$/))
  26. const generateCorrectWords = function (n) {
  27. const words = []
  28. const N = Math.random() > 0.5 ? wordlist.length : 10
  29. for (
  30. let i = 1, end = n, asc = 1 <= end;
  31. asc ? i <= end : i >= end;
  32. asc ? i++ : i--
  33. ) {
  34. const j = Math.floor(N * Math.random())
  35. words.push(wordlist[j])
  36. }
  37. return words
  38. }
  39. const generateIncorrectWords = function (n) {
  40. const words = []
  41. const N = wordlist.length
  42. for (
  43. let i = 1, end = n, asc = 1 <= end;
  44. asc ? i <= end : i >= end;
  45. asc ? i++ : i--
  46. ) {
  47. const j = Math.floor(N * Math.random())
  48. words.push(`qzxq${wordlist[j]}`)
  49. }
  50. return words
  51. }
  52. const makeRequest = function (correctWords, incorrectWords, callback) {
  53. let i, j, w
  54. let i1
  55. let j1
  56. const correctSet = generateCorrectWords(correctWords)
  57. const incorrectSet = generateIncorrectWords(incorrectWords)
  58. correctSet.push('constructor')
  59. incorrectSet.push('qzxqfoofoofoo')
  60. const full = correctSet.concat(incorrectSet)
  61. const bad = []
  62. for (j = 0, i = j; j < correctSet.length; j++, i = j) {
  63. w = correctSet[i]
  64. bad[i] = false
  65. }
  66. for (i1 = 0, i = i1; i1 < incorrectSet.length; i1++, i = i1) {
  67. w = incorrectSet[i]
  68. bad[i + correctSet.length] = true
  69. }
  70. const k = full.length
  71. full.forEach(function (e, i) {
  72. let ref
  73. j = Math.floor(k * Math.random())
  74. ;[full[i], full[j]] = Array.from([full[j], full[i]])
  75. return ([bad[i], bad[j]] = Array.from((ref = [bad[j], bad[i]]))), ref
  76. })
  77. const expected = []
  78. for (j1 = 0, i = j1; j1 < bad.length; j1++, i = j1) {
  79. const tf = bad[i]
  80. if (tf) {
  81. expected.push({ index: i, word: full[i] })
  82. }
  83. }
  84. return request.post(
  85. 'http://127.0.0.1:3005/user/1/check',
  86. { json: true, body: { words: full } },
  87. function (err, req, body) {
  88. let m
  89. const { misspellings } = body
  90. console.log(JSON.stringify({ full, misspellings }))
  91. if (expected.length !== misspellings.length) {
  92. let asc, end
  93. console.log(
  94. 'ERROR: length mismatch',
  95. expected.length,
  96. misspellings.length
  97. )
  98. console.log(full, bad)
  99. console.log('expected', expected, 'mispellings', misspellings)
  100. for (
  101. i = 0,
  102. end = Math.max(expected.length, misspellings.length) - 1,
  103. asc = 0 <= end;
  104. asc ? i <= end : i >= end;
  105. asc ? i++ : i--
  106. ) {
  107. if (expected[i].index !== misspellings[i].index) {
  108. console.log(
  109. 'ERROR',
  110. i,
  111. expected[i],
  112. misspellings[i],
  113. full[misspellings[i].index]
  114. )
  115. }
  116. }
  117. for (m of Array.from(misspellings)) {
  118. console.log(full[m.index], '=>', m)
  119. }
  120. process.exit()
  121. callback('error')
  122. } else {
  123. for (i = 0; i < body.misspellings.length; i++) {
  124. m = body.misspellings[i]
  125. if (m.index !== expected[i].index) {
  126. console.log('ERROR AT RESULT', i, m, expected[i])
  127. process.exit()
  128. callback('error')
  129. }
  130. }
  131. }
  132. return callback(null, full)
  133. }
  134. )
  135. }
  136. const q = async.queue(
  137. (task, callback) =>
  138. setTimeout(
  139. () => makeRequest(task.correct, task.incorrect, callback),
  140. Math.random() * 100
  141. ),
  142. 3
  143. )
  144. q.drain(() => console.log('all items have been processed'))
  145. for (let i = 0; i <= 1000; i++) {
  146. q.push({
  147. correct: Math.floor(30 * Math.random()) + 1,
  148. incorrect: Math.floor(3 * Math.random()),
  149. })
  150. }
  151. // if Math.random() < 0.1
  152. // else
  153. // q.push({correct: Math.floor(100*Math.random()) + 1, incorrect: Math.floor(3*Math.random())})