expat.cxx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // file : examples/performance/expat.cxx
  2. // license : not copyrighted - public domain
  3. #include <iostream>
  4. #include <fstream>
  5. #include <expat.h>
  6. #include "time.hxx"
  7. using namespace std;
  8. const unsigned long iterations = 1000;
  9. unsigned long start_count;
  10. unsigned long end_count;
  11. void XMLCALL
  12. start_element (void* data, const XML_Char* ns_name, const XML_Char** atts)
  13. {
  14. start_count++;
  15. }
  16. void XMLCALL
  17. end_element (void* data, const XML_Char* ns_name)
  18. {
  19. end_count++;
  20. }
  21. void XMLCALL
  22. characters (void* data, const XML_Char* s, int n)
  23. {
  24. }
  25. int
  26. main (int argc, char* argv[])
  27. {
  28. if (argc != 2)
  29. {
  30. cerr << "usage: " << argv[0] << " <xml-file>" << endl;
  31. return 1;
  32. }
  33. try
  34. {
  35. ifstream ifs;
  36. ifs.exceptions (ios_base::failbit);
  37. ifs.open (argv[1], ios::in | ios::ate);
  38. size_t size (ifs.tellg ());
  39. ifs.seekg (0, ios::beg);
  40. char* buf = new char[size];
  41. ifs.read (buf, size);
  42. cerr << " document size: " << size << " bytes" << endl;
  43. // Warmup.
  44. //
  45. bool failed (false);
  46. for (unsigned long i (0); !failed && i < 10; ++i)
  47. {
  48. start_count = 0;
  49. end_count = 0;
  50. XML_Parser p (XML_ParserCreateNS (0, ' '));
  51. XML_SetStartElementHandler (p, start_element);
  52. XML_SetEndElementHandler (p, end_element);
  53. XML_SetCharacterDataHandler (p, characters);
  54. XML_Parse (p, buf, size, 1);
  55. XML_ParserFree (p);
  56. if (start_count != end_count)
  57. failed = true;
  58. }
  59. if (failed)
  60. {
  61. cerr << "failed" << endl;
  62. return 1;
  63. }
  64. cerr << " elements: " << start_count << endl;
  65. os::time start;
  66. for (unsigned long i (0); !failed && i < 1000; ++i)
  67. {
  68. start_count = 0;
  69. end_count = 0;
  70. XML_Parser p (XML_ParserCreateNS (0, ' '));
  71. XML_SetStartElementHandler (p, start_element);
  72. XML_SetEndElementHandler (p, end_element);
  73. XML_SetCharacterDataHandler (p, characters);
  74. XML_Parse (p, buf, size, 1);
  75. XML_ParserFree (p);
  76. if (start_count != end_count)
  77. failed = true;
  78. }
  79. os::time end;
  80. delete[] buf;
  81. if (failed)
  82. {
  83. cerr << "failed" << endl;
  84. return 1;
  85. }
  86. os::time time (end - start);
  87. double ms (time.sec () * 1000000ULL + time.nsec () / 1000ULL);
  88. cerr << " time: " << time << " sec" << endl;
  89. // Calculate throughput in documents/sec.
  90. //
  91. double tpd ((iterations / ms) * 1000000);
  92. cerr << " throughput: " << tpd << " documents/sec" << endl;
  93. // Calculate throughput in MBytes/sec.
  94. //
  95. double tpb (((size * iterations) / ms) * 1000000/(1024*1024));
  96. cerr << " throughput: " << tpb << " MBytes/sec" << endl;
  97. }
  98. catch (ios_base::failure const&)
  99. {
  100. cerr << "io failure" << endl;
  101. return 1;
  102. }
  103. }