benchmark.cc 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 "benchmark.h"
  18. using namespace std;
  19. uBenchmark::uBenchmark(uint32_t iterations, const std::string& dbname,
  20. bool sync /*= false*/, bool verbose /*= true*/,
  21. const std::string& host /* = "" */,
  22. const std::string& user /* = "" */,
  23. const std::string& pass /* = "" */)
  24. :Num_(iterations), Sync_(sync), Verbose_(verbose),
  25. Hostname_(host), User_(user), Passwd_(pass), DBName_(dbname)
  26. {
  27. memset(ts, 0, sizeof(ts));
  28. }
  29. bool uBenchmark::parseCmdline(int args, const char* argv[]) {
  30. return false;
  31. }
  32. void uBenchmark::failure(const char* operation) {
  33. cout << "Error during " << operation << endl;
  34. throw string(operation);
  35. }
  36. void uBenchmark::print_clock(const std::string& operation, uint32_t num,
  37. const struct timespec& before,
  38. const struct timespec& after) {
  39. long int tv_sec = after.tv_sec - before.tv_sec;
  40. long int tv_nsec = after.tv_nsec - before.tv_nsec;
  41. if (tv_nsec < 0) {
  42. tv_sec--;
  43. tv_nsec += 1000000000; // 10^9
  44. }
  45. double oneoper = (tv_nsec/1000 + tv_sec*1000000)/num;
  46. cout << "Operation " << operation << " repeated " << num << " times took "
  47. << tv_sec << " seconds, " << tv_nsec/1000 << " us, 1 operation took "
  48. << oneoper << "us (or " << (1000000/oneoper) << " oper/sec)" << endl;
  49. }
  50. int uBenchmark::run() {
  51. srandom(time(NULL));
  52. try {
  53. connect();
  54. clock_gettime(CLOCK_REALTIME, &ts[0]);
  55. createLease4Test();
  56. clock_gettime(CLOCK_REALTIME, &ts[1]);
  57. searchLease4Test();
  58. clock_gettime(CLOCK_REALTIME, &ts[2]);
  59. updateLease4Test();
  60. clock_gettime(CLOCK_REALTIME, &ts[3]);
  61. deleteLease4Test();
  62. clock_gettime(CLOCK_REALTIME, &ts[4]);
  63. disconnect();
  64. } catch (const std::string& e) {
  65. cout << "Failed: " << e << endl;
  66. return (-1);
  67. }
  68. print_clock("Create leases4", Num_, ts[0], ts[1]);
  69. print_clock("Search leases4", Num_, ts[1], ts[2]);
  70. print_clock("Update leases4", Num_, ts[2], ts[3]);
  71. print_clock("Delete leases4", Num_, ts[3], ts[4]);
  72. return (0);
  73. }