benchmark.cc 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. // Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <iostream>
  15. #include <stdlib.h>
  16. #include <string.h>
  17. #include <boost/lexical_cast.hpp>
  18. #include "benchmark.h"
  19. using namespace std;
  20. uBenchmark::uBenchmark(uint32_t iterations, const std::string& dbname,
  21. bool sync /*= false*/, bool verbose /*= true*/,
  22. const std::string& host /* = "" */,
  23. const std::string& user /* = "" */,
  24. const std::string& pass /* = "" */)
  25. :num_(iterations), sync_(sync), verbose_(verbose),
  26. hostname_(host), user_(user), passwd_(pass), dbname_(dbname),
  27. compiled_stmt_(true)
  28. {
  29. /// @todo: make compiled statements a configurable parameter
  30. /// @todo: convert this to user-configurable parameter
  31. hitratio_ = 0.9f;
  32. memset(ts_, 0, sizeof(ts_));
  33. }
  34. void uBenchmark::usage() {
  35. cout << "This is a benchmark designed to measure expected performance" << endl;
  36. cout << "of several backends. This backend identifies itself as:" << endl;
  37. printInfo();
  38. cout << endl << "Possible command-line parameters:" << endl;
  39. cout << " -h - help (you are reading this)" << endl;
  40. cout << " -m hostname - specifies MySQL server to connect (MySQL backend only)" << endl;
  41. cout << " -u username - specifies MySQL user name (MySQL backend only)" << endl;
  42. cout << " -p password - specifies MySQL passwod (MySQL backend only)" << endl;
  43. cout << " -f name - database or filename (MySQL, SQLite and memfile)" << endl;
  44. cout << " -n integer - number of test repetitions (MySQL, SQLite and memfile)" << endl;
  45. cout << " -s yes|no - synchronous/asynchronous operation (MySQL, SQLite and memfile)" << endl;
  46. cout << " -v yes|no - verbose mode (MySQL, SQLite and memfile)" << endl;
  47. exit(EXIT_FAILURE);
  48. }
  49. void uBenchmark::parseCmdline(int argc, char* const argv[]) {
  50. int ch;
  51. while ((ch = getopt(argc, argv, "hm:u:p:f:n:s:v:c:")) != -1) {
  52. switch (ch) {
  53. case 'h':
  54. usage();
  55. case 'm':
  56. hostname_ = string(optarg);
  57. break;
  58. case 'u':
  59. user_ = string(optarg);
  60. break;
  61. case 'p':
  62. passwd_ = string(optarg);
  63. break;
  64. case 'f':
  65. dbname_ = string(optarg);
  66. break;
  67. case 'n':
  68. try {
  69. num_ = boost::lexical_cast<int>(optarg);
  70. } catch (const boost::bad_lexical_cast &) {
  71. cerr << "Failed to iterations (-n option)." << endl;
  72. usage();
  73. }
  74. break;
  75. case 'c':
  76. compiled_stmt_ = !strcasecmp(optarg, "yes") || !strcmp(optarg, "1");
  77. break;
  78. case 's':
  79. sync_ = !strcasecmp(optarg, "yes") || !strcmp(optarg, "1");
  80. break;
  81. case 'v':
  82. verbose_ = !strcasecmp(optarg, "yes") || !strcmp(optarg, "1");
  83. break;
  84. default:
  85. usage();
  86. }
  87. }
  88. }
  89. void uBenchmark::failure(const char* operation) {
  90. cout << "Error during " << operation << endl;
  91. throw string(operation);
  92. }
  93. void uBenchmark::print_clock(const std::string& operation, uint32_t num,
  94. const struct timespec& before,
  95. const struct timespec& after) {
  96. long int tv_sec = after.tv_sec - before.tv_sec;
  97. long int tv_nsec = after.tv_nsec - before.tv_nsec;
  98. if (tv_nsec < 0) {
  99. tv_sec--;
  100. tv_nsec += 1000000000; // 10^9
  101. }
  102. double oneoper = (tv_nsec/1000 + tv_sec*1000000)/num;
  103. cout << operation << " repeated " << num << " times took "
  104. << tv_sec << " s, " << tv_nsec/1000 << " us, 1 operation took "
  105. << oneoper << "us (or " << (1000000/oneoper) << " oper/sec)" << endl;
  106. }
  107. int uBenchmark::run() {
  108. cout << "Starting test. Parameters:" << endl
  109. << "Number of iterations : " << num_ << endl
  110. << "Sync/async : " << (sync_ ? "sync" : "async") << endl
  111. << "Verbose : " << (verbose_ ? "verbose" : "quiet") << endl
  112. << "Compiled statements : " << (compiled_stmt_ ? "yes": "no") << endl
  113. << "Database name : " << dbname_ << endl
  114. << "MySQL hostname : " << hostname_ << endl
  115. << "MySQL username : " << user_ << endl
  116. << "MySQL password : " << passwd_ << endl << endl;
  117. srandom(time(NULL));
  118. try {
  119. connect();
  120. clock_gettime(CLOCK_REALTIME, &ts_[0]);
  121. createLease4Test();
  122. clock_gettime(CLOCK_REALTIME, &ts_[1]);
  123. searchLease4Test();
  124. clock_gettime(CLOCK_REALTIME, &ts_[2]);
  125. updateLease4Test();
  126. clock_gettime(CLOCK_REALTIME, &ts_[3]);
  127. deleteLease4Test();
  128. clock_gettime(CLOCK_REALTIME, &ts_[4]);
  129. disconnect();
  130. } catch (const std::string& e) {
  131. cout << "Failed: " << e << endl;
  132. return (-1);
  133. }
  134. print_clock("Create leases4", num_, ts_[0], ts_[1]);
  135. print_clock("Search leases4", num_, ts_[1], ts_[2]);
  136. print_clock("Update leases4", num_, ts_[2], ts_[3]);
  137. print_clock("Delete leases4", num_, ts_[3], ts_[4]);
  138. return (0);
  139. }