Skip to content

Commit

Permalink
Add randomized $near tests SERVER-1342
Browse files Browse the repository at this point in the history
  • Loading branch information
RedBeard0531 committed Sep 3, 2010
1 parent 0f7906c commit f107692
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 0 deletions.
12 changes: 12 additions & 0 deletions jstests/geo_near_random1.js
@@ -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());
21 changes: 21 additions & 0 deletions jstests/geo_near_random2.js
@@ -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);

76 changes: 76 additions & 0 deletions jstests/libs/geo_near_random.js
@@ -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);
}


11 changes: 11 additions & 0 deletions jstests/perf/geo_near1.js
@@ -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});
}
13 changes: 13 additions & 0 deletions jstests/slowNightly/geo_near_random1.js
@@ -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());

21 changes: 21 additions & 0 deletions jstests/slowNightly/geo_near_random2.js
@@ -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);

0 comments on commit f107692

Please sign in to comment.