benchtest5.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * benchtest5.c
  3. *
  4. *
  5. * --------------------------------------------------------------------------
  6. *
  7. * Pthreads-win32 - POSIX Threads Library for Win32
  8. * Copyright(C) 1998 John E. Bossom
  9. * Copyright(C) 1999,2005 Pthreads-win32 contributors
  10. *
  11. * Contact Email: [email protected]
  12. *
  13. * The current list of contributors is contained
  14. * in the file CONTRIBUTORS included with the source
  15. * code distribution. The list can also be seen at the
  16. * following World Wide Web location:
  17. * http://sources.redhat.com/pthreads-win32/contributors.html
  18. *
  19. * This library is free software; you can redistribute it and/or
  20. * modify it under the terms of the GNU Lesser General Public
  21. * License as published by the Free Software Foundation; either
  22. * version 2 of the License, or (at your option) any later version.
  23. *
  24. * This library is distributed in the hope that it will be useful,
  25. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  26. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  27. * Lesser General Public License for more details.
  28. *
  29. * You should have received a copy of the GNU Lesser General Public
  30. * License along with this library in the file COPYING.LIB;
  31. * if not, write to the Free Software Foundation, Inc.,
  32. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  33. *
  34. * --------------------------------------------------------------------------
  35. *
  36. * Measure time taken to complete an elementary operation.
  37. *
  38. * - Semaphore
  39. * Single thread iteration over post/wait for a semaphore.
  40. */
  41. #include "test.h"
  42. #include <sys/timeb.h>
  43. #ifdef __GNUC__
  44. #include <stdlib.h>
  45. #endif
  46. #include "benchtest.h"
  47. #define ITERATIONS 1000000L
  48. sem_t sema;
  49. HANDLE w32sema;
  50. PTW32_STRUCT_TIMEB currSysTimeStart;
  51. PTW32_STRUCT_TIMEB currSysTimeStop;
  52. long durationMilliSecs;
  53. long overHeadMilliSecs = 0;
  54. int one = 1;
  55. int zero = 0;
  56. #define GetDurationMilliSecs(_TStart, _TStop) ((long)((_TStop.time*1000+_TStop.millitm) \
  57. - (_TStart.time*1000+_TStart.millitm)))
  58. /*
  59. * Dummy use of j, otherwise the loop may be removed by the optimiser
  60. * when doing the overhead timing with an empty loop.
  61. */
  62. #define TESTSTART \
  63. { int i, j = 0, k = 0; PTW32_FTIME(&currSysTimeStart); for (i = 0; i < ITERATIONS; i++) { j++;
  64. #define TESTSTOP \
  65. }; PTW32_FTIME(&currSysTimeStop); if (j + k == i) j++; }
  66. void
  67. reportTest (char * testNameString)
  68. {
  69. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  70. printf( "%-45s %15ld %15.3f\n",
  71. testNameString,
  72. durationMilliSecs,
  73. (float) durationMilliSecs * 1E3 / ITERATIONS);
  74. }
  75. int
  76. main (int argc, char *argv[])
  77. {
  78. printf( "=============================================================================\n");
  79. printf( "\nOperations on a semaphore.\n%ld iterations\n\n",
  80. ITERATIONS);
  81. printf( "%-45s %15s %15s\n",
  82. "Test",
  83. "Total(msec)",
  84. "average(usec)");
  85. printf( "-----------------------------------------------------------------------------\n");
  86. /*
  87. * Time the loop overhead so we can subtract it from the actual test times.
  88. */
  89. TESTSTART
  90. assert(1 == one);
  91. TESTSTOP
  92. durationMilliSecs = GetDurationMilliSecs(currSysTimeStart, currSysTimeStop) - overHeadMilliSecs;
  93. overHeadMilliSecs = durationMilliSecs;
  94. /*
  95. * Now we can start the actual tests
  96. */
  97. assert((w32sema = CreateSemaphore(NULL, (long) 0, (long) ITERATIONS, NULL)) != 0);
  98. TESTSTART
  99. assert((ReleaseSemaphore(w32sema, 1, NULL),1) == one);
  100. TESTSTOP
  101. assert(CloseHandle(w32sema) != 0);
  102. reportTest("W32 Post with no waiters");
  103. assert((w32sema = CreateSemaphore(NULL, (long) ITERATIONS, (long) ITERATIONS, NULL)) != 0);
  104. TESTSTART
  105. assert((WaitForSingleObject(w32sema, INFINITE),1) == one);
  106. TESTSTOP
  107. assert(CloseHandle(w32sema) != 0);
  108. reportTest("W32 Wait without blocking");
  109. assert(sem_init(&sema, 0, 0) == 0);
  110. TESTSTART
  111. assert((sem_post(&sema),1) == one);
  112. TESTSTOP
  113. assert(sem_destroy(&sema) == 0);
  114. reportTest("POSIX Post with no waiters");
  115. assert(sem_init(&sema, 0, ITERATIONS) == 0);
  116. TESTSTART
  117. assert((sem_wait(&sema),1) == one);
  118. TESTSTOP
  119. assert(sem_destroy(&sema) == 0);
  120. reportTest("POSIX Wait without blocking");
  121. printf( "=============================================================================\n");
  122. /*
  123. * End of tests.
  124. */
  125. return 0;
  126. }