-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson-stream-stringify-benchmark.js
More file actions
369 lines (349 loc) · 13.5 KB
/
Copy pathjson-stream-stringify-benchmark.js
File metadata and controls
369 lines (349 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
import fs from "fs"
import path from "path"
import crypto from "crypto"
// NPM modules
import JSONStream from "JSONStream"
import bigJson from "big-json"
import { JsonStreamStringify } from 'json-stream-stringify';
import { stringifyJsonStream } from "json-stream-es";
import stream from "stream";
// import concatjson from "concatjson"
// import ndjson from "ndjson"
// https://github.com/microsoft/playwright/issues/36707
// `recordHar: { path: "requests.har", content: "attach" }`
// creates a smaller `requests.har` file (still can exceed 512MB in rare cases)
// with the attached files stored in the same directory
// const inputHarPath = "requests.split.har"
// const harObject = JSON.parse(fs.readFileSync(inputHarPath, "utf8"))
const harObject = {
log: {
entries: []
}
}
// populate the harObject.log.entries array with random data
// 60 * 10MB = 600MB > 512MB
for (let i = 0; i < 60; i++) {
const entry = {
"startedDateTime": "2025-07-17T14:38:52.853Z",
"time": 743.019,
"request": {
"method": "GET",
"url": "https://example.com/page1?somekey=someval",
"httpVersion": "HTTP/2.0",
"cookies": [],
"headers": [
],
"queryString": [
{
"name": "somekey",
"value": "someval"
}
],
"headersSize": 999,
"bodySize": 0
},
"response": {
"status": 200,
"statusText": "",
"httpVersion": "HTTP/2.0",
"cookies": [
],
"content": {
"size": 999999,
"mimeType": "text/html;charset=UTF-8",
"compression": 0,
// "_file": "0000000000000000000000000000000000000000.html"
"text": crypto.randomBytes(10 * 1024 * 1024 / (4 / 3) | 0).toString('base64'), // 10MB
"encoding": "base64"
},
"headersSize": 0,
"bodySize": 999999,
"redirectURL": "",
"_transferSize": 999999
},
"cache": {},
"timings": {
"dns": 59.043,
"connect": 272.799,
"ssl": 256.649,
"send": 0,
"wait": 151.576,
"receive": 2.952
},
"pageref": "page@00000000000000000000000000000000",
"serverIPAddress": "1.2.3.4",
"_serverPort": 443,
"_securityDetails": {
"protocol": "TLS 1.3",
"subjectName": "example.com",
"issuer": "Example RSA 2048 M01",
"validFrom": 0,
"validTo": 9999999999
}
}
harObject.log.entries.push(entry)
}
// const outputHarPath = "test.har"
/*
const attachedFilesDir = path.dirname(inputHarPath)
// embed file contents
var t1 = Date.now()
// for (let entryIdx = 0; entryIdx < harObject.log.entries.length; entryIdx++) {
for (const entry of harObject.log.entries) {
// const entry = harObject.log.entries[entryIdx]
const attachedFile = entry.response?.content?._file
if (!attachedFile) continue
delete entry.response.content._file
entry.response.content.text = fs.readFileSync(`${attachedFilesDir}/${attachedFile}`).toString("base64")
entry.response.content.encoding = "base64"
}
var t2 = Date.now()
console.log(`embed file contents done after ${(t2 - t1) / 1000} seconds`)
*/
/*
// try to stringify
try {
var t1 = Date.now()
fs.writeFileSync('test.json', JSON.stringify(harObject))
var t2 = Date.now()
console.log(`JSON.stringify done after ${(t2 - t1) / 1000} seconds`)
}
catch (error) {
var t2 = Date.now()
console.log(`JSON.stringify fails after ${(t2 - t1) / 1000} seconds`)
switch (error.constructor) {
case RangeError:
*/
// RangeError: Invalid string length
// harObject is too big for JSON.stringify
let streamJSONMethodList = [
"JSON.stringify",
"big-json",
"json-stream-stringify",
"json-stream-es",
"har-json-stringify",
]
// running all tests in one process disfavors the first method
// following methods profit from a warm cache
// run all methods with a cold cache:
/*
for x in JSON.stringify big-json json-stream-stringify json-stream-es har-json-stringify; do
for y in {1..3}; do
node json-stream-stringify-benchmark.js $x;
done
done
*/
if (process.argv[2]) {
streamJSONMethodList = [process.argv[2]]
}
for (const streamJSONMethod of streamJSONMethodList) {
// console.log(`streamJSONMethod ${streamJSONMethod}`)
// const outputHarPath = `test.${streamJSONMethod}.har`
const outputHarPath = '/dev/null' // dont waste SSD writes
var t1 = Date.now()
let jsonStream = null
let useJsonStream = true
switch (streamJSONMethod) {
case "JSON.stringify":
useJsonStream = false
var t1 = Date.now()
try {
fs.writeFileSync('test.json', JSON.stringify(harObject))
var t2 = Date.now()
console.log(`- JSON.stringify done after ${(t2 - t1) / 1000} seconds`)
}
catch (error) {
var t2 = Date.now()
console.log(`- JSON.stringify fails after ${(t2 - t1) / 1000} seconds`)
}
break
case "JSONStream":
// FIXME RangeError: Invalid string length
// https://github.com/dominictarr/JSONStream
// https://github.com/dominictarr/JSONStream/blob/master/test/stringify.js
// https://www.reddit.com/r/node/comments/4vmloi/streaming_a_large_json_object_to_a_file_to_avoid/
jsonStream = JSONStream.stringify()
// const jsonStream = JSONStream.stringifyObject()
for (var key in harObject) {
jsonStream.write([key, harObject[key]])
}
jsonStream.end()
/*
jsonStream.pipe(fs.createWriteStream(outputHarPath))
var t2 = Date.now()
var outputSizeMB = fs.statSync(outputHarPath).size / 1024 / 1024
console.log(`- ${streamJSONMethod} done after ${(t2 - t1) / 1000} seconds for ${outputSizeMB} MB`)
*/
// no. this fails because harObject has only one key: harObject.log
// and JSONStream does no recursive chunking
/*
var json = JSON.stringify(data, null, indent)
^
RangeError: Invalid string length
*/
break
case "big-json":
// streamJSONMethod big-json done after 7.944 seconds for 679.522294998169 MB
// https://github.com/DonutEspresso/big-json
// slow? https://github.com/DonutEspresso/big-json/issues/24
// slow? https://stackoverflow.com/questions/73947747/writing-big-json-stream-to-a-file-why-is-it-so-slow
// https://github.com/DonutEspresso/big-json
// https://stackoverflow.com/questions/73947747/writing-big-json-stream-to-a-file-why-is-it-so-slow
jsonStream = bigJson.createStringifyStream({ body: harObject });
break
case "json-stream-stringify":
// streamJSONMethod json-stream-stringify done after 3.967 seconds for 679.522294998169 MB
// https://github.com/Faleij/json-stream-stringify
// trailing ";" is required to fix: ReferenceError: Cannot access 'jsonStream' before initialization
jsonStream = new JsonStreamStringify(harObject);
break
case "json-stream-es":
useJsonStream = false
var outputStream = fs.createWriteStream(outputHarPath)
jsonStream = stringifyJsonStream(harObject);
(jsonStream
// TypeError: jsonStream.pipe is not a function
// https://github.com/cdauth/json-stream-es/issues/5
// .pipe(fs.createWriteStream(outputHarPath))
.pipeTo(stream.Writable.toWeb(outputStream))
// too early
/*
.then(() => {
var t2 = Date.now()
var outputSizeMB = fs.statSync(outputHarPath).size / 1024 / 1024
console.log(`- ${streamJSONMethod} done after ${(t2 - t1) / 1000} seconds for ${outputSizeMB} MB`)
})
*/
.catch((error) => {
throw error
})
)
outputStream.on("close", function() {
var t2 = Date.now()
// FIXME get number of bytes written to /dev/null
var outputSizeMB = fs.statSync(outputHarPath).size / 1024 / 1024
console.log(`- ${streamJSONMethod} done after ${(t2 - t1) / 1000} seconds for ${outputSizeMB} MB`)
// TODO resolve promise
})
break
case "concatjson":
// FIXME RangeError: Invalid string length
// https://github.com/manidlou/concatjson
jsonStream = concatjson.stringify()
jsonStream.write(harObject)
jsonStream.end()
break
case "ndjson":
// WONTFIX only works for newline-delimited JSON
// FIXME RangeError: Invalid string length
// https://github.com/ndjson/ndjson.js
jsonStream = ndjson.stringify()
jsonStream.write(harObject)
jsonStream.end()
break
case "har-json-stringify":
// custom serializer for HAR format
// streamJSONMethod har-json-stringify done after 2.176 seconds for 687.0114965438843 MB
useJsonStream = false
var outputStream = fs.createWriteStream(outputHarPath)
outputStream.on("open", function() {
// the only big part of the object is the array harObject.log.entries
// so we remove it from harObject
var harLogEntries = harObject.log.entries
delete harObject.log.entries
// stringify the rest with indent=2
var harString = JSON.stringify(harObject, null, 2)
// remove the trailing closing brace of harObject.log
harString = harString.slice(0, -5)
harString += ', "entries": [\n' // open harObject.log.entries
outputStream.write(harString); harString = ""
// add entries
let isFirstEntry = true
for (const entry of harLogEntries) {
try {
harString = (isFirstEntry ? "" : ",\n") + JSON.stringify(entry, null, 2)
outputStream.write(harString); harString = ""
}
catch (error) {
switch (error.constructor) {
case RangeError:
// RangeError: Invalid string length
// entry is too big for JSON.stringify
outputStream.write(isFirstEntry ? "" : ",\n")
jsonStream = new JsonStreamStringify(entry);
// FIXME use Promise + await + async function
(jsonStream
.pipe(outputStream)
.on('finish', () => {
console.log('done JsonStreamStringify(entry)')
})
.on('error', (error) => {
console.log('Error at path', jsonStream.stack.join('.'))
throw error
})
)
break
default:
throw error
}
}
isFirstEntry = false
}
// harString += 'xxxxx\n'
harString += ']\n' // close harObject.log.entries
harString += '}\n' // close harObject.log
harString += '}\n' // close harObject
outputStream.write(harString); harString = ""
outputStream.end()
outputStream.close()
})
outputStream.on("close", function() {
var t2 = Date.now()
// FIXME get number of bytes written to /dev/null
var outputSizeMB = fs.statSync(outputHarPath).size / 1024 / 1024
console.log(`- ${streamJSONMethod} done after ${(t2 - t1) / 1000} seconds for ${outputSizeMB} MB`)
// TODO resolve promise
})
break
// case "node-ld-jsonstream":
// https://github.com/timkuijsten/node-ld-jsonstream
// no. only works with arrays -> line-delimited JSON
// https://github.com/uhop/stream-json
// no. JSON.parse only
default:
throw new Error(`unknown streamJSONMethod ${streamJSONMethod}`)
}
if (useJsonStream) {
var outputStream = fs.createWriteStream(outputHarPath);
(jsonStream
.pipe(outputStream)
// no, this is called too early
/*
.on('finish', () => {
var t2 = Date.now()
var outputSizeMB = fs.statSync(outputHarPath).size / 1024 / 1024
console.log(`- ${streamJSONMethod} done after ${(t2 - t1) / 1000} seconds for ${outputSizeMB} MB`)
})
*/
.on('error', (error) => {
if (streamJSONMethod == "json-stream-stringify") {
console.log('Error at path', jsonStream.stack.join('.'))
}
throw error
})
);
outputStream.on("close", function() {
var t2 = Date.now()
// FIXME get number of bytes written to /dev/null
var outputSizeMB = fs.statSync(outputHarPath).size / 1024 / 1024
console.log(`- ${streamJSONMethod} done after ${(t2 - t1) / 1000} seconds for ${outputSizeMB} MB`)
})
}
}
/*
break // case RangeError
default:
throw error
}
}
*/