benchmark.cc 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. {
  28. /// @todo: convert this to user-configurable parameter
  29. hitratio_ = 0.9f;
  30. memset(ts_, 0, sizeof(ts_));
  31. }
  32. void uBenchmark::usage() {
  33. cout << "This is a benchmark designed to measure expected performance" << endl;
  34. cout << "of several backends. This backend identifies itself as:" << endl;
  35. printInfo();
  36. cout << endl << "Possible command-line parameters:" << endl;
  37. cout << " -h - help (you are reading this)" << endl;
  38. cout << " -m hostname - specifies MySQL server to connect (MySQL backend only)" << endl;
  39. cout << " -u username - specifies MySQL user name (MySQL backend only)" << endl;
  40. cout << " -p password - specifies MySQL passwod (MySQL backend only)" << endl;
  41. cout << " -f name - database or filename (MySQL, SQLite and memfile)" << endl;
  42. cout << " -n integer - number of test repetitions (MySQL, SQLite and memfile)" << endl;
  43. cout << " -s yes|no - synchronous/asynchronous operation (MySQL, SQLite and memfile)" << endl;
  44. cout << " -v yes|no - verbose mode (MySQL, SQLite and memfile)" << endl;
  45. exit(EXIT_FAILURE);
  46. }
  47. void uBenchmark::parseCmdline(int argc, char* const argv[]) {
  48. int ch;
  49. while ((ch = getopt(argc, argv, "hm:u:p:f:n:s:v:")) != -1) {
  50. switch (ch) {
  51. case 'h':
  52. usage();
  53. case 'm':
  54. hostname_ = string(optarg);
  55. break;
  56. case 'u':
  57. user_ = string(optarg);
  58. break;
  59. case 'p':
  60. passwd_ = string(optarg);
  61. break;
  62. case 'f':
  63. dbname_ = string(optarg);
  64. break;
  65. case 'n':
  66. try {
  67. num_ = boost::lexical_cast<int>(optarg);
  68. } catch (const boost::bad_lexical_cast &) {
  69. cerr << "Failed to iterations (-n option)." << endl;
  70. usage();
  71. }
  72. break;
  73. case 's':
  74. sync_ = !strcasecmp(optarg, "yes") || !strcmp(optarg, "1");
  75. break;
  76. case 'v':
  77. verbose_ = !strcasecmp(optarg, "yes") || !strcmp(optarg, "1");
  78. break;
  79. default:
  80. usage();
  81. }
  82. }
  83. }
  84. void uBenchmark::failure(const char* operation) {
  85. cout << "Error during " << operation << endl;
  86. throw string(operation);
  87. }
  88. void uBenchmark::print_clock(const std::string& operation, uint32_t num,
  89. const struct timespec& before,
  90. const struct timespec& after) {
  91. long int tv_sec = after.tv_sec - before.tv_sec;
  92. long int tv_nsec = after.tv_nsec - before.tv_nsec;
  93. if (tv_nsec < 0) {
  94. tv_sec--;
  95. tv_nsec += 1000000000; // 10^9
  96. }
  97. double oneoper = (tv_nsec/1000 + tv_sec*1000000)/num;
  98. cout << operation << " repeated " << num << " times took "
  99. << tv_sec << " s, " << tv_nsec/1000 << " us, 1 operation took "
  100. << oneoper << "us (or " << (1000000/oneoper) << " oper/sec)" << endl;
  101. }
  102. int uBenchmark::run() {
  103. cout << "Starting test. Parameters:" << endl
  104. << "Number of iterations : " << num_ << endl
  105. << "Sync/async : " << (sync_ ? "sync" : "async") << endl
  106. << "Verbose : " << (verbose_ ? "verbose" : "quiet") << endl
  107. << "Database name : " << dbname_ << endl
  108. << "MySQL hostname : " << hostname_ << endl
  109. << "MySQL username : " << user_ << endl
  110. << "MySQL password : " << passwd_ << endl << endl;
  111. srandom(time(NULL));
  112. try {
  113. connect();
  114. clock_gettime(CLOCK_REALTIME, &ts_[0]);
  115. createLease4Test();
  116. clock_gettime(CLOCK_REALTIME, &ts_[1]);
  117. searchLease4Test();
  118. clock_gettime(CLOCK_REALTIME, &ts_[2]);
  119. updateLease4Test();
  120. clock_gettime(CLOCK_REALTIME, &ts_[3]);
  121. deleteLease4Test();
  122. clock_gettime(CLOCK_REALTIME, &ts_[4]);
  123. disconnect();
  124. } catch (const std::string& e) {
  125. cout << "Failed: " << e << endl;
  126. return (-1);
  127. }
  128. print_clock("Create leases4", num_, ts_[0], ts_[1]);
  129. print_clock("Search leases4", num_, ts_[1], ts_[2]);
  130. print_clock("Update leases4", num_, ts_[2], ts_[3]);
  131. print_clock("Delete leases4", num_, ts_[3], ts_[4]);
  132. return (0);
  133. }