Debian Codegraph @

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

raw
1 'use strict'
2
3 var multipartser = require('multipartser'),
4 boundary = '-----------------------------168072824752491622650073',
5 parser = multipartser(),
6 mb = 100,
7 buffer = createMultipartBuffer(boundary, mb * 1024 * 1024),
8 callbacks =
9 { partBegin: -1,
10 partEnd: -1,
11 headerField: -1,
12 headerValue: -1,
13 partData: -1,
14 end: -1,
15 };
16
17 parser.boundary( boundary );
18
19 parser.on( 'part', function ( part ) {
20 });
21
22 parser.on( 'end', function () {
23 //console.log( 'completed parsing' );
24 });
25
26 parser.on( 'error', function ( error ) {
27 console.error( error );
28 });
29
30 var start = +new Date(),
31 nparsed = parser.data(buffer),
32 nend = parser.end(),
33 duration = +new Date - start,
34 mbPerSec = (mb / (duration / 1000)).toFixed(2);
35
36 console.log(mbPerSec+' mb/sec');
37
38 //assert.equal(nparsed, buffer.length);
39
40 function createMultipartBuffer(boundary, size) {
41 var head =
42 '--'+boundary+'\r\n'
43 + 'content-disposition: form-data; name="field1"\r\n'
44 + '\r\n'
45 , tail = '\r\n--'+boundary+'--\r\n'
46 , buffer = Buffer.allocUnsafe(size);
47
48 buffer.write(head, 'ascii', 0);
49 buffer.write(tail, 'ascii', buffer.length - tail.length);
50 return buffer;
51 }
52
53 process.on('exit', function() {
54 /*for (var k in callbacks) {
55 assert.equal(0, callbacks[k], k+' count off by '+callbacks[k]);
56 }*/
57 });