gen.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. return // dont run this test for now since tape is weird and broken on 0.10
  2. var fs = require('fs')
  3. var JSONStream = require('../')
  4. var file = process.argv[2] || '/tmp/JSONStream-test-large.json'
  5. var size = Number(process.argv[3] || 100000)
  6. var tape = require('tape')
  7. // if (process.title !== 'browser') {
  8. tape('out of mem', function (t) {
  9. t.plan(1)
  10. //////////////////////////////////////////////////////
  11. // Produces a random number between arg1 and arg2
  12. //////////////////////////////////////////////////////
  13. var randomNumber = function (min, max) {
  14. var number = Math.floor(Math.random() * (max - min + 1) + min);
  15. return number;
  16. };
  17. //////////////////////////////////////////////////////
  18. // Produces a random string of a length between arg1 and arg2
  19. //////////////////////////////////////////////////////
  20. var randomString = function (min, max) {
  21. // add several spaces to increase chanses of creating 'words'
  22. var chars = ' 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  23. var result = '';
  24. var randomLength = randomNumber(min, max);
  25. for (var i = randomLength; i > 0; --i) {
  26. result += chars[Math.round(Math.random() * (chars.length - 1))];
  27. }
  28. return result;
  29. };
  30. //////////////////////////////////////////////////////
  31. // Produces a random JSON document, as a string
  32. //////////////////////////////////////////////////////
  33. var randomJsonDoc = function () {
  34. var doc = {
  35. "CrashOccurenceID": randomNumber(10000, 50000),
  36. "CrashID": randomNumber(1000, 10000),
  37. "SiteName": randomString(10, 25),
  38. "MachineName": randomString(10, 25),
  39. "Date": randomString(26, 26),
  40. "ProcessDuration": randomString(18, 18),
  41. "ThreadIdentityName": null,
  42. "WindowsIdentityName": randomString(15, 40),
  43. "OperatingSystemName": randomString(35, 65),
  44. "DetailedExceptionInformation": randomString(100, 800)
  45. };
  46. doc = JSON.stringify(doc);
  47. doc = doc.replace(/\,/g, ',\n'); // add new lines after each attribute
  48. return doc;
  49. };
  50. //////////////////////////////////////////////////////
  51. // generates test data
  52. //////////////////////////////////////////////////////
  53. var generateTestData = function (cb) {
  54. console.log('generating large data file...');
  55. var stream = fs.createWriteStream(file, {
  56. encoding: 'utf8'
  57. });
  58. var i = 0;
  59. var max = size;
  60. var writing = false
  61. var split = ',\n';
  62. var doc = randomJsonDoc();
  63. stream.write('[');
  64. function write () {
  65. if(writing) return
  66. writing = true
  67. while(++i < max) {
  68. if(Math.random() < 0.001)
  69. console.log('generate..', i + ' / ' + size)
  70. if(!stream.write(doc + split)) {
  71. writing = false
  72. return stream.once('drain', write)
  73. }
  74. }
  75. stream.write(doc + ']')
  76. stream.end();
  77. console.log('END')
  78. }
  79. write()
  80. stream.on('close', cb)
  81. };
  82. //////////////////////////////////////////////////////
  83. // Shows that parsing 100000 instances using JSONStream fails
  84. //
  85. // After several seconds, you will get this crash
  86. // FATAL ERROR: JS Allocation failed - process out of memory
  87. //////////////////////////////////////////////////////
  88. var testJSONStreamParse_causesOutOfMem = function (done) {
  89. var items = 0
  90. console.log('parsing data files using JSONStream...');
  91. var parser = JSONStream.parse([true]);
  92. var stream = fs.createReadStream(file);
  93. stream.pipe(parser);
  94. parser.on('data', function (data) {
  95. items++
  96. if(Math.random() < 0.01) console.log(items, '...')
  97. });
  98. parser.on('end', function () {
  99. t.equal(items, size)
  100. });
  101. };
  102. //////////////////////////////////////////////////////
  103. // main
  104. //////////////////////////////////////////////////////
  105. fs.stat(file, function (err, stat) {
  106. console.log(stat)
  107. if(err)
  108. generateTestData(testJSONStreamParse_causesOutOfMem);
  109. else
  110. testJSONStreamParse_causesOutOfMem()
  111. })
  112. })
  113. // }