-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
10x slowdown on Windows when upgrading from Mongoose 4 to 5. #6566
Comments
Here are the test outputs for the Mongoose 4 and 5 branches, respectively, on my Windows 10 desktop. Running the tests repeatedly gave similar results.
|
Thanks for reporting, will investigate ASAP |
I am not sure if it's relevant, but the connection process takes 5 seconds on Windows, I'll try with mongoose v4 and see if it's a v5 thing or that's the normal duration. |
Running the following code to calculate the connection time: const mongoose = require('mongoose');
const run = async () => {
let beforeConnection = Date.now();
await mongoose.connect('mongodb://localhost/somedb');
let afterConnection = Date.now();
console.log(`Time to connect is: ${(afterConnection - beforeConnection) / 1000}s`);
};
run().catch(console.error); output using mongoose v4.13.14:
output using mongoose v5.1.5:
Windows 10 1803 (Build 17134.81) |
Just to clarify, are you running mongodb locally or are you connecting to a remote mongodb @AbdelrahmanHafez ? |
Locally |
It's not just the connection either, deleting, and creating data are 10x+ slower as well. Using the following code: const mongoose = require('mongoose');
const { Schema } = mongoose;
mongoose.Promise = Promise;
const userSchema = new Schema();
const User = mongoose.model('User', userSchema);
const users = [];
for (let i = 0; i < 1000; i++) users.push(new User());
const run = async () => {
await mongoose.connect('mongodb://localhost/test');
const before = Date.now();
await User.remove();
await User.create(users);
const after = Date.now();
console.log(`Duration: ${(after - before) / 1000}s`);
};
run().catch(console.error); v4.13.14 outputs:
v5.1.5 outputs:
|
Perhaps we should remove the |
Needs repro just means that the mongoose devs haven't been able to confirm this as a bug. In this case, it means we haven't had the time to boot up windows and try it. Perhaps we should split that label into 2 labels, one that says theres no repro script and one that says theres a repro script but the issue is blocked on mongoose devs? |
a |
Speed is greatly reduced for me too. I'm Using MongoDb 4.0, Mongoose 5.2.1, Nodejs 10.5.0 and Windows 10. |
@cosminn777 I don't think so, I am using MongoDB v3.2.7 and I am experiencing the same slowdown. |
@AbdelrahmanHafez you are right. From my tests I get slowdown on both MongoDB 4.0.0 and MongoDB 3.6.5 |
I am doing many creation/editing operations in DB and I do encounter heavy slowdown. |
The issue might be from one of the underlying .js libraries that Mongoose is using. |
Are you connecting by name or IP? On the example you posted, you documented Regards, |
@NewEraCracker interesting! This actually reduced the connection time from 1.04s to 0.04s, and the read/write operation in #6566 (comment) from 4.563s to 0.577s. Which is way better, yet slightly slower than mongoose v4.14.13. This is a good place to start debugging the issue. However, I am curious on how you found out that was the problem, was it a happy accident or by debugging it? If so, please elaborate on your debugging process. |
There's a similar issue starting with PHP 5.3 and the native MySQL driver (mysqlnd) that cause same delays. I spotted this back in 2011 I think. I am guessing something in node or mongoose or a library is now preferring IPv6 but MongoDB is only listening in IPv4. There are two possible workarounds I am aware of:
|
Already switched to 127.0.0.1 in the connection string, and the tests run much faster now. Thank you! |
Yeah I can confirm that switching to 127.0.0.1 from localhost speeds up initial connection from 1000ms to 30ms on windows. Currently digging in to why this is the case. |
…260) If the user does not specify an IP family, we will attempt to connect first via IPv6, and fall back to IPv4 if that fails Fixes NODE-1222
@NewEraCracker is spot on, good call. The reason for this slowdown is that, starting in mongodb driver 3.0.0, the mongodb driver tries to connect using ipv6 first, and falls back to ipv4 if that fails unless you set the mongodb-js/mongodb-core@107bae5 As a workaround, do this: const mongoose = require('mongoose');
const run = async () => {
let beforeConnection = Date.now();
await mongoose.connect('mongodb://localhost/somedb', { family: 4 });
let afterConnection = Date.now();
console.log(`Time to connect is: ${(afterConnection - beforeConnection) / 1000}s`);
};
run().catch(console.error); We'll add documentation around this option |
My tests are taking 10x as long on Windows when I upgrade from Mongoose 4 to 5. The slowdown doesn't appear to happen on my Ubuntu laptop.
I created a repo with two branches to illustrate the problem,
https://github.com/deklanw/pensieve-mongoose-4-to-5-speed-testing
I included the .env file for testing. If need be just change this line
MONGODB_URI=mongodb://localhost:27017/pensieve-test
to reflect your testing environment.
Just,
yarn install
yarn test
I tested on my Windows desktop and laptop to make sure. They're both Windows 10 x64. The problem occurred on both. The problem doesn't occur on my Ubuntu laptop.
Here are the versions:
Windows 10 x64 laptop: mongodb v3.6.5 node v10.1.0 yarn v1.6.0
Windows 10 x64 desktop: mongodb v3.6.3 node v10.3.0 yarn v1.7.0
The Ubuntu laptop has the same versions of everything as the Windows 10 desktop.
@lineus Tested this on OSX, I believe, and didn't find the slowdown. So it appears to be Windows related.
The text was updated successfully, but these errors were encountered: