Skip to content

Commit

Permalink
issue #39
Browse files Browse the repository at this point in the history
  • Loading branch information
zaggino committed Jun 15, 2014
1 parent 5df534c commit ac4ed8c
Show file tree
Hide file tree
Showing 2 changed files with 116 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@
"grunt-simple-mocha": "latest",
"chai": "latest",
"mocha": "latest",
"uglify-js": "latest"
"uglify-js": "latest",
"mongodb": "latest"
},
"bugs": {
"url": "https://github.com/zaggino/z-schema/issues",
Expand Down
114 changes: 114 additions & 0 deletions test/issue_39.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*jshint strict:false, loopfunc:true*/
/*global describe, before, it*/

var ObjectID = require("mongodb").ObjectID;
var ZSchema = require("../src/ZSchema");
var assert = require("chai").assert;

function hrtoms(t) {
return (t[0] * 1e9 + t[1]) / 1e6;
}

describe("https://github.com/zaggino/z-schema/issues/39", function() {
var schema = {
type: "array",
items: {
type: "object",
properties: {
"_id": { type: "object" },
"note": { type: "string", maxLength: 2048 },
"createdBy": { type: "string", maxLength: 100 },
"anotherProperty": { type: "object" }
},
additionalProperties: false,
},
additionalItems: false,
};

var notes = [];
var notesWithoutObjectId = [];

before(function() {
// compile schema before using
var validator = new ZSchema({ sync: true });
validator.compileSchema(schema);

// fill the arrays
var num = 10000, i;

for(i = 0; i < num; i++) {
notes.push({
_id: new ObjectID(),
note: "Here is my note",
createdBy: "test@whatever.com",
});
}

for(i = 0; i < num; i++) {
notesWithoutObjectId.push({
note: "Here is my note",
createdBy: "test@whatever.com",
anotherProperty: {
"key": "something so that we have 3 properties here too"
}
});
}
});

it("should be fast sync", function(done) {
this.timeout(30000);

var validator = new ZSchema({ sync: true });

var s1 = process.hrtime();
var valid = validator.validate(notes, schema);
var e1 = process.hrtime(s1);
var duration1 = hrtoms(e1);

var s2 = process.hrtime();
valid = validator.validate(notesWithoutObjectId, schema);
var e2 = process.hrtime(s2);
var duration2 = hrtoms(e2);

assert.isTrue(duration1 * 2 > duration2);
assert.isTrue(duration1 < duration2 * 2);

/*
console.log(duration1);
console.log(duration2);
*/

done();
});

it("should be fast async", function(done) {
this.timeout(30000);

var validator = new ZSchema({ async: true, noTypeless: true, noExtraKeywords: true, noZeroLengthStrings: true, forceAdditional: true});

var s1 = process.hrtime();
validator.validate(notes, schema, function (err, report) {
var e1 = process.hrtime(s1);
var duration1 = hrtoms(e1);
assert.isTrue(report.valid);

var s2 = process.hrtime();
validator.validate(notesWithoutObjectId, schema, function (err, report) {
var e2 = process.hrtime(s2);
var duration2 = hrtoms(e2);
assert.isTrue(report.valid);

/*
console.log(duration1);
console.log(duration2);
*/

assert.isTrue(duration1 * 2 > duration2);
assert.isTrue(duration1 < duration2 * 2);

done();
});

});
});
});

3 comments on commit ac4ed8c

@bzuillsmith
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your test worked fine for me. I tried updating my mongodb library from 1.4.0 to 1.4.6 and my test runs quickly now. The odd part is, if I downgrade your example's mongodb library to 1.4.0, it still runs fine. So something still seems inconsistent. Still playing around with it to see if I can isolate the problem some more.

@bzuillsmith
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently z-schema@2.4.3 is the version I've been running this whole time X.X

Updating to 2.4.8 has resolved the issue. Not sure what the underlying issue was, but seems a combination of an old mongodb and old z-schema library was running extremely slow.

Simply updating our project dependencies is the solution. Sorry about that.

@zaggino
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, I actually fixed a bunch of bugs yesterday for version 2.4.8 so I might have fixed the problem without meaning to. Since now we have a test-case for this, we can be sure this won't happen again in the future.

Please sign in to comment.