multipartser-bench-multipart-parser.js 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. var assert = require('assert');
  2. var multipartser = require('multipartser'),
  3. boundary = '-----------------------------168072824752491622650073',
  4. parser = multipartser(),
  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. parser.boundary( boundary );
  16. parser.on( 'part', function ( part ) {
  17. });
  18. parser.on( 'end', function () {
  19. //console.log( 'completed parsing' );
  20. });
  21. parser.on( 'error', function ( error ) {
  22. console.error( error );
  23. });
  24. var start = +new Date(),
  25. nparsed = parser.data(buffer),
  26. nend = parser.end(),
  27. duration = +new Date - start,
  28. mbPerSec = (mb / (duration / 1000)).toFixed(2);
  29. console.log(mbPerSec+' mb/sec');
  30. //assert.equal(nparsed, buffer.length);
  31. function createMultipartBuffer(boundary, size) {
  32. var head =
  33. '--'+boundary+'\r\n'
  34. + 'content-disposition: form-data; name="field1"\r\n'
  35. + '\r\n'
  36. , tail = '\r\n--'+boundary+'--\r\n'
  37. , buffer = new Buffer(size);
  38. buffer.write(head, 'ascii', 0);
  39. buffer.write(tail, 'ascii', buffer.length - tail.length);
  40. return buffer;
  41. }
  42. process.on('exit', function() {
  43. /*for (var k in callbacks) {
  44. assert.equal(0, callbacks[k], k+' count off by '+callbacks[k]);
  45. }*/
  46. });