driver.cxx 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // file : examples/performance/driver.cxx
  2. // copyright : not copyrighted - public domain
  3. #include <string>
  4. #include <fstream>
  5. #include <iostream>
  6. #include <xml/parser>
  7. #include "time.hxx"
  8. using namespace std;
  9. using namespace xml;
  10. const unsigned long iterations = 1000;
  11. int
  12. main (int argc, char* argv[])
  13. {
  14. if (argc != 2)
  15. {
  16. cerr << "usage: " << argv[0] << " <xml-file>" << endl;
  17. return 1;
  18. }
  19. try
  20. {
  21. ifstream ifs;
  22. ifs.exceptions (ios_base::failbit);
  23. ifs.open (argv[1], ios::in | ios::ate);
  24. size_t size (ifs.tellg ());
  25. ifs.seekg (0, ios::beg);
  26. char* buf = new char[size];
  27. ifs.read (buf, size);
  28. cerr << " document size: " << size << " bytes" << endl;
  29. unsigned long start_count;
  30. unsigned long end_count;
  31. // Warmup.
  32. //
  33. for (unsigned long i (0); i < 10; ++i)
  34. {
  35. start_count = 0;
  36. end_count = 0;
  37. // Configure the parser to receive attributes as events without
  38. // creating the attribute map.
  39. //
  40. parser p (buf,
  41. size,
  42. argv[1],
  43. parser::receive_default | parser::receive_attributes_event);
  44. for (parser::event_type e (p.next ()); e != parser::eof; e = p.next ())
  45. {
  46. switch (e)
  47. {
  48. case parser::start_element:
  49. start_count++;
  50. break;
  51. case parser::end_element:
  52. end_count++;
  53. break;
  54. default:
  55. break;
  56. }
  57. }
  58. }
  59. cerr << " elements: " << start_count << endl;
  60. os::time start;
  61. for (unsigned long i (0); i < iterations; ++i)
  62. {
  63. start_count = 0;
  64. end_count = 0;
  65. // Configure the parser to receive attributes as events without
  66. // creating the attribute map.
  67. //
  68. parser p (buf,
  69. size,
  70. argv[1],
  71. parser::receive_default | parser::receive_attributes_event);
  72. for (parser::event_type e (p.next ()); e != parser::eof; e = p.next ())
  73. {
  74. switch (e)
  75. {
  76. case parser::start_element:
  77. start_count++;
  78. break;
  79. case parser::end_element:
  80. end_count++;
  81. break;
  82. default:
  83. break;
  84. }
  85. }
  86. }
  87. os::time end;
  88. delete[] buf;
  89. os::time time (end - start);
  90. double ms (
  91. static_cast<double> (
  92. time.sec () * 1000000ULL + time.nsec () / 1000ULL));
  93. cerr << " time: " << time << " sec" << endl;
  94. // Calculate throughput in documents/sec.
  95. //
  96. double tpd ((iterations / ms) * 1000000);
  97. cerr << " throughput: " << tpd << " documents/sec" << endl;
  98. // Calculate throughput in MBytes/sec.
  99. //
  100. double tpb (((size * iterations) / ms) * 1000000/(1024*1024));
  101. cerr << " throughput: " << tpb << " MBytes/sec" << endl;
  102. }
  103. catch (const ios_base::failure&)
  104. {
  105. cerr << "io failure" << endl;
  106. return 1;
  107. }
  108. catch (const xml::exception& e)
  109. {
  110. cerr << e.what () << endl;
  111. return 1;
  112. }
  113. }