parted-bench-multipart-parser.js 1.5 KB

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