benchtest5.c 5.0 KB

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