2
2
3
3
const fs = require ( 'fs' ) ;
4
4
const path = require ( 'path' ) ;
5
+ const { Readable } = require ( 'stream' ) ;
6
+ const { pipeline } = require ( 'stream/promises' ) ;
5
7
const { MongoClient } = require ( '../../..' ) ;
6
8
const { GridFSBucket } = require ( '../../..' ) ;
7
9
10
+ // eslint-disable-next-line no-restricted-modules
11
+ const { MONGODB_ERROR_CODES } = require ( '../../../lib/error' ) ;
12
+
8
13
const DB_NAME = 'perftest' ;
9
14
const COLLECTION_NAME = 'corpus' ;
10
15
@@ -48,7 +53,11 @@ function initCollection() {
48
53
}
49
54
50
55
function dropCollection ( ) {
51
- return this . collection . drop ( ) ;
56
+ return this . collection . drop ( ) . catch ( e => {
57
+ if ( e . code !== MONGODB_ERROR_CODES . NamespaceNotFound ) {
58
+ throw e ;
59
+ }
60
+ } ) ;
52
61
}
53
62
54
63
function initBucket ( ) {
@@ -65,6 +74,33 @@ function makeLoadJSON(name) {
65
74
} ;
66
75
}
67
76
77
+ function makeLoadTweets ( makeId ) {
78
+ return function ( ) {
79
+ const doc = this . doc ;
80
+ const tweets = [ ] ;
81
+ for ( let _id = 1 ; _id <= 10000 ; _id += 1 ) {
82
+ tweets . push ( Object . assign ( { } , doc , makeId ? { _id } : { } ) ) ;
83
+ }
84
+
85
+ return this . collection . insertMany ( tweets ) ;
86
+ } ;
87
+ }
88
+
89
+ function makeLoadInsertDocs ( numberOfOperations ) {
90
+ return function ( ) {
91
+ this . docs = [ ] ;
92
+ for ( let i = 0 ; i < numberOfOperations ; i += 1 ) {
93
+ this . docs . push ( Object . assign ( { } , this . doc ) ) ;
94
+ }
95
+ } ;
96
+ }
97
+
98
+ async function writeSingleByteFileToBucket ( ) {
99
+ const stream = this . bucket . openUploadStream ( 'setup-file.txt' ) ;
100
+ const oneByteFile = Readable . from ( 'a' ) ;
101
+ return pipeline ( oneByteFile , stream ) ;
102
+ }
103
+
68
104
module . exports = {
69
105
makeClient,
70
106
connectClient,
@@ -78,5 +114,8 @@ module.exports = {
78
114
loadSpecFile,
79
115
loadSpecString,
80
116
initBucket,
81
- dropBucket
117
+ dropBucket,
118
+ makeLoadTweets,
119
+ makeLoadInsertDocs,
120
+ writeSingleByteFileToBucket
82
121
} ;
0 commit comments