formidable-bench-multipart-parser.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. var assert = require('assert');
  2. require('../node_modules/formidable/test/common');
  3. var multipartParser = require('../node_modules/formidable/lib/multipart_parser'),
  4. MultipartParser = multipartParser.MultipartParser,
  5. parser = new MultipartParser(),
  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. parser.initWithBoundary(boundary);
  18. parser.onHeaderField = function() {
  19. callbacks.headerField++;
  20. };
  21. parser.onHeaderValue = function() {
  22. callbacks.headerValue++;
  23. };
  24. parser.onPartBegin = function() {
  25. callbacks.partBegin++;
  26. };
  27. parser.onPartData = function() {
  28. callbacks.partData++;
  29. };
  30. parser.onPartEnd = function() {
  31. callbacks.partEnd++;
  32. };
  33. parser.onEnd = function() {
  34. callbacks.end++;
  35. };
  36. var start = +new Date(),
  37. nparsed = parser.write(buffer),
  38. duration = +new Date - start,
  39. mbPerSec = (mb / (duration / 1000)).toFixed(2);
  40. console.log(mbPerSec+' mb/sec');
  41. //assert.equal(nparsed, buffer.length);
  42. function createMultipartBuffer(boundary, size) {
  43. var head =
  44. '--'+boundary+'\r\n'
  45. + 'content-disposition: form-data; name="field1"\r\n'
  46. + '\r\n'
  47. , tail = '\r\n--'+boundary+'--\r\n'
  48. , buffer = new Buffer(size);
  49. buffer.write(head, 'ascii', 0);
  50. buffer.write(tail, 'ascii', buffer.length - tail.length);
  51. return buffer;
  52. }
  53. process.on('exit', function() {
  54. /*for (var k in callbacks) {
  55. assert.equal(0, callbacks[k], k+' count off by '+callbacks[k]);
  56. }*/
  57. });