diff --git a/test/benchmarks/mongoBench/runner.js b/test/benchmarks/mongoBench/runner.js index a93a114562..541c9efb6c 100644 --- a/test/benchmarks/mongoBench/runner.js +++ b/test/benchmarks/mongoBench/runner.js @@ -9,33 +9,10 @@ function percentileIndex(percentile, total) { return Math.max(Math.floor((total * percentile) / 100 - 1), 0); } -function timeDoneTask(task, ctx) { - return new Promise((resolve, reject) => { - let called = false; - const start = performance.now(); - task.call(ctx, err => { - const end = performance.now(start); - if (called) return; - if (err) return reject(err); - return resolve((end - start) / 1000); - }); - }); -} - async function timeTask(task, ctx) { - // Some tasks are async, so they take callbacks. - if (task.length) { - return timeDoneTask(task, ctx); - } - const start = performance.now(); - const ret = task.call(ctx); - let end = performance.now(); - - if (ret && ret.then) { - await ret; - end = performance.now(); - } + await task.call(ctx); + const end = performance.now(); return (end - start) / 1000; } @@ -174,7 +151,7 @@ class Runner { async _loopTask(benchmark, ctx) { const start = performance.now(); const rawData = []; - const minExecutionCount = this.minExecutionCount; + const minExecutionCount = 1; const minExecutionTime = this.minExecutionTime; const maxExecutionTime = this.maxExecutionTime; let time = performance.now() - start; diff --git a/test/benchmarks/mongoBench/suites/multiBench.js b/test/benchmarks/mongoBench/suites/multiBench.js index d247d290d4..b1b2464f9c 100644 --- a/test/benchmarks/mongoBench/suites/multiBench.js +++ b/test/benchmarks/mongoBench/suites/multiBench.js @@ -1,3 +1,5 @@ +const { Readable } = require('stream'); +const { pipeline } = require('stream/promises'); const { loadSpecFile, makeLoadJSON, @@ -12,30 +14,24 @@ const { createCollection, dropCollection, dropBucket, - initBucket + initBucket, + writeSingleByteFileToBucket } = require('../../driverBench/common'); function loadGridFs() { this.bin = loadSpecFile(['single_and_multi_document', 'gridfs_large.bin']); } -function findManyAndEmptyCursor(done) { - return this.collection.find({}).forEach(() => {}, done); -} - -function docBulkInsert(done) { - return this.collection.insertMany(this.docs, { ordered: true }, done); -} - function gridFsInitUploadStream() { - this.stream = this.bucket.openUploadStream('gridfstest'); + this.uploadStream = this.bucket.openUploadStream('gridfstest'); } -function writeSingleByteToUploadStream() { - return new Promise((resolve, reject) => { - this.stream.write('\0', null, err => (err ? reject(err) : resolve())); - }); +async function gridFsUpload() { + const uploadData = Readable.from(this.bin); + const uploadStream = this.uploadStream; + await pipeline(uploadData, uploadStream); } + function makeMultiBench(suite) { return suite .benchmark('findManyAndEmptyCursor', benchmark => @@ -48,7 +44,12 @@ function makeMultiBench(suite) { .setup(dropDb) .setup(initCollection) .setup(makeLoadTweets(false)) - .task(findManyAndEmptyCursor) + .task(async function () { + // eslint-disable-next-line no-unused-vars + for await (const _ of this.collection.find({})) { + // do nothing + } + }) .teardown(dropDb) .teardown(disconnectClient) ) @@ -67,7 +68,9 @@ function makeMultiBench(suite) { .beforeTask(dropCollection) .beforeTask(createCollection) .beforeTask(initCollection) - .task(docBulkInsert) + .task(async function () { + await this.collection.insertMany(this.docs, { ordered: true }); + }) .teardown(dropDb) .teardown(disconnectClient) ) @@ -86,7 +89,9 @@ function makeMultiBench(suite) { .beforeTask(dropCollection) .beforeTask(createCollection) .beforeTask(initCollection) - .task(docBulkInsert) + .task(async function () { + await this.collection.insertMany(this.docs, { ordered: true }); + }) .teardown(dropDb) .teardown(disconnectClient) ) @@ -103,10 +108,8 @@ function makeMultiBench(suite) { .beforeTask(dropBucket) .beforeTask(initBucket) .beforeTask(gridFsInitUploadStream) - .beforeTask(writeSingleByteToUploadStream) - .task(function (done) { - this.stream.on('error', done).end(this.bin, null, () => done()); - }) + .beforeTask(writeSingleByteFileToBucket) + .task(gridFsUpload) .teardown(dropDb) .teardown(disconnectClient) ) @@ -123,21 +126,16 @@ function makeMultiBench(suite) { .setup(dropBucket) .setup(initBucket) .setup(gridFsInitUploadStream) - .setup(function () { - return new Promise((resolve, reject) => { - this.stream.end(this.bin, null, err => { - if (err) { - return reject(err); - } - - this.id = this.stream.id; - this.stream = undefined; - resolve(); - }); - }); + .setup(async function () { + await gridFsUpload.call(this); + this.id = this.uploadStream.id; + this.uploadData = undefined; }) - .task(function (done) { - this.bucket.openDownloadStream(this.id).resume().on('end', done); + .task(async function () { + // eslint-disable-next-line no-unused-vars + for await (const _ of this.bucket.openDownloadStream(this.id)) { + // do nothing + } }) .teardown(dropDb) .teardown(disconnectClient) diff --git a/test/benchmarks/mongoBench/suites/singleBench.js b/test/benchmarks/mongoBench/suites/singleBench.js index 60b9c30795..aef4fc2acd 100644 --- a/test/benchmarks/mongoBench/suites/singleBench.js +++ b/test/benchmarks/mongoBench/suites/singleBench.js @@ -11,45 +11,6 @@ const { makeLoadTweets } = require('../../driverBench/common'); -function makeTestInsertOne(numberOfOps) { - return function (done) { - const loop = _id => { - if (_id > numberOfOps) { - return done(); - } - - const doc = Object.assign({}, this.doc); - - this.collection.insertOne(doc, err => (err ? done(err) : loop(_id + 1))); - }; - - loop(1); - }; -} - -function findOneById(done) { - const loop = _id => { - if (_id > 10000) { - return done(); - } - - return this.collection.findOne({ _id }, err => (err ? done(err) : loop(_id + 1))); - }; - - return loop(1); -} - -function runCommand(done) { - const loop = _id => { - if (_id > 10000) { - return done(); - } - return this.db.command({ hello: true }, err => (err ? done(err) : loop(_id + 1))); - }; - - return loop(1); -} - function makeSingleBench(suite) { suite .benchmark('runCommand', benchmark => @@ -58,7 +19,11 @@ function makeSingleBench(suite) { .setup(makeClient) .setup(connectClient) .setup(initDb) - .task(runCommand) + .task(async function () { + for (let i = 0; i < 10000; ++i) { + await this.db.command({ hello: true }); + } + }) .teardown(disconnectClient) ) .benchmark('findOne', benchmark => @@ -71,7 +36,11 @@ function makeSingleBench(suite) { .setup(dropDb) .setup(initCollection) .setup(makeLoadTweets(true)) - .task(findOneById) + .task(async function () { + for (let _id = 0; _id < 10000; ++_id) { + await this.collection.findOne({ _id }); + } + }) .teardown(dropDb) .teardown(disconnectClient) ) @@ -89,7 +58,12 @@ function makeSingleBench(suite) { .beforeTask(dropCollection) .beforeTask(createCollection) .beforeTask(initCollection) - .task(makeTestInsertOne(10000)) + .task(async function () { + for (let i = 0; i < 10000; ++i) { + const doc = Object.assign({}, this.doc); + await this.collection.insertOne(doc); + } + }) .teardown(dropDb) .teardown(disconnectClient) ) @@ -107,7 +81,12 @@ function makeSingleBench(suite) { .beforeTask(dropCollection) .beforeTask(createCollection) .beforeTask(initCollection) - .task(makeTestInsertOne(10)) + .task(async function () { + for (let i = 0; i < 10; ++i) { + const doc = Object.assign({}, this.doc); + await this.collection.insertOne(doc); + } + }) .teardown(dropDb) .teardown(disconnectClient) );