-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
|
||
}); | ||
}); | ||
}); |
ac4ed8c
There was a problem hiding this comment.
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.
ac4ed8c
There was a problem hiding this comment.
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.XUpdating 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.
ac4ed8c
There was a problem hiding this comment.
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.