Tizen 2.0 Release
[platform/framework/web/web-ui-fw.git] / libs / js / jquery-mobile-1.2.0 / node_modules / grunt / node_modules / connect / node_modules / formidable / benchmark / bench-multipart-parser.js
1 require('../test/common');
2 var multipartParser = require('../lib/multipart_parser'),
3     MultipartParser = multipartParser.MultipartParser,
4     parser = new MultipartParser(),
5     Buffer = require('buffer').Buffer,
6     boundary = '-----------------------------168072824752491622650073',
7     mb = 100,
8     buffer = createMultipartBuffer(boundary, mb * 1024 * 1024),
9     callbacks =
10       { partBegin: -1,
11         partEnd: -1,
12         headerField: -1,
13         headerValue: -1,
14         partData: -1,
15         end: -1,
16       };
17
18
19 parser.initWithBoundary(boundary);
20 parser.onHeaderField = function() {
21   callbacks.headerField++;
22 };
23
24 parser.onHeaderValue = function() {
25   callbacks.headerValue++;
26 };
27
28 parser.onPartBegin = function() {
29   callbacks.partBegin++;
30 };
31
32 parser.onPartData = function() {
33   callbacks.partData++;
34 };
35
36 parser.onPartEnd = function() {
37   callbacks.partEnd++;
38 };
39
40 parser.onEnd = function() {
41   callbacks.end++;
42 };
43
44 var start = +new Date(),
45     nparsed = parser.write(buffer),
46     duration = +new Date - start,
47     mbPerSec = (mb / (duration / 1000)).toFixed(2);
48
49 console.log(mbPerSec+' mb/sec');
50
51 assert.equal(nparsed, buffer.length);
52
53 function createMultipartBuffer(boundary, size) {
54   var head =
55         '--'+boundary+'\r\n'
56       + 'content-disposition: form-data; name="field1"\r\n'
57       + '\r\n'
58     , tail = '\r\n--'+boundary+'--\r\n'
59     , buffer = new Buffer(size);
60
61   buffer.write(head, 'ascii', 0);
62   buffer.write(tail, 'ascii', buffer.length - tail.length);
63   return buffer;
64 }
65
66 process.on('exit', function() {
67   for (var k in callbacks) {
68     assert.equal(0, callbacks[k], k+' count off by '+callbacks[k]);
69   }
70 });