multiparty-bench-multipart-parser.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. var assert = require('assert'),
  2. Form = require('multiparty').Form,
  3. boundary = '-----------------------------168072824752491622650073',
  4. mb = 100,
  5. buffer = createMultipartBuffer(boundary, mb * 1024 * 1024),
  6. callbacks =
  7. { partBegin: -1,
  8. partEnd: -1,
  9. headerField: -1,
  10. headerValue: -1,
  11. partData: -1,
  12. end: -1,
  13. };
  14. var form = new Form({ boundary: boundary });
  15. hijack('onParseHeaderField', function() {
  16. callbacks.headerField++;
  17. });
  18. hijack('onParseHeaderValue', function() {
  19. callbacks.headerValue++;
  20. });
  21. hijack('onParsePartBegin', function() {
  22. callbacks.partBegin++;
  23. });
  24. hijack('onParsePartData', function() {
  25. callbacks.partData++;
  26. });
  27. hijack('onParsePartEnd', function() {
  28. callbacks.partEnd++;
  29. });
  30. form.on('finish', function() {
  31. callbacks.end++;
  32. });
  33. var start = new Date();
  34. form.write(buffer, function(err) {
  35. var duration = new Date() - start;
  36. assert.ifError(err);
  37. var mbPerSec = (mb / (duration / 1000)).toFixed(2);
  38. console.log(mbPerSec+' mb/sec');
  39. });
  40. //assert.equal(nparsed, buffer.length);
  41. function createMultipartBuffer(boundary, size) {
  42. var head =
  43. '--'+boundary+'\r\n'
  44. + 'content-disposition: form-data; name="field1"\r\n'
  45. + '\r\n'
  46. , tail = '\r\n--'+boundary+'--\r\n'
  47. , buffer = new Buffer(size);
  48. buffer.write(head, 'ascii', 0);
  49. buffer.write(tail, 'ascii', buffer.length - tail.length);
  50. return buffer;
  51. }
  52. process.on('exit', function() {
  53. /*for (var k in callbacks) {
  54. assert.equal(0, callbacks[k], k+' count off by '+callbacks[k]);
  55. }*/
  56. });
  57. function hijack(name, fn) {
  58. var oldFn = form[name];
  59. form[name] = function() {
  60. fn();
  61. return oldFn.apply(this, arguments);
  62. };
  63. }