mongo icon indicating copy to clipboard operation
mongo copied to clipboard

[Performance] performance difference between deno_mongo and node.js driver

Open yuqiang-yuan opened this issue 4 years ago • 6 comments

I'm facing a performance issue between deno_mongo driver and official mongodb driver for Node.js. Here are the versions information of my test:

  • MongoDB server: 4.2.6, sharded database and collection. all the mongos and mongodb processes are located in the same host.
  • deno_mongo driver: 0.28.0
  • mongodb driver for Node.js: 4.2.0
  • deno: 1.16.3
  • Node.js: 12.19.0
  • OS: Ubuntu Desktop 20.04.3

My laptop connects to mongodb server via wired connection and I send a very simple query to get all the 16 documents from a small collection. My connection string is very simple: mongodb://172.16.1.95:27017

Here are the time used:

With deno_mongo driver:

deno: 16 documents returned from mongodb used: 103 ms
deno: 16 documents returned from mongodb used: 99 ms
deno: 16 documents returned from mongodb used: 94 ms
deno: 16 documents returned from mongodb used: 95 ms
deno: 16 documents returned from mongodb used: 96 ms
deno: 16 documents returned from mongodb used: 98 ms
deno: 16 documents returned from mongodb used: 94 ms
deno: 16 documents returned from mongodb used: 96 ms
deno: 16 documents returned from mongodb used: 95 ms
deno: 16 documents returned from mongodb used: 96 ms

With mongodb official driver for Node.js:

node.js: 16 documents returned from mongodb used: 11 ms
node.js: 16 documents returned from mongodb used: 3 ms
node.js: 16 documents returned from mongodb used: 2 ms
node.js: 16 documents returned from mongodb used: 3 ms
node.js: 16 documents returned from mongodb used: 4 ms
node.js: 16 documents returned from mongodb used: 3 ms
node.js: 16 documents returned from mongodb used: 6 ms
node.js: 16 documents returned from mongodb used: 4 ms
node.js: 16 documents returned from mongodb used: 3 ms
node.js: 16 documents returned from mongodb used: 4 ms

I'd like to know why is there such a significant performance difference between deno_mongo driver and official mongodb driver for Node.js. Or I did something wrong?

Here are the source code:

Deno:

import { Document, MongoClient } from "mongo/mod.ts";

const client = new MongoClient();

console.log('connecting to mongodb...');
await client.connect('mongodb://172.16.1.95:27017/');
console.log('mongodb connected');

const db = client.database('mqp');
const col = db.collection<Document>('dataDicts');

for (let i = 0; i < 10; i++) {
    const _ms = Date.now();
    const docs = await col.find({}).toArray();
    console.log(`deno: ${docs.length} documents returned from mongodb used: ${ Date.now() - _ms } ms`);
}

client.close();

Node.js:

const { MongoClient } = require('mongodb');

const url = 'mongodb://172.16.1.95:27017/';
const client = new MongoClient(url);

// Database Name
const dbName = 'mqp';

async function main() {
  // Use connect method to connect to the server
  await client.connect();
  console.log('Connected successfully to server');
  const db = client.db(dbName);
  const collection = db.collection('dataDicts');

  for (let i = 0; i < 10; i++) {
    const _ms = Date.now();
    const docs = await collection.find({}).toArray();
    console.log(`node.js: ${docs.length} documents returned from mongodb used: ${ Date.now() - _ms } ms`);
  }

  return 'done.';
}

main()
  .then(console.log)
  .catch(console.error)
  .finally(() => client.close());

yuqiang-yuan avatar Nov 30 '21 16:11 yuqiang-yuan

This is an smaller repro for this bug

import { Document, MongoClient } from "./mod.ts";

const client = new MongoClient();

await client.connect("mongodb://localhost:27017");
console.log("mongodb connected");

const db = client.database("app");
const col = db.collection<Document>("events");

const t0 = performance.now();
await col.find({}).toArray();
const t1 = performance.now();
await col.find({}).toArray();
const t2 = performance.now();
await col.find({}).toArray();
const t3 = performance.now();

console.log(`A took ${t1 - t0} ms`);
console.log(`B took ${t2 - t1} ms`);
console.log(`C took ${t3 - t2} ms`);


and result:

A took 21.15469200000001 ms
B took 83.48546299999998 ms
C took 14.065189000000032 ms

node.js result:

A took 36 ms
B took 10 ms
C took 9 ms

erfanium avatar Nov 30 '21 17:11 erfanium

@erfanium Nice work! I've tested the new code and it took about 12ms for retrieving 16 documents. But it's still 2~4 times slower than the node.js driver. It will be appreciated if you would keep tracking this. Thanks and Regards,

yuqiang-yuan avatar Dec 02 '21 16:12 yuqiang-yuan

@yuqiang-yuan Thank you yeah, there's work to be done about performance but i'm trying to keep everything in balance. some important features are still missing. so it would be nice if others could help about this issue

erfanium avatar Dec 02 '21 20:12 erfanium

@MierenManz I think you were interested in performance/benchmark stuff! This can be a good start

erfanium avatar Dec 02 '21 20:12 erfanium

Thanks I'll look into it :D

MierenManz avatar Dec 02 '21 22:12 MierenManz

@yuqiang-yuan Thank you yeah, there's work to be done about performance but i try to keep everything in balance. some important features are still missing. so it would be nice if others could help about this issue

Thank you very much for your repair. Last Thursday, I also found the query difference between the Deno and nodejs versions of my project, which is much better than before.

jiawei397 avatar Dec 05 '21 07:12 jiawei397