()
| 9 | }); |
| 10 | |
| 11 | async function run() { |
| 12 | const uri = 'mongodb://127.0.0.1:27017/mongoose_benchmark'; |
| 13 | |
| 14 | await mongoose.connect(uri); |
| 15 | const FooSchema = new mongoose.Schema({ foo: String }); |
| 16 | const FooModel = mongoose.model('Foo', FooSchema, 'foos'); |
| 17 | |
| 18 | const client = new MongoClient(uri); |
| 19 | await client.connect(); |
| 20 | const db = client.db(); |
| 21 | const fooCollection = db.collection('foos'); |
| 22 | |
| 23 | if (!process.env.MONGOOSE_BENCHMARK_SKIP_SETUP) { |
| 24 | await FooModel.deleteMany({}); |
| 25 | await fooCollection.deleteMany({}); |
| 26 | } |
| 27 | |
| 28 | const numDocs = 1500; |
| 29 | const createDocs = iteration => { |
| 30 | const docs = []; |
| 31 | for (let i = 0; i < numDocs; ++i) { |
| 32 | docs.push({ foo: 'test foo ' + iteration + ' ' + i }); |
| 33 | } |
| 34 | return docs; |
| 35 | }; |
| 36 | |
| 37 | for (let i = 0; i < 10; ++i) { |
| 38 | // Warm up |
| 39 | await FooModel.insertMany(createDocs(i)); |
| 40 | } |
| 41 | |
| 42 | const numIterations = 200; |
| 43 | |
| 44 | for (let i = 0; i < 3; ++i) { |
| 45 | const driverInsertManyStart = Date.now(); |
| 46 | for (let i = 0; i < numIterations; ++i) { |
| 47 | await fooCollection.insertMany(createDocs(i)); |
| 48 | } |
| 49 | const driverInsertManyEnd = Date.now(); |
| 50 | |
| 51 | const mongooseInsertManyStart = Date.now(); |
| 52 | for (let i = 0; i < numIterations; ++i) { |
| 53 | await FooModel.insertMany(createDocs(i), { lean: false }); |
| 54 | } |
| 55 | const mongooseInsertManyEnd = Date.now(); |
| 56 | |
| 57 | const results = { |
| 58 | 'Average mongoose insertMany time ms': +((mongooseInsertManyEnd - mongooseInsertManyStart) / numIterations).toFixed(2), |
| 59 | 'Average driver insertMany time ms': +((driverInsertManyEnd - driverInsertManyStart) / numIterations).toFixed(2) |
| 60 | }; |
| 61 | |
| 62 | console.log(JSON.stringify(results, null, ' ')); |
| 63 | } |
| 64 | |
| 65 | await client.close(); |
| 66 | process.exit(0); |
| 67 | } |
no test coverage detected