File tree Expand file tree Collapse file tree 6 files changed +47
-2
lines changed Expand file tree Collapse file tree 6 files changed +47
-2
lines changed Original file line number Diff line number Diff line change @@ -22,6 +22,10 @@ Statistics
2222bench (void (*function)(void ), int numIterations) {
2323 return PerfUtils::bench (function, numIterations);
2424}
25+ Statistics
26+ manualBench (void (*function)(uint64_t *), int numIterations) {
27+ return PerfUtils::manualBench (function, numIterations);
28+ }
2529
2630#ifdef __cplusplus
2731}
Original file line number Diff line number Diff line change @@ -25,6 +25,7 @@ extern "C" {
2525
2626typedef struct Statistics Statistics ;
2727Statistics bench (void (* function )(void ), int numIterations );
28+ Statistics manualBench (void (* function )(uint64_t * ), int numIterations );
2829
2930#ifdef __cplusplus
3031}
Original file line number Diff line number Diff line change @@ -33,13 +33,24 @@ uint64_t half(uint64_t n) {
3333 return n / 2 ;
3434}
3535
36+ void fixedPerformance (uint64_t * N ) {
37+ * N = 7 ;
38+ }
39+
3640int
3741main () {
3842 Statistics stats = bench (fiveHundredCycles , 100000 );
3943 stats = transformStatistics (stats , half );
4044 if (stats .count == 100000 && stats .count > stats .min ) {
41- puts (GREEN ("perf_wrapper_test PASSED" ));
45+ puts (GREEN ("perf_wrapper_test::bench PASSED" ));
46+ } else {
47+ puts (RED ("perf_wrapper_test::bench FAILED" ));
48+ }
49+
50+ stats = manualBench (fixedPerformance , 100000 );
51+ if (stats .count == 100000 && stats .average == 7 && stats .median == 7 && stats .min == 7 && stats .max == 7 && stats .stddev == 0 ) {
52+ puts (GREEN ("perf_wrapper_test::manualBench PASSED" ));
4253 } else {
43- puts (RED ("perf_wrapper FAILED" ));
54+ puts (RED ("perf_wrapper_test::manualBench FAILED" ));
4455 }
4556}
Original file line number Diff line number Diff line change @@ -37,6 +37,19 @@ namespace PerfUtils {
3737 latencies[i] = Cycles::rdtsc () - startTime;
3838 }
3939
40+ Statistics stats = computeStatistics (latencies, numIterations);
41+ delete[] latencies;
42+ return stats;
43+ }
44+ Statistics manualBench (void (*function)(uint64_t *), int numIterations) {
45+ uint64_t * latencies = new uint64_t [numIterations];
46+
47+ // Page in the memory
48+ memset (latencies, 0 , numIterations * sizeof (uint64_t ));
49+ for (int i = 0 ; i < numIterations; i++) {
50+ function (&latencies[i]);
51+ }
52+
4053 Statistics stats = computeStatistics (latencies, numIterations);
4154 delete[] latencies;
4255 return stats;
Original file line number Diff line number Diff line change 1515#include " Stats.h"
1616namespace PerfUtils {
1717 Statistics bench (void (*function)(void ), int numIterations);
18+ Statistics manualBench (void (*function)(uint64_t *), int numIterations);
1819}
Original file line number Diff line number Diff line change 1717
1818#include < fcntl.h>
1919#include < string.h>
20+ #include < stdlib.h>
2021#include < sys/stat.h>
2122#include < sys/types.h>
2223
@@ -30,8 +31,22 @@ void fixedCycles(int N) {
3031 }
3132}
3233
34+ void fixedPerformance (uint64_t * N) {
35+ *N = 7 ;
36+ }
37+
3338TEST (PerfTest, bench) {
3439 Statistics stats = PerfUtils::bench ([]() {fixedCycles (500 );}, 100000 );
3540 EXPECT_EQ (100000 , stats.count );
3641 EXPECT_LE (20 , stats.min );
3742}
43+
44+ TEST (PerfTest, manualBench) {
45+ Statistics stats = PerfUtils::manualBench (fixedPerformance, 100000 );
46+ EXPECT_EQ (100000 , stats.count );
47+ EXPECT_EQ (7 , stats.average );
48+ EXPECT_EQ (7 , stats.median );
49+ EXPECT_EQ (7 , stats.min );
50+ EXPECT_EQ (7 , stats.max );
51+ EXPECT_EQ (0 , stats.stddev );
52+ }
You can’t perform that action at this time.
0 commit comments