1// Licensed to the .NET Foundation under one or more agreements.
2// The .NET Foundation licenses this file to you under the MIT license.
3// See the LICENSE file in the project root for more information.
4
5/*============================================================
6**
7** Source: test1.c
8**
9** Purpose: Test for QueryPerformanceCounter function
10**
11**
12**=========================================================*/
13
14/* Depends on: QueryPerformanceFrequency. */
15
16#include <palsuite.h>
17
18/* Milliseconds of error which are acceptable Function execution time, etc.
19 FreeBSD has a "standard" resolution of 50ms for waiting operations, so we
20 must take that into account as well */
21DWORD AcceptableTimeError = 15;
22
23int __cdecl main(int argc, char *argv[])
24{
25
26 int i;
27 int NumIterations = 100;
28 DWORD AvgTimeDiff;
29 DWORD TimeDiff[100];
30 DWORD TotalTimeDiff = 0;
31 DWORD SleepInterval = 50;
32 LARGE_INTEGER StartTime;
33 LARGE_INTEGER EndTime;
34 LARGE_INTEGER Freq;
35
36 /* Initialize the PAL.
37 */
38
39 if(0 != (PAL_Initialize(argc, argv)))
40 {
41 return FAIL;
42 }
43
44 /* Get the frequency of the High-Performance Counter,
45 * in order to convert counter time to milliseconds.
46 */
47 if (!QueryPerformanceFrequency(&Freq))
48 {
49 Fail("ERROR:%u:Unable to retrieve the frequency of the "
50 "high-resolution performance counter.\n",
51 GetLastError());
52 }
53
54 /* Perform this set of sleep timings a number of times.
55 */
56 for(i=0; i < NumIterations; i++)
57 {
58
59 /* Get the current counter value.
60 */
61 if (!QueryPerformanceCounter(&StartTime))
62 {
63 Fail("ERROR:%u:Unable to retrieve the current value of the "
64 "high-resolution performance counter.\n",
65 GetLastError());
66 }
67
68 /* Sleep a predetermined interval.
69 */
70 Sleep(SleepInterval);
71
72 /* Get the new current counter value.
73 */
74 if (!QueryPerformanceCounter(&EndTime))
75 {
76 Fail("ERROR:%u:Unable to retrieve the current value of the "
77 "high-resolution performance counter.\n",
78 GetLastError());
79 }
80
81 /* Determine elapsed time, in milliseconds. Compare the elapsed time
82 * with the sleep interval, and add to counter.
83 */
84 TimeDiff[i] = (DWORD)(((EndTime.QuadPart - StartTime.QuadPart)*1000)/
85 (Freq.QuadPart));
86 TotalTimeDiff += TimeDiff[i] - SleepInterval;
87
88 }
89
90 /* Verify that the average of the difference between the performance
91 * counter and the sleep interval is within our acceptable range.
92 */
93 AvgTimeDiff = TotalTimeDiff / NumIterations;
94 if (AvgTimeDiff > AcceptableTimeError)
95 {
96 Fail("ERROR: average diff %u acceptable %u.\n",
97 AvgTimeDiff,
98 AcceptableTimeError);
99 }
100
101 /* Terminate the PAL.
102 */
103 PAL_Terminate();
104 return PASS;
105}
106
107
108