benchmark.cc 2.5 KB

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