test-timer-performance.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. /*
  2. * Test program which measures the time it takes to schedule and
  3. * remove timers.
  4. *
  5. * Copyright (C) 2013 by Open Source Routing.
  6. * Copyright (C) 2013 by Internet Systems Consortium, Inc. ("ISC")
  7. *
  8. * This file is part of Quagga
  9. *
  10. * Quagga is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the
  12. * Free Software Foundation; either version 2, or (at your option) any
  13. * later version.
  14. *
  15. * Quagga is distributed in the hope that it will be useful, but
  16. * WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18. * General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with Quagga; see the file COPYING. If not, write to the Free
  22. * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
  23. * 02111-1307, USA.
  24. */
  25. #include <zebra.h>
  26. #include <stdio.h>
  27. #include <unistd.h>
  28. #include "thread.h"
  29. #include "pqueue.h"
  30. #include "prng.h"
  31. #define SCHEDULE_TIMERS 1000000
  32. #define REMOVE_TIMERS 500000
  33. struct thread_master *master;
  34. static int dummy_func(struct thread *thread)
  35. {
  36. return 0;
  37. }
  38. int main(int argc, char **argv)
  39. {
  40. struct prng *prng;
  41. int i;
  42. struct thread **timers;
  43. struct timeval tv_start, tv_lap, tv_stop;
  44. unsigned long t_schedule, t_remove;
  45. master = thread_master_create();
  46. prng = prng_new(0);
  47. timers = calloc(SCHEDULE_TIMERS, sizeof(*timers));
  48. /* create thread structures so they won't be allocated during the
  49. * time measurement */
  50. for (i = 0; i < SCHEDULE_TIMERS; i++)
  51. timers[i] = thread_add_timer_msec(master, dummy_func, NULL, 0);
  52. for (i = 0; i < SCHEDULE_TIMERS; i++)
  53. thread_cancel(timers[i]);
  54. quagga_gettime(QUAGGA_CLK_MONOTONIC, &tv_start);
  55. for (i = 0; i < SCHEDULE_TIMERS; i++)
  56. {
  57. long interval_msec;
  58. interval_msec = prng_rand(prng) % (100 * SCHEDULE_TIMERS);
  59. timers[i] = thread_add_timer_msec(master, dummy_func,
  60. NULL, interval_msec);
  61. }
  62. quagga_gettime(QUAGGA_CLK_MONOTONIC, &tv_lap);
  63. for (i = 0; i < REMOVE_TIMERS; i++)
  64. {
  65. int index;
  66. index = prng_rand(prng) % SCHEDULE_TIMERS;
  67. if (timers[index])
  68. thread_cancel(timers[index]);
  69. timers[index] = NULL;
  70. }
  71. quagga_gettime(QUAGGA_CLK_MONOTONIC, &tv_stop);
  72. t_schedule = 1000 * (tv_lap.tv_sec - tv_start.tv_sec);
  73. t_schedule += (tv_lap.tv_usec - tv_start.tv_usec) / 1000;
  74. t_remove = 1000 * (tv_stop.tv_sec - tv_lap.tv_sec);
  75. t_remove += (tv_stop.tv_usec - tv_lap.tv_usec) / 1000;
  76. printf("Scheduling %d random timers took %ld.%03ld seconds.\n",
  77. SCHEDULE_TIMERS, t_schedule/1000, t_schedule%1000);
  78. printf("Removing %d random timers took %ld.%03ld seconds.\n",
  79. REMOVE_TIMERS, t_remove/1000, t_remove%1000);
  80. fflush(stdout);
  81. free(timers);
  82. thread_master_free(master);
  83. prng_free(prng);
  84. return 0;
  85. }