wechat wss demo

testRandom.js 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. 'use strict'
  2. var mqtt = require('./')
  3. var crypto = require('crypto')
  4. var max = 1E5
  5. var i
  6. var start = Date.now() / 1000
  7. var time
  8. var errors = 0
  9. var packets = 0
  10. var randomPacket
  11. var firstBytes = [
  12. 16 * 1, // CONNECT
  13. 16 * 2, // CONNACK
  14. 16 * 3, // PUBLISH, QoS: 0, No Retain, No Dup
  15. 16 * 3 + 1, // PUBLISH, QoS: 0, Retain, No Dup
  16. 16 * 3 + 8, // PUBLISH, QoS: 0, No Retain, Dup
  17. 16 * 3 + 1 + 8, // PUBLISH, QoS: 0, Retain, Dup
  18. 16 * 3 + 2, // PUBLISH, QoS: 1, No Retain, No Dup
  19. 16 * 3 + 2 + 1, // PUBLISH, QoS: 1, Retain, No Dup
  20. 16 * 3 + 2 + 8, // PUBLISH, QoS: 1, No Retain, Dup
  21. 16 * 3 + 2 + 1 + 8, // PUBLISH, QoS: 1, Retain, Dup
  22. 16 * 3 + 4, // PUBLISH, QoS: 2, No Retain, No Dup
  23. 16 * 3 + 4 + 1, // PUBLISH, QoS: 2, Retain, No Dup
  24. 16 * 3 + 4 + 8, // PUBLISH, QoS: 2, No Retain, Dup
  25. 16 * 3 + 4 + 1 + 8, // PUBLISH, QoS: 2, Retain, Dup
  26. 16 * 4, // PUBACK
  27. 16 * 5, // PUBREC
  28. 16 * 6, // PUBREL
  29. 16 * 7, // PUBCOMP
  30. 16 * 8, // SUBSCRIBE
  31. 16 * 9, // SUBACK
  32. 16 * 10, // UNSUBSCRIBE
  33. 16 * 11, // UNSUBACK
  34. 16 * 12, // PINGREQ
  35. 16 * 13, // PINGRESP
  36. 16 * 14, // DISCONNECT
  37. 16 * 15 // RESERVED
  38. ]
  39. function doParse () {
  40. var parser = mqtt.parser()
  41. parser.on('error', onError)
  42. parser.on('packet', onPacket)
  43. randomPacket = crypto.randomBytes(Math.floor(Math.random() * 512))
  44. // Increase probability to have a valid first byte in order to at least
  45. // enter the parser
  46. if (Math.random() > 0.2 && randomPacket.length > 0) randomPacket.writeUInt8(firstBytes[Math.floor(Math.random() * firstBytes.length)], 0)
  47. parser.parse(randomPacket)
  48. }
  49. try {
  50. console.log('Starting benchmark')
  51. for (i = 0; i < max; i++) {
  52. doParse()
  53. }
  54. } catch (e) {
  55. console.log('Exception occurred at packet')
  56. console.log(randomPacket)
  57. console.log(e.message)
  58. console.log(e.stack)
  59. }
  60. function onError () {
  61. errors++
  62. }
  63. function onPacket () {
  64. packets++
  65. }
  66. var delta = Math.abs(max - packets - errors)
  67. time = Date.now() / 1000 - start
  68. console.log('Benchmark complete')
  69. console.log('==========================')
  70. console.log('Sent packets:', max)
  71. console.log('Total time:', Math.round(time * 100) / 100, 'seconds', '\r\n')
  72. console.log('Valid packets:', packets)
  73. console.log('Erroneous packets:', errors)
  74. if ((max - packets - errors) < 0) console.log('Excess packets:', delta, '\r\n')
  75. else console.log('Missing packets:', delta, '\r\n')
  76. console.log('Total packets:', packets + errors)
  77. console.log('Total errors:', errors + delta)
  78. console.log('Error rate:', ((errors + delta) / max * 100).toFixed(2) + '%')
  79. console.log('==========================')