Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongoose 6.3.6 Performance slowdown #11919

Closed
2 tasks done
nikosszzz opened this issue Jun 9, 2022 · 14 comments
Closed
2 tasks done

Mongoose 6.3.6 Performance slowdown #11919

nikosszzz opened this issue Jun 9, 2022 · 14 comments
Labels
needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity

Comments

@nikosszzz
Copy link

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the performance issue has not already been reported

Last performant version

6.3.5

Slowed down in version

6.3.6

Node.js version

16.14.2

馃Ε Performance issue

Hello!

The latest version of mongoose which is 6.3.6 has significantly increased my bot's start up time and increased memory usage, as you can see in this picture:
image (this picture shows how long it took to import a command.)

Meanwhile with 6.3.5, the results differ alot:
image

As you can see it slows down the bot significantly by around 1000~ ms!

Also here is how I connect to my MongoDB database:

import mongoose from "mongoose";
import { config } from "../../utils/config";

export async function database(): Promise<void> {
    mongoose.connect
        (
            config.MONGODB_URI
        )
        .then(() => console.log(`[DATABASE] Connected to database.`))
        .catch(err => console.log(`[DATABASE] Oops, there was an error! ${err}`));
}

Steps to Reproduce

Run a discord bot with mongoose installed as a npm package and make it connect to a MongoDB database.

Expected Behavior

Should not slow down the bot in any way.

@nikosszzz
Copy link
Author

Also, here is a great example of how bad it is jumping up the memory, the following image is using version 6.3.6:
image

And this one is using 6.3.5:
image

@Uzlopak
Copy link
Collaborator

Uzlopak commented Jun 10, 2022

There are not many changes from 6.3.5 to 6.3.6

Can you try to determine which one of the changes in the lib/*.js files resulted in the higher memory consumption?

@AbdelrahmanHafez
Copy link
Collaborator

Welcome @nikosszzz

We can't reproduce the whole system you have with discord bot and mongoose. Can you modify the script below to make it demonstrate the issue you're facing?

11919.js

'use strict';
const mongoose = require('mongoose');
const assert = require('node:assert');


run().catch(console.error);

async function run() {
  const startAt = Date.now();
  await mongoose.connect('mongodb://localhost:27017/test');
  const connectedAt = Date.now();

  const millisecondsToConnect = connectedAt - startAt;
  console.log(millisecondsToConnect);
  assert.equal(millisecondsToConnect < 40, true);

  const mongoServerVersion = await getMongoServerVersion();

  console.log(mongoServerVersion);
  console.log(mongoose.version);

  console.log('All assertions passed.');
}

async function getMongoServerVersion() {
  const MongoAdmin = new mongoose.mongo.Admin(mongoose.connection.db);
  const info = await MongoAdmin.buildInfo();
  const mongoServerVersion = info.version;
  return mongoServerVersion;
}

Output

$ node 11919.js
31
5.0.9
6.3.5
All assertions passed.

$ node 11919.js
28
5.0.9
6.3.6
All assertions passed.

The connection time is consistent for me: 10~35 milliseconds for both versions.

@AbdelrahmanHafez AbdelrahmanHafez added needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Jun 10, 2022
@nikosszzz
Copy link
Author

image
It seems to fail at the assertion part, though it does show the time it took to connect.

@nikosszzz
Copy link
Author

Perhaps this is some issue with the default TSC compiler I use for my TypeScript discord bot? When I use SWC as the compiler, it is completely fine and way faster.

@AbdelrahmanHafez
Copy link
Collaborator

What version of MongoDB are you using? What operating system are you using as well?

Can you move the assertions to the bottom of the function so the logs show up?
I'm guessing that this is related to #6566. Can you try to use this line to connect instead?

  await mongoose.connect('mongodb://127.0.0.1:27017/test', { family: 4 });

@Uzlopak
Copy link
Collaborator

Uzlopak commented Jun 10, 2022

I think it would really help if you patch in your project in node_modules/mongoose the changes you see in the link i provided and report back which of those changes resulted in the performance regression.

@nikosszzz
Copy link
Author

nikosszzz commented Jun 10, 2022

@AbdelrahmanHafez

I'm using Windows 11 Pro 21H1 (build 22000.708).
MongoDB 5.0.8 Enterprise.

with the family: 4 option:
image

Note: I am not connecting to a local Mongo database. I am connecting to my bot's database.

@Uzlopak
Copy link
Collaborator

Uzlopak commented Jun 15, 2022

We will probably not be able to reproduce your issue, if you dont hint which of the few changes resulted in that slowdown. Just saying that you dont get sad, when this issue will get marked as stale and then auto-closed.

@vkarpov15
Copy link
Collaborator

@nikosszzz try upgrading to 6.3.9. I suspect this may be due to #11898, which was released in 6.3.6 and caused a slowdown in the TypeScript compiler

@vkarpov15 vkarpov15 removed performance needs repro script Maybe a bug, but no repro script. The issue reporter should create a script that demos the issue labels Jun 17, 2022
@nikosszzz
Copy link
Author

Hmmm, yes. It seems to have improved and is back to normal.

@AbdelrahmanHafez
Copy link
Collaborator

@vkarpov15 Is #11898 a typo? How can a TS PR affect JS connection performance?

@vkarpov15
Copy link
Collaborator

@AbdelrahmanHafez OP is running through ts-node and reporting slow startup time. ts-node usually runs typescript compilation as part of startup. So that's my guess as to the root cause here

@AbdelrahmanHafez
Copy link
Collaborator

I didn't notice the ts-node bit.

However, running the script in #11919 (comment) calculates the time of the connection, which still demonstrates the issue, so this eliminates the possibility that this is a TS issue.

However, the issue is solved now, so I think we're good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs clarification This issue doesn't have enough information to be actionable. Close after 14 days of inactivity
Projects
None yet
Development

No branches or pull requests

4 participants