| 1 |
/*** |
| 2 |
* This file is based on or incorporates material from the UnitTest++ r30 open source project. |
| 3 |
* Microsoft is not the original author of this code but has modified it and is licensing the code under |
| 4 |
* the MIT License. Microsoft reserves all other rights not expressly granted under the MIT License, |
| 5 |
* whether by implication, estoppel or otherwise. |
| 6 |
* |
| 7 |
* UnitTest++ r30 |
| 8 |
* |
| 9 |
* Copyright (c) 2006 Noel Llopis and Charles Nicholson |
| 10 |
* Portions Copyright (c) Microsoft Corporation |
| 11 |
* |
| 12 |
* All Rights Reserved. |
| 13 |
* |
| 14 |
* MIT License |
| 15 |
* |
| 16 |
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
| 17 |
* and associated documentation files (the "Software"), to deal in the Software without restriction, |
| 18 |
* including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 19 |
* and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, |
| 20 |
* subject to the following conditions: |
| 21 |
* |
| 22 |
* The above copyright notice and this permission notice shall be included in all copies or |
| 23 |
* substantial portions of the Software. |
| 24 |
* |
| 25 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, |
| 26 |
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE |
| 27 |
* AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 28 |
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 29 |
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 30 |
***/ |
| 31 |
|
| 32 |
#include "stdafx.h" |
| 33 |
|
| 34 |
namespace UnitTest |
| 35 |
{ |
| 36 |
Timer::Timer() : m_threadHandle(::GetCurrentThread()), m_startTime(0) |
| 37 |
{ |
| 38 |
#if defined(UNITTEST_WIN32) && (_MSC_VER == 1200) // VC6 doesn't have DWORD_PTR |
| 39 |
typedef unsigned long DWORD_PTR; |
| 40 |
#endif |
| 41 |
|
| 42 |
DWORD_PTR systemMask; |
| 43 |
::GetProcessAffinityMask(GetCurrentProcess(), &m_processAffinityMask, &systemMask); |
| 44 |
::SetThreadAffinityMask(m_threadHandle, 1); |
| 45 |
::QueryPerformanceFrequency(reinterpret_cast<LARGE_INTEGER*>(&m_frequency)); |
| 46 |
::SetThreadAffinityMask(m_threadHandle, m_processAffinityMask); |
| 47 |
} |
| 48 |
|
| 49 |
void Timer::Start() { m_startTime = GetTime(); } |
| 50 |
|
| 51 |
double Timer::GetTimeInMs() const |
| 52 |
{ |
| 53 |
__int64 const elapsedTime = GetTime() - m_startTime; |
| 54 |
double const seconds = double(elapsedTime) / double(m_frequency); |
| 55 |
return seconds * 1000.0; |
| 56 |
} |
| 57 |
|
| 58 |
__int64 Timer::GetTime() const |
| 59 |
{ |
| 60 |
LARGE_INTEGER curTime; |
| 61 |
::SetThreadAffinityMask(m_threadHandle, 1); |
| 62 |
::QueryPerformanceCounter(&curTime); |
| 63 |
::SetThreadAffinityMask(m_threadHandle, m_processAffinityMask); |
| 64 |
return curTime.QuadPart; |
| 65 |
} |
| 66 |
|
| 67 |
void TimeHelpers::SleepMs(int ms) { ::Sleep(ms); } |
| 68 |
|
| 69 |
} // namespace UnitTest |