You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

71 lines
1.6 KiB

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