Debian Codegraph @

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

raw
1 'use strict'
2
3 // A special, edited version of the multipart parser from parted is needed here
4 // because otherwise it attempts to do some things above and beyond just parsing
5 // -- like saving to disk and whatnot
6
7 var assert = require('node:assert');
8 var Parser = require('./parted-multipart'),
9 boundary = '-----------------------------168072824752491622650073',
10 parser = new Parser('boundary=' + boundary),
11 mb = 100,
12 buffer = createMultipartBuffer(boundary, mb * 1024 * 1024),
13 callbacks =
14 { partBegin: -1,
15 partEnd: -1,
16 headerField: -1,
17 headerValue: -1,
18 partData: -1,
19 end: -1,
20 };
21
22
23 parser.on('header', function() {
24 //callbacks.headerField++;
25 });
26
27 parser.on('data', function() {
28 //callbacks.partBegin++;
29 });
30
31 parser.on('part', function() {
32
33 });
34
35 parser.on('end', function() {
36 //callbacks.end++;
37 });
38
39 var start = +new Date(),
40 nparsed = parser.write(buffer),
41 duration = +new Date - start,
42 mbPerSec = (mb / (duration / 1000)).toFixed(2);
43
44 console.log(mbPerSec+' mb/sec');
45
46 //assert.equal(nparsed, buffer.length);
47
48 function createMultipartBuffer(boundary, size) {
49 var head =
50 '--'+boundary+'\r\n'
51 + 'content-disposition: form-data; name="field1"\r\n'
52 + '\r\n'
53 , tail = '\r\n--'+boundary+'--\r\n'
54 , buffer = Buffer.allocUnsafe(size);
55
56 buffer.write(head, 'ascii', 0);
57 buffer.write(tail, 'ascii', buffer.length - tail.length);
58 return buffer;
59 }
60
61 process.on('exit', function() {
62 /*for (var k in callbacks) {
63 assert.equal(0, callbacks[k], k+' count off by '+callbacks[k]);
64 }*/
65 });