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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
|
/////////////////////////////////////////////////////////////////////////////
// Copyright (c) Electronic Arts Inc. All rights reserved.
/////////////////////////////////////////////////////////////////////////////
#include "EASTLBenchmark.h"
#include "EASTLTest.h"
#include <EASTL/string.h>
#include <EAMain/EAMain.h>
#ifdef _MSC_VER
#pragma warning(push, 0)
#endif
#include <stdio.h>
#include <math.h>
#include <float.h>
#ifdef _MSC_VER
#pragma warning(pop)
#endif
namespace Benchmark
{
static int64_t ConvertStopwatchUnits(EA::StdC::Stopwatch::Units unitsSource, int64_t valueSource, EA::StdC::Stopwatch::Units unitsDest)
{
using namespace EA::StdC;
int64_t valueDest = valueSource;
if(unitsSource != unitsDest)
{
double sourceMultiplier;
switch (unitsSource)
{
case Stopwatch::kUnitsCPUCycles:
sourceMultiplier = Stopwatch::GetUnitsPerCPUCycle(unitsDest); // This will typically be a number less than 1.
valueDest = (int64_t)(valueSource * sourceMultiplier);
break;
case Stopwatch::kUnitsCycles:
sourceMultiplier = Stopwatch::GetUnitsPerStopwatchCycle(unitsDest); // This will typically be a number less than 1.
valueDest = (int64_t)(valueSource * sourceMultiplier);
break;
case Stopwatch::kUnitsNanoseconds:
case Stopwatch::kUnitsMicroseconds:
case Stopwatch::kUnitsMilliseconds:
case Stopwatch::kUnitsSeconds:
case Stopwatch::kUnitsMinutes:
case Stopwatch::kUnitsUserDefined:
// To do. Also, handle the case of unitsDest being Cycles or CPUCycles and unitsSource being a time.
break;
}
}
return valueDest;
}
void WriteTime(int64_t timeNS, eastl::string& sTime)
{
if(timeNS > 1000000000)
sTime.sprintf(" %6.2f s", (double)timeNS / 1000000000);
else if(timeNS > 1000000)
sTime.sprintf("%6.1f ms", (double)timeNS / 1000000);
else if(timeNS > 1000)
sTime.sprintf("%6.1f us", (double)timeNS / 1000);
else
sTime.sprintf("%6.1f ns", (double)timeNS / 1);
}
Environment gEnvironment;
Environment& GetEnvironment()
{
return gEnvironment;
}
ResultSet gResultSet;
ResultSet& GetResultSet()
{
return gResultSet;
}
// Scratch sprintf buffer
char gScratchBuffer[1024];
void DoNothing(...)
{
// Intentionally nothing.
}
void AddResult(const char* pName, int units, int64_t nTime1, int64_t nTime2, const char* pNotes)
{
Result result;
result.msName = pName;
result.mUnits = units;
result.mTime1 = nTime1;
result.mTime1NS = ConvertStopwatchUnits((EA::StdC::Stopwatch::Units)units, nTime1, EA::StdC::Stopwatch::kUnitsNanoseconds);
result.mTime2 = nTime2;
result.mTime2NS = ConvertStopwatchUnits((EA::StdC::Stopwatch::Units)units, nTime2, EA::StdC::Stopwatch::kUnitsNanoseconds);
if(pNotes)
result.msNotes = pNotes;
gResultSet.insert(result);
}
void PrintResultLine(const Result& result)
{
const double fRatio = (double)result.mTime1 / (double)result.mTime2;
const double fRatioPrinted = (fRatio > 100) ? 100 : fRatio;
const double fPercentChange = fabs(((double)result.mTime1 - (double)result.mTime2) / (((double)result.mTime1 + (double)result.mTime2) / 2));
const bool bDifference = (result.mTime1 > 10) && (result.mTime2 > 10) && (fPercentChange > 0.25);
const char* pDifference = (bDifference ? (result.mTime1 < result.mTime2 ? "-" : "+") : "");
eastl::string sClockTime1, sClockTime2;
WriteTime(result.mTime1NS, sClockTime1); // This converts an integer in nanoseconds (e.g. 23400000) to a string (e.g. "23.4 ms")
WriteTime(result.mTime2NS, sClockTime2);
EA::UnitTest::Report("%-43s | %13" PRIu64 " %s | %13" PRIu64 " %s | %10.2f%10s", result.msName.c_str(), result.mTime1, sClockTime1.c_str(), result.mTime2, sClockTime2.c_str(), fRatioPrinted, pDifference);
if(result.msNotes.length()) // If there are any notes...
EA::UnitTest::Report(" %s", result.msNotes.c_str());
EA::UnitTest::Report("\n");
}
#if defined(EASTL_BENCHMARK_WRITE_FILE) && EASTL_BENCHMARK_WRITE_FILE
#if !defined(EASTL_BENCHMARK_WRITE_FILE_PATH)
#define EASTL_BENCHMARK_WRITE_FILE_PATH "BenchmarkResults.txt"
#endif
struct FileWriter
{
FILE* mpReportFile;
EA::EAMain::ReportFunction mpSavedReportFunction;
static FileWriter* gpFileWriter;
static void StaticPrintfReportFunction(const char8_t* pText)
{
if(gpFileWriter)
gpFileWriter->PrintfReportFunction(pText);
}
void PrintfReportFunction(const char8_t* pText)
{
fwrite(pText, strlen(pText), 1, mpReportFile);
EA::EAMain::ReportFunction gpReportFunction = EA::EAMain::GetDefaultReportFunction();
gpReportFunction(pText);
}
FileWriter() : mpReportFile(NULL), mpSavedReportFunction(NULL)
{
mpReportFile = fopen(EASTL_BENCHMARK_WRITE_FILE_PATH, "w+");
if(mpReportFile)
{
gpFileWriter = this;
mpSavedReportFunction = EA::EAMain::GetDefaultReportFunction();
EA::EAMain::SetReportFunction(StaticPrintfReportFunction);
}
}
~FileWriter()
{
if(mpReportFile)
{
gpFileWriter = NULL;
EA::EAMain::SetReportFunction(mpSavedReportFunction);
fclose(mpReportFile);
}
}
};
FileWriter* FileWriter::gpFileWriter = NULL;
#endif
void PrintResults()
{
#if defined(EASTL_BENCHMARK_WRITE_FILE) && EASTL_BENCHMARK_WRITE_FILE
FileWriter fileWriter; // This will auto-execute.
#endif
// Print the results
EA::UnitTest::Report("\n");
EA::UnitTest::Report("****************************************************************************************\n");
EA::UnitTest::Report("EASTL Benchmark test results\n");
EA::UnitTest::Report("****************************************************************************************\n");
EA::UnitTest::Report("\n");
EA::UnitTest::Report("EASTL version: %s\n", EASTL_VERSION);
EA::UnitTest::Report("Platform: %s\n", gEnvironment.msPlatform.c_str());
EA::UnitTest::Report("Compiler: %s\n", EA_COMPILER_STRING);
#if defined(EA_DEBUG) || defined(_DEBUG)
EA::UnitTest::Report("Allocator: PPMalloc::GeneralAllocatorDebug. Thread safety enabled.\n");
EA::UnitTest::Report("Build: Debug. Inlining disabled. STL debug features disabled.\n");
#else
EA::UnitTest::Report("Allocator: PPMalloc::GeneralAllocator. Thread safety enabled.\n");
EA::UnitTest::Report("Build: Full optimization. Inlining enabled.\n");
#endif
EA::UnitTest::Report("\n");
EA::UnitTest::Report("Values are ticks and time to complete tests; smaller values are better.\n");
EA::UnitTest::Report("\n");
EA::UnitTest::Report("%-43s%26s%26s%13s%13s\n", "Test", gEnvironment.msSTLName1.c_str(), gEnvironment.msSTLName2.c_str(), "Ratio", "Difference?");
EA::UnitTest::Report("---------------------------------------------------------------------------------------------------------------------\n");
eastl::string sTestTypeLast;
eastl::string sTestTypeTemp;
for(ResultSet::iterator it = gResultSet.begin(); it != gResultSet.end(); ++it)
{
const Result& result = *it;
eastl_size_t n = result.msName.find('/');
if(n == eastl::string::npos)
n = result.msName.length();
sTestTypeTemp.assign(result.msName, 0, n);
if(sTestTypeTemp != sTestTypeLast) // If it looks like we are changing to a new test type... add an empty line to help readability.
{
if(it != gResultSet.begin())
EA::UnitTest::Report("\n");
sTestTypeLast = sTestTypeTemp;
}
PrintResultLine(result);
}
// We will print out a final line that has the sum of the rows printed above.
Result resultSum;
resultSum.msName = "sum";
for(ResultSet::iterator its = gResultSet.begin(); its != gResultSet.end(); ++its)
{
const Result& resultTemp = *its;
EASTL_ASSERT(resultTemp.mUnits == EA::StdC::Stopwatch::kUnitsCPUCycles); // Our ConvertStopwatchUnits call below assumes that every measured time is CPUCycles.
resultSum.mTime1 += resultTemp.mTime1;
resultSum.mTime2 += resultTemp.mTime2;
}
// We do this convert as a final step instead of the loop in order to avoid loss of precision.
resultSum.mTime1NS = ConvertStopwatchUnits(EA::StdC::Stopwatch::kUnitsCPUCycles, resultSum.mTime1, EA::StdC::Stopwatch::kUnitsNanoseconds);
resultSum.mTime2NS = ConvertStopwatchUnits(EA::StdC::Stopwatch::kUnitsCPUCycles, resultSum.mTime2, EA::StdC::Stopwatch::kUnitsNanoseconds);
EA::UnitTest::Report("\n");
PrintResultLine(resultSum);
EA::UnitTest::Report("\n");
EA::UnitTest::Report("****************************************************************************************\n");
EA::UnitTest::Report("\n");
// Clear the results
gResultSet.clear();
gEnvironment.clear();
}
} // namespace Benchmark
|