Skip to content

Commit

Permalink
Update stress test
Browse files Browse the repository at this point in the history
Change tsconfig compile target to es2017
	This improves performance greatly in node v10+ due to native async/await improvements
  • Loading branch information
maritz committed May 30, 2018
1 parent 866bf21 commit 5619747
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 29 deletions.
77 changes: 62 additions & 15 deletions extra/stress.js
Expand Up @@ -3,31 +3,69 @@ var nohm = require(__dirname + '/../').Nohm,

const redisClient = require('redis').createClient();

redisClient.once('connect', function () {
console.log(`Starting stress test - saving and then updating ${iterations} models in parallel.`);
redisClient.once('connect', async () => {
console.log(
`Starting stress test - saving and then updating ${iterations} models in parallel.`,
);

nohm.setPrefix('stress');
nohm.setClient();
nohm.setPrefix('nohm-stress-test');
nohm.setClient(redisClient);

var models = require(__dirname + '/models');
var UserModel = models.user;

try {
await nohm.purgeDb();
} catch (err) {
console.error('Failed to purge DB before starting.', err);
process.exit(1);
}

var counter = 0;
var start = Date.now();
var startUpdates = 0;
var users = [];

var callback = function () {
var callback = function() {
counter++;
if (counter >= iterations) {
const updateTime = Date.now() - startUpdates;
const timePerUpdate = iterations / updateTime * 1000;
console.log(`${updateTime}ms for ${counter} parallel User updates, ${timePerUpdate.toFixed(2)} updates/second`);
console.log(
`${updateTime}ms for ${counter} parallel User updates, ${timePerUpdate.toFixed(
2,
)} updates/second`,
);
console.log(`Total time: ${Date.now() - start}ms`);
console.log('Memory usage after', process.memoryUsage());
console.log('Done.');
redisClient.quit();
process.exit();
redisClient.SCARD(
`${nohm.prefix.idsets}${new UserModel().modelName}`,
async (err, numUsers) => {
if (err) {
console.error(
'Error while trying to check number of saved users.',
err,
);
process.exit(1);
}
if (numUsers !== iterations) {
console.error(
`Number of users is wrong. ${numUsers} !== ${iterations}`,
`${nohm.prefix.idsets}${UserModel.modelName}`,
);
process.exit(1);
}
try {
await nohm.purgeDb();
} catch (err) {
console.error('Failed to purge DB during cleanup.', err);
process.exit(1);
}
console.log('Done.');
redisClient.quit();
process.exit();
},
);
}
};

Expand All @@ -41,12 +79,15 @@ redisClient.once('connect', function () {
console.log('Saves done, starting updates');
counter = 0;
for (var i = 0, len = users.length; i < len; i++) {
users[i].property({ name: 'Bob' + i, key: i + i })
users[i].save().then(callback).catch(errorCallback);
users[i].property({ name: 'Bob' + i, key: i + i });
users[i]
.save()
.then(callback)
.catch(errorCallback);
}
}

var saveCallback = function (err) {
var saveCallback = function(err) {
if (err) {
console.log('error: ' + err);
process.exit();
Expand All @@ -56,16 +97,22 @@ redisClient.once('connect', function () {
saveCallback = null;
const saveTime = Date.now() - start;
const timePerSave = iterations / saveTime * 1000;
console.log(`${saveTime}ms for ${counter} parallel User saves, ${timePerSave.toFixed(2)} saves/second`);
console.log(
`${saveTime}ms for ${counter} parallel User saves, ${timePerSave.toFixed(
2,
)} saves/second`,
);
update();
}
};

for (var i = 0; i < iterations; i++) {
var user = new UserModel();
user.property({ name: 'Bob', key: i });
user.save().then(saveCallback).catch(errorCallback);
user
.save()
.then(saveCallback)
.catch(errorCallback);
users.push(user);
}

});
2 changes: 1 addition & 1 deletion ts/model.ts
Expand Up @@ -594,7 +594,7 @@ abstract class NohmModel<TProps extends IDictionary = IDictionary> {
}
let numIdExisting: number = 0;
if (action !== 'create') {
numIdExisting = numIdExisting = await SISMEMBER(
numIdExisting = await SISMEMBER(
this.client,
this.prefix('idsets'),
this.id,
Expand Down
18 changes: 5 additions & 13 deletions tsconfig.json
@@ -1,6 +1,6 @@
{
"compilerOptions": {
"target": "es6",
"target": "es2017",
"module": "commonjs",
"declaration": true,
"experimentalDecorators": true,
Expand All @@ -11,19 +11,11 @@
"noUnusedLocals": true,
"strictNullChecks": true,
"noUnusedParameters": true,
"lib": [
"es7"
],
"lib": ["es7"],
"outDir": "./tsOut",
"sourceMap": true
},
"files": [
"./ts/index.ts"
],
"include": [
"./ts/tests.ts"
],
"exclude": [
"./lib/*.js"
]
"files": ["./ts/index.ts"],
"include": ["./ts/tests.ts"],
"exclude": ["./lib/*.js"]
}

0 comments on commit 5619747

Please sign in to comment.