benchmark.cc 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 <string.h>
  16. #include "benchmark.h"
  17. using namespace std;
  18. uBenchmark::uBenchmark(uint32_t iterations)
  19. :Num_(iterations) {
  20. memset(ts, 0, sizeof(ts));
  21. }
  22. void uBenchmark::failure(const char* operation) {
  23. cout << "Error during " << operation << endl;
  24. throw string(operation);
  25. }
  26. void uBenchmark::print_clock(const std::string& operation, uint32_t num,
  27. const struct timespec& before,
  28. const struct timespec& after) {
  29. long int tv_sec = after.tv_sec - before.tv_sec;
  30. long int tv_nsec = after.tv_nsec - before.tv_nsec;
  31. if (tv_nsec < 0) {
  32. tv_sec--;
  33. tv_nsec += 1000000000; // 10^9
  34. }
  35. double oneoper = (tv_nsec/1000 + tv_sec*1000000)/num;
  36. cout << "Operation " << operation << " repeated " << num << " times took "
  37. << tv_sec << " seconds, " << tv_nsec/1000 << " us, 1 operation took "
  38. << oneoper << "us (or " << (1000000/oneoper) << " oper/sec)" << endl;
  39. }
  40. int uBenchmark::run() {
  41. try {
  42. connect();
  43. clock_gettime(CLOCK_REALTIME, &ts[0]);
  44. createLease4Test();
  45. clock_gettime(CLOCK_REALTIME, &ts[1]);
  46. searchLease4Test();
  47. clock_gettime(CLOCK_REALTIME, &ts[2]);
  48. updateLease4Test();
  49. clock_gettime(CLOCK_REALTIME, &ts[3]);
  50. deleteLease4Test();
  51. clock_gettime(CLOCK_REALTIME, &ts[4]);
  52. disconnect();
  53. } catch (const std::string& e) {
  54. cout << "Failed: " << e << endl;
  55. return (-1);
  56. }
  57. print_clock("Create leases4", Num_, ts[0], ts[1]);
  58. print_clock("Search leases4", Num_, ts[1], ts[2]);
  59. print_clock("Update leases4", Num_, ts[2], ts[3]);
  60. print_clock("Delete leases4", Num_, ts[3], ts[4]);
  61. return (0);
  62. }