Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Features/time based yield #14

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
.vscode
package-lock.json
103 changes: 62 additions & 41 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,18 +40,16 @@ let validateSpace = (space) => {
};

/**
* Checks whether the provided intensity
* @param { number } intensity
* Checks whether the provided maxDuration
* @param { number } maxDuration
* @return { number }
*/
let validateIntensity = (intensity) => {
intensity = Math.round(intensity);
if (intensity > 0 && intensity <= 32)
return intensity;
else if (intensity <= 0)
return 1;
const validateDuration = (maxDuration) => {
maxDuration = Math.round(maxDuration);
if (maxDuration > 0 && maxDuration <= 60*60*1000)
return maxDuration;
else
return 32;
return 15;
};

module.exports = {
Expand All @@ -60,59 +58,82 @@ module.exports = {
* Error checking and call of appropriate functions for JSON parse
* @param { primitive data types } data
* @param { function or array } reviver
* @param { number } intensity
* @param { number } maxDuration
* @param { function } callback
* @return { function } parseWrapper
*/
parseAsync(data, reviver, intensity, callback) {
parseAsync(data, reviver, maxDuration, callback) {
const argv = arguments;
if (argv.length < 2)
throw new Error('Missing Callback');

if (typeof argv[argv.length - 1] === 'function') {
// Make sure last argument is callback
if (typeof argv[argv.length - 1] === 'function' && argv.length > 1) {
callback = argv[argv.length - 1];
reviver = null;
intensity = 1;
} else
} else {
throw new TypeError('Callback is not a function');
}

if (argv.length > 2) {
let i = 1;
if (typeof argv[i] === 'function')
reviver = argv[i++];
if (typeof argv[i] === 'number')
intensity = validateIntensity(argv[i]);
if (argv.length > 2) { // more than just just data and callback
let i = argv.length - 2;
if (typeof argv[i] === 'number') {
maxDuration = validateDuration(argv[i]);
i--;
} else {
maxDuration = undefined;
}
if (i > 0 && (typeof argv[i] === 'function' || Array.isArray(argv[i]))) {
reviver = argv[i];
} else {
reviver = undefined;
}
} else {
reviver = undefined;
maxDuration = 15;
}
return pa.parseWrapper(data, reviver, intensity, callback);
return pa.parseWrapper(data, reviver, maxDuration, callback);
},

/**
* Error checking and call of appropriate functions for JSON stringify API
* @param { primitive data types } data
* @param { function or array } replacer
* @param { number or string } space
* @param { number } intensity
* @param { number } maxDuration
* @param { function } callback
* @return { function } stringifyWrapper
*/
stringifyAsync(data, replacer, space, intensity, callback) {
stringifyAsync(data, replacer, space, maxDuration, callback) {
const argv = arguments;
if (typeof argv[argv.length - 1] === 'function') {

// Make sure last argument is callback
if (typeof argv[argv.length - 1] === 'function' && argv.length > 1) {
callback = argv[argv.length - 1];
replacer = null;
intensity = 1;
} else
} else {
throw new TypeError('Callback is not a function');
if (argv.length > 2) {
let i = 1;
if (typeof argv[i] === 'function' || typeof argv[i] === 'object')
replacer = argv[i++];
if ((typeof argv[i] === 'number' || typeof argv[i] === 'string') &&
typeof argv[i++] === 'number')
space = validateSpace(argv[i++]);
if (typeof argv[i] === 'number')
intensity = validateIntensity(argv[i]);
}
return ps.stringifyWrapper(data, replacer, space, intensity, callback);

if (argv.length > 2) { // more than just just data and callback
let i = argv.length - 2;
if (typeof argv[i] === 'number') {
maxDuration = validateDuration(argv[i]);
i--;
} else {
maxDuration = undefined;
}
if (i > 0 && (typeof argv[i] === 'number' || typeof argv[i] === 'string')) {
space = validateSpace(argv[i]);
i--;
} else {
space = undefined;
}
if (i > 0 && (typeof argv[i] === 'function' || Array.isArray(argv[i]))) {
replacer = argv[i];
} else {
replacer = undefined;
}
} else {
replacer = undefined;
maxDuration = 15;
}

return ps.stringifyWrapper(data, replacer, space, maxDuration, callback);
},
};
Empty file added package-lock.json
Empty file.
7 changes: 3 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"test": "tap test/*.js"
},
"engines": {
"node": ">= 4.0.0"
"node": ">= 10.0.0"
},
"files": [
"LICENSE",
Expand All @@ -42,11 +42,10 @@
"test/"
],
"devDependencies": {
"blocked-at": "^1.1.3",
"eslint": "^3.19.0",
"eslint-config-strongloop": "^2.1.0",
"seedrandom": "^2.4.3",
"tap": "^10.7.3"
},
"dependencies": {
"seedrandom": "^2.4.3"
}
}
79 changes: 79 additions & 0 deletions test/test-parse-big.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
'use strict';

const yj = require('../index');
const tap = require('tap');

// 1. Set up really big JSON object
const RANDOM_STRING = 'abcdefghijklmnopqrstuvwxCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-=_+[]{}\\|;:\'",<.>/?`~';
const stringLength = Math.pow(2, 4);
const bigString = new Array(stringLength).fill(RANDOM_STRING).join('');
const arrayLength = Math.pow(2, 8);
const bigArray = new Array(arrayLength).fill(bigString);
let index = 0;
const objectSize = Math.pow(2, 10);
const bigObject = new Array(objectSize).fill(undefined).reduce((accumulator) => {
accumulator[index] = bigArray;
index++;
return accumulator;
}, {});
const bigJSON = JSON.stringify(bigObject);
// console.log('Object size', bigJSON.length * 16, 'bytes');

// 2. Measure how long it takes regular JSON.parse
const nonYieldParseTime0 = Date.now();
JSON.parse(bigJSON);
const nonYieldParseTime1 = Date.now();
const nonYieldParseElapsedTime = nonYieldParseTime1 - nonYieldParseTime0;

// 3. Set up a measure of how long a single tick blocks for. Uses async-hooks
const blocked = require('blocked-at');
let longestTickTime = 0;
let longestTickStack;
blocked((blockTime, stack) => {
blockTime = parseInt(blockTime, 10); // eslint-disable-line no-param-reassign
if (blockTime > longestTickTime) {
longestTickTime = blockTime;
longestTickStack = stack;
}
});

// 4. Set up a measure of how often the node loop runs and how long between runs (max)
let longestLoopStarvation = 0;
let yieldCount = 0;
let time0 = Date.now();
const interval = setInterval(() => {
yieldCount++;
const time1 = Date.now();
const elapsedTime = time1 - time0;
time0 = time1;
if (elapsedTime > longestLoopStarvation) {
longestLoopStarvation = elapsedTime;
}
}, 0);

const yieldParseTime0 = Date.now();
yj.parseAsync(bigJSON, (error, obj) => {
// 5. Measure how long yj.parseAsync takes
const yieldParseTime1 = Date.now();
const yieldParseElapsedTime = yieldParseTime1 - yieldParseTime0;
// console.log('Total time', yieldParseElapsedTime);
// console.log('yieldCount', yieldCount);
// console.log('longestLoopStarvation', longestLoopStarvation);
// console.log('longestTickTime', longestTickTime);

if (error) {
tap.fail(error);
} else {
tap.equal(yieldParseElapsedTime / 10 < nonYieldParseElapsedTime, true, `Yieldable parse took 10x longer than regular parse ${yieldParseElapsedTime} vs ${nonYieldParseElapsedTime}`);
tap.equal(yieldCount > 10, true, `Not enough yielding ${yieldCount}`);
tap.equal(longestLoopStarvation < 100, true, `Loop starved too long ${longestLoopStarvation}`);
setTimeout(() => { // need 2 loops for blocked-at to call-back for some reason...
setTimeout(() => {
tap.equal(longestTickTime < 100, true, `Tick took too long ${longestTickTime}`);
// Will print what took that lon
// console.log(longestTickStack);
});
});
}
clearInterval(interval);
});
33 changes: 0 additions & 33 deletions test/test-parse-intensity.js

This file was deleted.

35 changes: 35 additions & 0 deletions test/test-parse-maxDuration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use strict';

const yj = require('../index');
const tap = require('tap');

var count = 0;
var i = 0;
var arr = [];
while (i < Math.pow(2, 20)) {
arr.push(i++);
}
var str = JSON.stringify(arr);
let timeout;

yj.parseAsync(str, 100, (err, obj) => {
clearTimeout(timeout);
if (!err) {
tap.ok(count >= 1, `Async function was expected to yield at least once, but got ${count}!`);
} else
tap.fail(err);
});

const foo = () => {
return setTimeout(() => {
count++;
if (count > 10){
clearTimeout(timeout);
return;
} else {
foo();
}
}, 0);
};

timeout = foo();
2 changes: 1 addition & 1 deletion test/test-parse-params.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var reviver = (key, value) => {

// Make sure all the parameters can co-exist and the API
// can handle them gracefully.
yj.parseAsync(str, reviver, 2, (err, obj) => {
yj.parseAsync(str, reviver, 100, (err, obj) => {
if (!err) {
tap.ok(flag, 'Unexpected Async-Behavior:' +
' callback was called synchronously');
Expand Down
32 changes: 0 additions & 32 deletions test/test-stringify-intensity.js

This file was deleted.

34 changes: 34 additions & 0 deletions test/test-stringify-maxDuration.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'use strict';

const yj = require('../index');
const tap = require('tap');

var count = 0;
var i = 0;
var arr = [];
while (i < Math.pow(2, 20)) {
arr.push(i++);
}
let timeout;

yj.stringifyAsync(arr, 1, 10, (err, str) => {
clearTimeout(timeout);
if (!err) {
tap.ok(count >= 1, `Async function was expected to yield at least once, but got ${count}!`);
} else
tap.fail(err);
});

const foo = () => {
return setTimeout(() => {
count++;
if (count > 10){
clearTimeout(timeout);
return;
} else {
foo();
}
}, 0);
};

timeout = foo();
Loading