Last active
November 27, 2022 18:16
-
-
Save sgpinkus/feb531c41ac7cf64e4c2937d05da97ee to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// node --max-old-space-size=8192 test-_id-random-lookup.js | |
var fs = require('node:fs'); | |
var numDocs = 1e6; | |
async function main() { | |
var { MongoClient, ObjectId } = require('mongodb'); | |
var client = await MongoClient.connect('mongodb://localhost:27017/test'); | |
console.log('Connected'); | |
var db = client.db('testing'); | |
try { | |
await db.dropCollection('obj'); | |
} catch(e) { null; } | |
var coll = await db.collection('obj'); | |
var docs = []; | |
console.time('insert'); | |
for(let i = 0; i < numDocs; i++) { | |
// docs.push({ _id: new ObjectId(), i }); | |
docs.push({ _id: (new ObjectId()).toString(), i }); | |
} | |
await coll.insertMany(docs); | |
console.timeEnd('insert'); | |
console.log((await coll.countDocuments({}))); | |
fs.writeFileSync('docs.js', JSON.stringify(docs)); | |
} | |
main() | |
.then(() => process.exit()) | |
.catch((e) => { console.error(e); process.exit(); }); |
This file contains hidden or 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
// Run test-_id-random-lookup-insert-phase.js first. | |
var fs = require('node:fs'); | |
var numLookup = 1e4; | |
async function main() { | |
var { MongoClient, ObjectId } = require('mongodb'); | |
var client = await MongoClient.connect('mongodb://localhost:27017/test'); | |
console.log('Connected'); | |
var db = client.db('testing'); | |
var coll = await db.collection('obj'); | |
var docs = JSON.parse(fs.readFileSync('docs.js')); | |
var numDocs = docs.length; | |
console.time('lookup'); | |
for(let i = 0; i < numLookup; i++) { | |
// var doc = await coll.findOne({ _id: ObjectId(docs[randInt(numDocs)]._id) }); | |
var doc = await coll.findOne({ _id: docs[randInt(numDocs)]._id }); | |
// console.log(doc); | |
} | |
console.timeEnd('lookup'); | |
client.close(); | |
} | |
function randInt(numDocs) { | |
return Math.floor(Math.random()*numDocs); | |
} | |
main() | |
.then(() => process.exit()) | |
.catch((e) => { console.error(e); process.exit(); }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment