-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.cpp
195 lines (156 loc) · 3.98 KB
/
test.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <stdio.h>
#include <stdlib.h>
#include "profiler_inline.cpp"
f32 SomeWork() {
f32 sum = 0.0f;
for (u32 i = 0; i < 1000000; ++i) {
f32 random = (f32)rand() / (f32)RAND_MAX;
sum += random;
}
return sum;
}
void TestTwoBlocksLoops() {
printf("Test Two Blocks Loops:\n");
srand(1234);
BeginProfiling();
for (u32 i = 0; i < 5; ++i) {
PROFILE_BLOCK("should be 25% total, 25% exclusive");
SomeWork();
}
for (u32 i = 0; i < 15; ++i) {
PROFILE_BLOCK("should be 75% total, 75% exclusive");
SomeWork();
}
FinishProfiling();
PrintProfilingResults();
printf("\n");
}
void TestRecursiveFunc(u32 counter) {
PROFILE_BLOCK("should be 80% total, 80% exclusive");
SomeWork();
if (counter != 0) {
TestRecursiveFunc(--counter);
}
}
void TestRecursive() {
printf("Test Recursive:\n");
BeginProfiling();
{
PROFILE_BLOCK("should be 100% total, 20% exclusive");
SomeWork();
TestRecursiveFunc(3);
}
FinishProfiling();
PrintProfilingResults();
printf("\n");
}
void RecursiveIndirectFuncA(u32 counter);
void RecursiveIndirectFuncB(u32 counter) {
PROFILE_BLOCK("should be 67% total, 33% exclusive");
SomeWork();
if (counter != 0) {
RecursiveIndirectFuncA(--counter);
}
}
void RecursiveIndirectFuncA(u32 counter) {
PROFILE_BLOCK("should be 83% total, 50% exclusive");
SomeWork();
if (counter != 0) {
RecursiveIndirectFuncB(--counter);
}
}
void TestRecursiveIndirect() {
printf("Test Recursive Indirect:\n");
BeginProfiling();
{
PROFILE_BLOCK("should be 100% total, 17% exclusive");
SomeWork();
RecursiveIndirectFuncA(4);
}
FinishProfiling();
PrintProfilingResults();
printf("\n");
}
void TestSimpleNested() {
printf("Test Simple Nested:\n");
BeginProfiling();
{
PROFILE_BLOCK("should be 100% total, 50% exclusive");
SomeWork();
{
PROFILE_BLOCK("should be 50% total, 50% exclusive");
SomeWork();
}
}
FinishProfiling();
PrintProfilingResults();
printf("\n");
}
void TestDoubleNested() {
printf("Test Double Nested:\n");
BeginProfiling();
{
PROFILE_BLOCK("should be 100% total, 33% exclusive");
SomeWork();
{
PROFILE_BLOCK("should be 66% total, 33% exclusive");
SomeWork();
{
PROFILE_BLOCK("should be 33% total, 33% exclusive");
SomeWork();
}
}
}
FinishProfiling();
PrintProfilingResults();
printf("\n");
}
void TestComplexNesting() {
printf("Test Complex Nesting:\n");
BeginProfiling();
{
PROFILE_BLOCK("should be 0% total, 0% exclusive");
srand(1234);
}
for (u32 i = 0; i < 5; ++i) {
PROFILE_BLOCK("should be 50% total, 50% exclusive");
f32 sum = SomeWork();
}
{
PROFILE_BLOCK("should be 50% total, 10% exclusive");
f32 sum = SomeWork();
{
{
PROFILE_BLOCK("should be 20% total, 10% exclusive");
f32 sum = SomeWork();
{
PROFILE_BLOCK(
"should be 10% total, 10% exclusive");
f32 sum = SomeWork();
}
}
{
PROFILE_BLOCK(
"should be 20% total, 10% exclusive");
sum = SomeWork();
{
PROFILE_BLOCK(
"should be 10% total, 10% exclusive");
f32 sum = SomeWork();
}
}
}
}
FinishProfiling();
PrintProfilingResults();
printf("\n");
}
int main(int argc, char** argv) {
TestSimpleNested();
TestTwoBlocksLoops();
TestDoubleNested();
TestComplexNesting();
TestRecursive();
TestRecursiveIndirect();
return 0;
}