Skip to content

Commit 7de414f

Browse files
author
Henry Qin
committed
Add manualBench function for tests that measure their own time
1 parent a5c1ffe commit 7de414f

File tree

6 files changed

+47
-2
lines changed

6 files changed

+47
-2
lines changed

cwrapper/perf_wrapper.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ Statistics
2222
bench(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
}

cwrapper/perf_wrapper.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ extern "C" {
2525

2626
typedef struct Statistics Statistics;
2727
Statistics bench(void (*function)(void), int numIterations);
28+
Statistics manualBench(void (*function)(uint64_t*), int numIterations);
2829

2930
#ifdef __cplusplus
3031
}

cwrapper/perf_wrapper_test.c

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff 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+
3640
int
3741
main() {
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
}

src/Perf.cc

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff 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;

src/Perf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@
1515
#include "Stats.h"
1616
namespace PerfUtils {
1717
Statistics bench(void (*function)(void), int numIterations);
18+
Statistics manualBench(void (*function)(uint64_t*), int numIterations);
1819
}

src/PerfTest.cc

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
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+
3338
TEST(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+
}

0 commit comments

Comments
 (0)