[Pkg-javascript-commits] [node-jsonstream] 79/214: test memory out of bounds
Bastien Roucariès
rouca at moszumanska.debian.org
Fri Dec 1 12:58:42 UTC 2017
This is an automated email from the git hooks/post-receive script.
rouca pushed a commit to branch master
in repository node-jsonstream.
commit 8845b9c1c41b44b813ae845c608e22f80416ae65
Author: Dominic Tarr <dominic.tarr at gmail.com>
Date: Sun Mar 17 18:25:01 2013 +1300
test memory out of bounds
---
test/gen.js | 130 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 130 insertions(+)
diff --git a/test/gen.js b/test/gen.js
new file mode 100644
index 0000000..82387af
--- /dev/null
+++ b/test/gen.js
@@ -0,0 +1,130 @@
+var fs = require('fs')
+var JSONStream = require('../')
+var file = '/tmp/JSONStream-test-large.json'
+
+if(process.title != 'browser')
+require('tape')('out of mem', function (t) {
+
+//////////////////////////////////////////////////////
+// Produces a random number between arg1 and arg2
+//////////////////////////////////////////////////////
+var randomNumber = function (min, max) {
+ var number = Math.floor(Math.random() * (max - min + 1) + min);
+ return number;
+};
+
+//////////////////////////////////////////////////////
+// Produces a random string of a length between arg1 and arg2
+//////////////////////////////////////////////////////
+var randomString = function (min, max) {
+
+ // add several spaces to increase chanses of creating 'words'
+ var chars = ' 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
+ var result = '';
+
+ var randomLength = randomNumber(min, max);
+
+ for (var i = randomLength; i > 0; --i) {
+ result += chars[Math.round(Math.random() * (chars.length - 1))];
+ }
+ return result;
+};
+
+//////////////////////////////////////////////////////
+// Produces a random JSON document, as a string
+//////////////////////////////////////////////////////
+var randomJsonDoc = function () {
+
+ var doc = {
+ "CrashOccurenceID": randomNumber(10000, 50000),
+ "CrashID": randomNumber(1000, 10000),
+ "SiteName": randomString(10, 25),
+ "MachineName": randomString(10, 25),
+ "Date": randomString(26, 26),
+ "ProcessDuration": randomString(18, 18),
+ "ThreadIdentityName": null,
+ "WindowsIdentityName": randomString(15, 40),
+ "OperatingSystemName": randomString(35, 65),
+ "DetailedExceptionInformation": randomString(100, 800)
+ };
+
+ doc = JSON.stringify(doc);
+ doc = doc.replace(/\,/g, ',\n'); // add new lines after each attribute
+ return doc;
+};
+
+//////////////////////////////////////////////////////
+// generates test data
+//////////////////////////////////////////////////////
+var generateTestData = function (cb) {
+
+ console.log('generating large data file...');
+
+ var stream = fs.createWriteStream(file, {
+ encoding: 'utf8'
+ });
+
+ var i = 0;
+ var max = 100000;
+ var writing = false
+ var split = ',\n';
+ var doc = randomJsonDoc();
+ stream.write('[');
+
+ function write () {
+ if(writing) return
+ writing = true
+ while(++i < max) {
+ if(!stream.write(doc + split)) {
+ writing = false
+ return stream.once('drain', write)
+ }
+ }
+ stream.write(doc + ']')
+ stream.end();
+ console.log('END')
+ }
+ write()
+ stream.on('close', cb)
+};
+
+//////////////////////////////////////////////////////
+// Shows that parsing 100000 instances using JSONStream fails
+//
+// After several seconds, you will get this crash
+// FATAL ERROR: JS Allocation failed - process out of memory
+//////////////////////////////////////////////////////
+var testJSONStreamParse_causesOutOfMem = function (done) {
+ var items = 0
+ console.log('parsing data files using JSONStream...');
+
+ var parser = JSONStream.parse([true]);
+ var stream = fs.createReadStream(file);
+ stream = stream.pipe(parser);
+
+ stream.on('data', function (data) {
+ items ++
+ if(Math.random() < 0.001) console.log(items, '...')
+
+ });
+
+ stream.on('end', function () {
+ t.equal(items, 100000)
+ t.end()
+ });
+
+};
+
+//////////////////////////////////////////////////////
+// main
+//////////////////////////////////////////////////////
+
+fs.stat(file, function (err, stat) {
+ console.log(stat)
+ if(err || stat.size !== 86000000)
+ generateTestData(testJSONStreamParse_causesOutOfMem);
+ else
+ testJSONStreamParse_causesOutOfMem()
+})
+
+})
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/node-jsonstream.git
More information about the Pkg-javascript-commits
mailing list