Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add randomized $near tests SERVER-1342
- Loading branch information
1 parent
0f7906c
commit f107692
Showing
6 changed files
with
154 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// this tests all points using $near | ||
load("jstests/libs/geo_near_random.js"); | ||
|
||
var test = new GeoNearRandomTest("geo_near_random1"); | ||
|
||
test.insertPts(100); | ||
|
||
test.testPt([0,0]); | ||
test.testPt(test.mkPt()); | ||
test.testPt(test.mkPt()); | ||
test.testPt(test.mkPt()); | ||
test.testPt(test.mkPt()); |
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,21 @@ | ||
// this tests 1% of all points using $near and $nearSphere | ||
load("jstests/libs/geo_near_random.js"); | ||
|
||
var test = new GeoNearRandomTest("geo_near_random2"); | ||
|
||
test.insertPts(5000); | ||
|
||
opts = {sphere:0, nToTest:test.nPts*0.01}; | ||
test.testPt([0,0], opts); | ||
test.testPt(test.mkPt(), opts); | ||
test.testPt(test.mkPt(), opts); | ||
test.testPt(test.mkPt(), opts); | ||
test.testPt(test.mkPt(), opts); | ||
|
||
opts.sphere = 1 | ||
test.testPt([0,0], opts); | ||
test.testPt(test.mkPt(0.8), opts); | ||
test.testPt(test.mkPt(0.8), opts); | ||
test.testPt(test.mkPt(0.8), opts); | ||
test.testPt(test.mkPt(0.8), opts); | ||
|
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,76 @@ | ||
GeoNearRandomTest = function(name) { | ||
this.name = name; | ||
this.t = db[name]; | ||
this.nPts = 0; | ||
|
||
// reset state | ||
this.t.drop(); | ||
Random.srand(1234); | ||
|
||
print("starting test: " + name); | ||
} | ||
|
||
|
||
GeoNearRandomTest.prototype.mkPt = function mkPt(scale){ | ||
scale = scale || 1; // scale is good for staying away from edges | ||
return [((Random.rand() * 360) - 180) * scale, ((Random.rand() * 180) - 90) * scale]; | ||
} | ||
|
||
GeoNearRandomTest.prototype.insertPts = function(nPts) { | ||
assert.eq(this.nPts, 0, "insertPoints already called"); | ||
this.nPts = nPts; | ||
|
||
for (var i=0; i<nPts; i++){ | ||
this.t.insert({_id: i, loc: this.mkPt()}); | ||
} | ||
|
||
this.t.ensureIndex({loc: '2d'}); | ||
} | ||
|
||
GeoNearRandomTest.prototype.assertIsPrefix = function(short, long) { | ||
for (var i=0; i < short.length; i++){ | ||
assert.eq(short[i], long[i]); | ||
} | ||
} | ||
|
||
GeoNearRandomTest.prototype.testPt = function(pt, opts) { | ||
assert.neq(this.nPts, 0, "insertPoints not yet called"); | ||
|
||
opts = opts || {}; | ||
opts['sphere'] = opts['sphere'] || 0; | ||
opts['nToTest'] = opts['nToTest'] || this.nPts; // be careful, test is O( N^2 ) | ||
|
||
print("testing point: " + tojson(pt) + " opts: " + tojson(opts)); | ||
|
||
|
||
var cmd = {geoNear:this.t.getName(), near: pt, num: 1, spherical:opts.sphere}; | ||
|
||
var last = db.runCommand(cmd).results; | ||
for (var i=2; i <= opts.nToTest; i++){ | ||
//print(i); // uncomment to watch status | ||
cmd.num = i | ||
var ret = db.runCommand(cmd).results; | ||
|
||
try { | ||
this.assertIsPrefix(last, ret); | ||
} catch (e) { | ||
print("*** failed while compairing " + (i-1) + " and " + i); | ||
printjson(cmd); | ||
throw e; // rethrow | ||
} | ||
|
||
last = ret; | ||
} | ||
|
||
|
||
last = last.map(function(x){return x.obj}); | ||
|
||
var query = {loc:{}}; | ||
query.loc[ opts.sphere ? '$nearSphere' : '$near' ] = pt; | ||
var near = this.t.find(query).limit(opts.nToTest).toArray(); | ||
|
||
this.assertIsPrefix(last, near); | ||
assert.eq(last, near); | ||
} | ||
|
||
|
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,11 @@ | ||
var t = db.bench.geo_near1; | ||
t.drop() | ||
|
||
var numPts = 1000*1000; | ||
|
||
|
||
for (var i=0; i < numPts; i++){ | ||
x = (Math.random() * 100) - 50; | ||
y = (Math.random() * 100) - 50; | ||
t.insert({loc: [x,y], i: i}); | ||
} |
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,13 @@ | ||
// this tests all points using $near | ||
load("jstests/libs/geo_near_random.js"); | ||
|
||
var test = new GeoNearRandomTest("nightly.geo_near_random1"); | ||
|
||
test.insertPts(1000); | ||
|
||
test.testPt([0,0]); | ||
test.testPt(test.mkPt()); | ||
test.testPt(test.mkPt()); | ||
test.testPt(test.mkPt()); | ||
test.testPt(test.mkPt()); | ||
|
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,21 @@ | ||
// this tests 1% of all points using $near and $nearSphere | ||
load("jstests/libs/geo_near_random.js"); | ||
|
||
var test = new GeoNearRandomTest("nightly.geo_near_random2"); | ||
|
||
test.insertPts(100000); | ||
|
||
opts = {sphere:0, nToTest:test.nPts*0.01}; | ||
test.testPt([0,0], opts); | ||
test.testPt(test.mkPt(), opts); | ||
test.testPt(test.mkPt(), opts); | ||
test.testPt(test.mkPt(), opts); | ||
test.testPt(test.mkPt(), opts); | ||
|
||
opts.sphere = 1 | ||
test.testPt([0,0], opts); | ||
test.testPt(test.mkPt(0.8), opts); | ||
test.testPt(test.mkPt(0.8), opts); | ||
test.testPt(test.mkPt(0.8), opts); | ||
test.testPt(test.mkPt(0.8), opts); | ||
|