Skip to content

Commit

Permalink
refactor: use promise based apis instead of callback apis in benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
baileympearson committed Sep 8, 2022
1 parent 5676f81 commit 16efe60
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 103 deletions.
27 changes: 2 additions & 25 deletions test/benchmarks/mongoBench/runner.js
Expand Up @@ -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;
}
Expand Down
68 changes: 33 additions & 35 deletions test/benchmarks/mongoBench/suites/multiBench.js
@@ -1,3 +1,5 @@
const { Readable } = require('stream');
const { pipeline } = require('stream/promises');
const {
loadSpecFile,
makeLoadJSON,
Expand All @@ -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 =>
Expand All @@ -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)
)
Expand All @@ -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)
)
Expand All @@ -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)
)
Expand All @@ -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)
)
Expand All @@ -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)
Expand Down
65 changes: 22 additions & 43 deletions test/benchmarks/mongoBench/suites/singleBench.js
Expand Up @@ -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 =>
Expand All @@ -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 =>
Expand All @@ -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)
)
Expand All @@ -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)
)
Expand All @@ -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)
);
Expand Down

0 comments on commit 16efe60

Please sign in to comment.