-
Notifications
You must be signed in to change notification settings - Fork 9
/
AlarmClockSpeedTest.cpp
122 lines (102 loc) · 5.22 KB
/
AlarmClockSpeedTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* File: AlarmClockSpeedTest.cpp
* Author: Amanda Carbonari
* Created: January 6, 2015 9:00am
*/
#include <ctime>
#include <iostream>
#include "AlarmClock.h" // To test other implementations, change this to #include<header file> or #inlcude<AlarmClock.h.old> for example
#include <chrono>
#include <thread>
using namespace std;
using namespace std::chrono;
typedef std::chrono::microseconds microseconds;
typedef std::chrono::milliseconds milliseconds;
typedef std::chrono::seconds seconds;
// Forward declaration of every type of templated class
template class AlarmClock<microseconds>;
template<typename T> size_t WaitForAlarmClockToExpire(AlarmClock<T>& alerter) {
size_t i = 1;
for(; !alerter.Expired(); ++i);
return i;
}
void getSleepOverhead(unsigned int sleep_time) {
high_resolution_clock::time_point start_overall = high_resolution_clock::now();
AlarmClock<microseconds> alerter(sleep_time);
auto forLoopVal = WaitForAlarmClockToExpire(alerter);
high_resolution_clock::time_point end_overall = high_resolution_clock::now();
auto overall_time = duration_cast<microseconds>(end_overall - start_overall).count();
auto slept_time = alerter.SleepTimeUs();
auto overhead = overall_time - slept_time;
cout << "Results: " << endl;
cout << "\tOverall Time: " << overall_time << " us" << endl;
cout << "\tOverhead Time: " << overhead << " us" << endl;
cout << "\tSleep Time: " << slept_time << " us" << endl;
cout << "\tFor Loop Count: " << forLoopVal << endl;
}
void testReset(unsigned int sleep_time, int reset_portion=-1) {
// Start overall measurements
high_resolution_clock::time_point start_overall = high_resolution_clock::now();
AlarmClock<microseconds> alerter(sleep_time);
// Sleep before restart, this can be changed to see the overhead for
// different reset periods
auto wait_amount = sleep_time/reset_portion;
if (reset_portion == -1) {
wait_amount = 0;
}
std::this_thread::sleep_for(microseconds(wait_amount));
// Start the reset measurement
high_resolution_clock::time_point start = high_resolution_clock::now();
alerter.Reset();
// Get the ending reset measurment
high_resolution_clock::time_point end = high_resolution_clock::now();
// Calculate the reset time
auto reset_time = duration_cast<microseconds>(end - start).count();
// Make sure the clock expires correctly after reset
WaitForAlarmClockToExpire(alerter);
// Get the ending overall time
high_resolution_clock::time_point end_overall = high_resolution_clock::now();
// Calculate the overall time
auto overall_time = duration_cast<microseconds>(end_overall - start_overall).count();
auto slept_time = alerter.SleepTimeUs();
// Overhead is just the overall time minus the slept time and wait period.
auto overhead = overall_time - slept_time - wait_amount;
cout << "Results:" << endl;
cout << "\tOverall Running Time: " << overall_time << " us" << endl;
cout << "\tOverhead Time: " << overhead << " us" << endl;
cout << "\tOverhead Breakdowns: " << endl;
cout << "\t\tReset Time: " << reset_time << " us" << endl;
cout << "\t\tSleeping Overhead: " << duration_cast<microseconds>(end_overall - end).count() - slept_time << " us" << endl;
cout << "\tWait Before Restart Time: " << wait_amount << " us" << endl;
cout << "\tTime Spent Sleeping: " << slept_time << " us" << endl;
}
int main(int, const char**) {
// The tester could take in different values as arguments, for now I have
// some hard coded.
microseconds us = microseconds(400);
milliseconds ms = milliseconds(3);
seconds s = seconds(1);
for (int reset_time = 2; reset_time < 10; ++reset_time) {
cout << "Reset @ 1/" << reset_time << endl;
cout << "---------------------------- Testing " << us.count() << " microseconds ----------------------------" << endl;
testReset(us.count(), 2);
cout << "---------------------------- Testing " << ms.count() << " milliseconds ----------------------------" << endl;
testReset(microseconds(ms).count(), reset_time);
cout << "---------------------------- Testing " << s.count() << " seconds ----------------------------" << endl;
testReset(microseconds(s).count(), reset_time);
}
cout << "Reset Immediately" << endl;
cout << "---------------------------- Testing " << us.count() << " microseconds ----------------------------" << endl;
testReset(us.count());
cout << "---------------------------- Testing " << ms.count() << " milliseconds ----------------------------" << endl;
testReset(microseconds(ms).count());
cout << "---------------------------- Testing " << s.count() << " seconds ----------------------------" << endl;
testReset(microseconds(s).count());
cout << "No Reset and For Loop Test" << endl;
cout << "---------------------------- Testing " << us.count() << " microseconds ----------------------------" << endl;
getSleepOverhead(us.count());
cout << "---------------------------- Testing " << ms.count() << " milliseconds ----------------------------" << endl;
getSleepOverhead(microseconds(ms).count());
cout << "---------------------------- Testing " << s.count() << " seconds ----------------------------" << endl;
getSleepOverhead(microseconds(s).count());
}