profile.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. //node --prof --prof_auto profile.js
  2. //deps/v8/tools/mac-tick-processor v8.log
  3. var sys = require("sys");
  4. var fs = require("fs");
  5. var http = require("http");
  6. var htmlparser = require("./lib/htmlparser");
  7. //var libxml = require('./libxmljs');
  8. var testNHP = true; //Should node-htmlparser be exercised?
  9. var testLXJS = false; //Should libxmljs be exercised?
  10. var testIterations = 100; //Number of test loops to run
  11. var testHost = "localhost"; //Host to fetch test HTML from
  12. var testPort = 80; //Port on host to fetch test HTML from
  13. var testPath = "/~chris/feed.xml"; //Path on host to fetch HTML from
  14. function getMillisecs () {
  15. return((new Date()).getTime());
  16. }
  17. function timeExecutions (loops, func) {
  18. var start = getMillisecs();
  19. while (loops--)
  20. func();
  21. return(getMillisecs() - start);
  22. }
  23. var html = "";
  24. http.createClient(testPort, testHost)
  25. .request("GET", testPath, { host: testHost })
  26. .addListener("response", function (response) {
  27. if (response.statusCode == "200") {
  28. response.setEncoding("utf8");
  29. response.addListener("data", function (chunk) {
  30. html += chunk;
  31. }).addListener("end", function() {
  32. var timeNodeHtmlParser = !testNHP ? 0 : timeExecutions(testIterations, function () {
  33. var handler = new htmlparser.DefaultHandler(function(err, dom) {
  34. if (err)
  35. sys.debug("Error: " + err);
  36. });
  37. var parser = new htmlparser.Parser(handler, { includeLocation: true });
  38. parser.parseComplete(html);
  39. })
  40. var timeLibXmlJs = !testLXJS ? 0 : timeExecutions(testIterations, function () {
  41. var dom = libxml.parseHtmlString(html);
  42. })
  43. if (testNHP)
  44. sys.debug("NodeHtmlParser: " + timeNodeHtmlParser);
  45. if (testLXJS)
  46. sys.debug("LibXmlJs: " + timeLibXmlJs);
  47. if (testNHP && testLXJS)
  48. sys.debug("Difference: " + ((timeNodeHtmlParser - timeLibXmlJs) / timeLibXmlJs) * 100);
  49. });
  50. }
  51. else
  52. sys.debug("Error: got response status " + response.statusCode);
  53. })
  54. .end();