11'use strict' ;
22
33const mongoose = require ( '../' ) ;
4- const Benchmark = require ( 'benchmark' ) ;
5-
6- const { Schema } = mongoose ;
74
85run ( ) . catch ( err => {
9- console . error ( err ) ;
10- process . exit ( 1 ) ;
6+ console . error ( err ) ;
7+ process . exit ( - 1 ) ;
118} ) ;
129
1310async function run ( ) {
1411 await mongoose . connect ( 'mongodb://127.0.0.1:27017/mongoose_benchmark' ) ;
1512
16- const bookSchema = new Schema ( {
13+ const bookSchema = new mongoose . Schema ( {
1714 ticker : String ,
1815 asks : [ [ Number ] ] ,
1916 bids : [ [ Number ] ] ,
@@ -22,24 +19,32 @@ async function run() {
2219
2320 const Book = mongoose . model ( 'BookNestedArray' , bookSchema ) ;
2421
25- let doc = { asks : [ ] , bids : [ ] } ;
22+ const doc = { asks : [ ] , bids : [ ] } ;
2623 for ( let i = 0 ; i < 10000 ; ++ i ) {
2724 doc . asks . push ( [ i ] ) ;
2825 doc . bids . push ( [ i ] ) ;
2926 }
3027
31- const suite = new Benchmark . Suite ( ) ;
28+ if ( ! process . env . MONGOOSE_BENCHMARK_SKIP_SETUP ) {
29+ await Book . deleteMany ( { } ) ;
30+ }
3231
33- suite
34- . add ( 'BookNestedArray document construction' , function ( ) {
32+ const constructStart = Date . now ( ) ;
33+ for ( let i = 0 ; i < 100 ; ++ i ) {
3534 new Book ( doc ) ;
36- } )
37- . on ( 'cycle' , function ( evt ) {
38- if ( process . env . MONGOOSE_DEV || process . env . PULL_REQUEST ) {
39- console . log ( String ( evt . target ) ) ;
40- }
41- } )
42- . run ( ) ;
43-
44- await mongoose . disconnect ( ) ;
35+ }
36+ const constructEnd = Date . now ( ) ;
37+
38+ const inst = new Book ( doc ) ;
39+ const saveStart = Date . now ( ) ;
40+ await inst . save ( ) ;
41+ const saveEnd = Date . now ( ) ;
42+
43+ const results = {
44+ 'document construction (100x) ms' : + ( constructEnd - constructStart ) . toFixed ( 2 ) ,
45+ 'save() time ms' : + ( saveEnd - saveStart ) . toFixed ( 2 )
46+ } ;
47+
48+ console . log ( JSON . stringify ( results , null , ' ' ) ) ;
49+ process . exit ( 0 ) ;
4550}
0 commit comments