1
+ const { Readable } = require ( 'stream' ) ;
2
+ const { pipeline } = require ( 'stream/promises' ) ;
1
3
const {
2
4
loadSpecFile,
3
5
makeLoadJSON,
@@ -12,30 +14,24 @@ const {
12
14
createCollection,
13
15
dropCollection,
14
16
dropBucket,
15
- initBucket
17
+ initBucket,
18
+ writeSingleByteFileToBucket
16
19
} = require ( '../../driverBench/common' ) ;
17
20
18
21
function loadGridFs ( ) {
19
22
this . bin = loadSpecFile ( [ 'single_and_multi_document' , 'gridfs_large.bin' ] ) ;
20
23
}
21
24
22
- function findManyAndEmptyCursor ( done ) {
23
- return this . collection . find ( { } ) . forEach ( ( ) => { } , done ) ;
24
- }
25
-
26
- function docBulkInsert ( done ) {
27
- return this . collection . insertMany ( this . docs , { ordered : true } , done ) ;
28
- }
29
-
30
25
function gridFsInitUploadStream ( ) {
31
- this . stream = this . bucket . openUploadStream ( 'gridfstest' ) ;
26
+ this . uploadStream = this . bucket . openUploadStream ( 'gridfstest' ) ;
32
27
}
33
28
34
- function writeSingleByteToUploadStream ( ) {
35
- return new Promise ( ( resolve , reject ) => {
36
- this . stream . write ( '\0' , null , err => ( err ? reject ( err ) : resolve ( ) ) ) ;
37
- } ) ;
29
+ async function gridFsUpload ( ) {
30
+ const uploadData = Readable . from ( this . bin ) ;
31
+ const uploadStream = this . uploadStream ;
32
+ await pipeline ( uploadData , uploadStream ) ;
38
33
}
34
+
39
35
function makeMultiBench ( suite ) {
40
36
return suite
41
37
. benchmark ( 'findManyAndEmptyCursor' , benchmark =>
@@ -48,7 +44,12 @@ function makeMultiBench(suite) {
48
44
. setup ( dropDb )
49
45
. setup ( initCollection )
50
46
. setup ( makeLoadTweets ( false ) )
51
- . task ( findManyAndEmptyCursor )
47
+ . task ( async function ( ) {
48
+ // eslint-disable-next-line no-unused-vars
49
+ for await ( const _ of this . collection . find ( { } ) ) {
50
+ // do nothing
51
+ }
52
+ } )
52
53
. teardown ( dropDb )
53
54
. teardown ( disconnectClient )
54
55
)
@@ -67,7 +68,9 @@ function makeMultiBench(suite) {
67
68
. beforeTask ( dropCollection )
68
69
. beforeTask ( createCollection )
69
70
. beforeTask ( initCollection )
70
- . task ( docBulkInsert )
71
+ . task ( async function ( ) {
72
+ await this . collection . insertMany ( this . docs , { ordered : true } ) ;
73
+ } )
71
74
. teardown ( dropDb )
72
75
. teardown ( disconnectClient )
73
76
)
@@ -86,7 +89,9 @@ function makeMultiBench(suite) {
86
89
. beforeTask ( dropCollection )
87
90
. beforeTask ( createCollection )
88
91
. beforeTask ( initCollection )
89
- . task ( docBulkInsert )
92
+ . task ( async function ( ) {
93
+ await this . collection . insertMany ( this . docs , { ordered : true } ) ;
94
+ } )
90
95
. teardown ( dropDb )
91
96
. teardown ( disconnectClient )
92
97
)
@@ -103,10 +108,8 @@ function makeMultiBench(suite) {
103
108
. beforeTask ( dropBucket )
104
109
. beforeTask ( initBucket )
105
110
. beforeTask ( gridFsInitUploadStream )
106
- . beforeTask ( writeSingleByteToUploadStream )
107
- . task ( function ( done ) {
108
- this . stream . on ( 'error' , done ) . end ( this . bin , null , ( ) => done ( ) ) ;
109
- } )
111
+ . beforeTask ( writeSingleByteFileToBucket )
112
+ . task ( gridFsUpload )
110
113
. teardown ( dropDb )
111
114
. teardown ( disconnectClient )
112
115
)
@@ -123,21 +126,16 @@ function makeMultiBench(suite) {
123
126
. setup ( dropBucket )
124
127
. setup ( initBucket )
125
128
. setup ( gridFsInitUploadStream )
126
- . setup ( function ( ) {
127
- return new Promise ( ( resolve , reject ) => {
128
- this . stream . end ( this . bin , null , err => {
129
- if ( err ) {
130
- return reject ( err ) ;
131
- }
132
-
133
- this . id = this . stream . id ;
134
- this . stream = undefined ;
135
- resolve ( ) ;
136
- } ) ;
137
- } ) ;
129
+ . setup ( async function ( ) {
130
+ await gridFsUpload . call ( this ) ;
131
+ this . id = this . uploadStream . id ;
132
+ this . uploadData = undefined ;
138
133
} )
139
- . task ( function ( done ) {
140
- this . bucket . openDownloadStream ( this . id ) . resume ( ) . on ( 'end' , done ) ;
134
+ . task ( async function ( ) {
135
+ // eslint-disable-next-line no-unused-vars
136
+ for await ( const _ of this . bucket . openDownloadStream ( this . id ) ) {
137
+ // do nothing
138
+ }
141
139
} )
142
140
. teardown ( dropDb )
143
141
. teardown ( disconnectClient )
0 commit comments