Debian Codegraph @

fastify-busboy/bench/dicer/dicer-bench-multipart-parser.js

raw
1 'use strict'
2
3 const Dicer = require('../../deps/dicer/lib/Dicer')
4
5 function createMultipartBuffer(boundary, size) {
6 const head =
7 '--' + boundary + '\r\n'
8 + 'content-disposition: form-data; name="field1"\r\n'
9 + '\r\n'
10 , tail = '\r\n--' + boundary + '--\r\n'
11 , buffer = Buffer.allocUnsafe(size);
12
13 buffer.write(head, 0, 'ascii');
14 buffer.write(tail, buffer.length - tail.length, 'ascii');
15 return buffer;
16 }
17
18 for (var i = 0, il = 10; i < il; i++) { // eslint-disable-line no-var
19 const boundary = '-----------------------------168072824752491622650073',
20 d = new Dicer({ boundary: boundary }),
21 mb = 100,
22 buffer = createMultipartBuffer(boundary, mb * 1024 * 1024),
23 callbacks =
24 {
25 partBegin: -1,
26 partEnd: -1,
27 headerField: -1,
28 headerValue: -1,
29 partData: -1,
30 end: -1,
31 };
32
33
34 d.on('part', function (p) {
35 callbacks.partBegin++;
36 p.on('header', function (header) {
37 /*for (var h in header)
38 console.log('Part header: k: ' + inspect(h) + ', v: ' + inspect(header[h]));*/
39 });
40 p.on('data', function (data) {
41 callbacks.partData++;
42 //console.log('Part data: ' + inspect(data.toString()));
43 });
44 p.on('end', function () {
45 //console.log('End of part\n');
46 callbacks.partEnd++;
47 });
48 });
49 d.on('end', function () {
50 //console.log('End of parts');
51 callbacks.end++;
52 });
53
54 const start = +new Date();
55 d.write(buffer);
56 const duration = +new Date - start;
57 const mbPerSec = (mb / (duration / 1000)).toFixed(2);
58
59 console.log(mbPerSec + ' mb/sec');
60 }