logsysbench.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) 2008, 2009 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake (sdake@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <config.h>
  35. #include <stdio.h>
  36. #include <stdint.h>
  37. #include <string.h>
  38. #include <sys/time.h>
  39. #include <time.h>
  40. #include <corosync/engine/logsys.h>
  41. LOGSYS_DECLARE_SYSTEM ("logtest_rec",
  42. LOGSYS_MODE_OUTPUT_STDERR | LOGSYS_MODE_THREADED,
  43. 0, /* debug */
  44. NULL,
  45. LOGSYS_LEVEL_INFO, /* logfile_priority */
  46. LOG_DAEMON, /* syslog facility */
  47. LOGSYS_LEVEL_INFO, /* syslog level */
  48. NULL, /* use default format */
  49. 1000000); /* flight recorder size */
  50. #define LOGREC_ID_CHECKPOINT_CREATE 2
  51. #define LOGREC_ARGS_CHECKPOINT_CREATE 2
  52. #define ITERATIONS 1000000
  53. static struct timeval tv1, tv2, tv_elapsed;
  54. #define timersub(a, b, result) \
  55. do { \
  56. (result)->tv_sec = (a)->tv_sec - (b)->tv_sec; \
  57. (result)->tv_usec = (a)->tv_usec - (b)->tv_usec; \
  58. if ((result)->tv_usec < 0) { \
  59. --(result)->tv_sec; \
  60. (result)->tv_usec += 1000000; \
  61. } \
  62. } while (0)
  63. static void bm_start (void)
  64. {
  65. gettimeofday (&tv1, NULL);
  66. }
  67. static void bm_finish (const char *operation)
  68. {
  69. gettimeofday (&tv2, NULL);
  70. timersub (&tv2, &tv1, &tv_elapsed);
  71. if (strlen (operation) > 22) {
  72. printf ("%s\t\t", operation);
  73. } else {
  74. printf ("%s\t\t\t", operation);
  75. }
  76. printf ("%9.3f operations/sec\n",
  77. ((float)ITERATIONS) / (tv_elapsed.tv_sec + (tv_elapsed.tv_usec / 1000000.0)));
  78. }
  79. static char buffer[256];
  80. int main (void)
  81. {
  82. int i;
  83. char buf[1024];
  84. printf ("heating up cache with logrec functionality\n");
  85. for (i = 0; i < ITERATIONS; i++) {
  86. log_rec (LOGREC_ID_CHECKPOINT_CREATE,
  87. "recordA", 8, "recordB", 8, LOGSYS_REC_END);
  88. }
  89. bm_start();
  90. for (i = 0; i < ITERATIONS; i++) {
  91. log_rec (LOGREC_ID_CHECKPOINT_CREATE,
  92. buffer, 7, LOGSYS_REC_END);
  93. }
  94. bm_finish ("log_rec 1 arguments:");
  95. bm_start();
  96. for (i = 0; i < ITERATIONS; i++) {
  97. log_rec (LOGREC_ID_CHECKPOINT_CREATE,
  98. "recordA", 8, LOGSYS_REC_END);
  99. }
  100. bm_finish ("log_rec 2 arguments:");
  101. bm_start();
  102. for (i = 0; i < 10; i++) {
  103. log_rec (LOGREC_ID_CHECKPOINT_CREATE,
  104. "recordA", 8, "recordB", 8, LOGSYS_REC_END);
  105. }
  106. bm_start();
  107. for (i = 0; i < ITERATIONS; i++) {
  108. log_rec (LOGREC_ID_CHECKPOINT_CREATE,
  109. "recordA", 8, "recordB", 8, "recordC", 8, LOGSYS_REC_END);
  110. }
  111. bm_finish ("log_rec 3 arguments:");
  112. bm_start();
  113. for (i = 0; i < ITERATIONS; i++) {
  114. log_rec (LOGREC_ID_CHECKPOINT_CREATE,
  115. "recordA", 8, "recordB", 8, "recordC", 8, "recordD", 8, LOGSYS_REC_END);
  116. }
  117. bm_finish ("log_rec 4 arguments:");
  118. /*
  119. * sprintf testing
  120. */
  121. printf ("heating up cache with sprintf functionality\n");
  122. for (i = 0; i < ITERATIONS; i++) {
  123. snprintf (buf, sizeof(buf), "Some logging information %s", "recordA");
  124. }
  125. bm_start();
  126. for (i = 0; i < ITERATIONS; i++) {
  127. snprintf (buf, sizeof(buf), "Some logging information %s", "recordA");
  128. }
  129. bm_finish ("sprintf 1 argument:");
  130. bm_start();
  131. for (i = 0; i < ITERATIONS; i++) {
  132. sprintf (buf, "Some logging information %s %s", "recordA", "recordB");
  133. }
  134. bm_finish ("sprintf 2 arguments:");
  135. bm_start();
  136. for (i = 0; i < ITERATIONS; i++) {
  137. sprintf (buf, "Some logging information %s %s %s", "recordA", "recordB", "recordC");
  138. }
  139. bm_finish ("sprintf 3 arguments:");
  140. bm_start();
  141. for (i = 0; i < ITERATIONS; i++) {
  142. sprintf (buf, "Some logging information %s %s %s %s", "recordA", "recordB", "recordC", "recordD");
  143. }
  144. bm_finish ("sprintf 4 arguments:");
  145. bm_start();
  146. for (i = 0; i < ITERATIONS; i++) {
  147. sprintf (buf, "Some logging information %s %s %s %d", "recordA", "recordB", "recordC", i);
  148. }
  149. bm_finish ("sprintf 4 arguments (1 int):");
  150. logsys_log_rec_store ("fdata");
  151. /* TODO
  152. currently fails under some circumstances
  153. bm_start();
  154. for (i = 0; i < ITERATIONS; i++) {
  155. log_printf (LOGSYS_LEVEL_NOTICE, "test %d", i);
  156. }
  157. bm_finish("log_printf");
  158. */
  159. return (0);
  160. }