dicer-bench-multipart-parser.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. var assert = require('assert');
  2. var Dicer = require('..'),
  3. boundary = '-----------------------------168072824752491622650073',
  4. d = new Dicer({ boundary: boundary }),
  5. mb = 100,
  6. buffer = createMultipartBuffer(boundary, mb * 1024 * 1024),
  7. callbacks =
  8. { partBegin: -1,
  9. partEnd: -1,
  10. headerField: -1,
  11. headerValue: -1,
  12. partData: -1,
  13. end: -1,
  14. };
  15. d.on('part', function(p) {
  16. callbacks.partBegin++;
  17. p.on('header', function(header) {
  18. /*for (var h in header)
  19. console.log('Part header: k: ' + inspect(h) + ', v: ' + inspect(header[h]));*/
  20. });
  21. p.on('data', function(data) {
  22. callbacks.partData++;
  23. //console.log('Part data: ' + inspect(data.toString()));
  24. });
  25. p.on('end', function() {
  26. //console.log('End of part\n');
  27. callbacks.partEnd++;
  28. });
  29. });
  30. d.on('end', function() {
  31. //console.log('End of parts');
  32. callbacks.end++;
  33. });
  34. var start = +new Date(),
  35. nparsed = d.write(buffer),
  36. duration = +new Date - start,
  37. mbPerSec = (mb / (duration / 1000)).toFixed(2);
  38. console.log(mbPerSec+' mb/sec');
  39. //assert.equal(nparsed, buffer.length);
  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 = new Buffer(size);
  47. buffer.write(head, 'ascii', 0);
  48. buffer.write(tail, 'ascii', buffer.length - tail.length);
  49. return buffer;
  50. }
  51. process.on('exit', function() {
  52. /*for (var k in callbacks) {
  53. assert.equal(0, callbacks[k], k+' count off by '+callbacks[k]);
  54. }*/
  55. });