Skip to content

Commit 0e16cb5

Browse files
author
Henry Qin
committed
Add transformStatistics and printStatistics
1 parent 031d0aa commit 0e16cb5

File tree

3 files changed

+87
-10
lines changed

3 files changed

+87
-10
lines changed

src/Stats.cc

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,48 @@ computeStatistics(uint64_t* rawdata, size_t count) {
6666
return stats;
6767
}
6868

69+
Statistics
70+
transformStatistics(Statistics stats, uint64_t (*function)(uint64_t)) {
71+
Statistics result;
72+
result.count = stats.count;
73+
result.average = function(stats.average);
74+
result.stddev = function(stats.stddev);
75+
result.min = function(stats.min);
76+
result.median = function(stats.median);
77+
result.P10 = function(stats.P10);
78+
result.P20 = function(stats.P20);
79+
result.P30 = function(stats.P30);
80+
result.P40 = function(stats.P40);
81+
result.P50 = function(stats.P50);
82+
result.P60 = function(stats.P60);
83+
result.P70 = function(stats.P70);
84+
result.P80 = function(stats.P80);
85+
result.P90 = function(stats.P90);
86+
result.P99 = function(stats.P99);
87+
result.P999 = function(stats.P999);
88+
result.P9999 = function(stats.P9999);
89+
result.max = function(stats.max);
90+
return result;
91+
}
92+
93+
void
94+
printStatistics(Statistics stats, const char* label) {
95+
static bool headerPrinted = false;
96+
if (!headerPrinted) {
97+
puts("Benchmark,Count,Avg,Stddev,Median,Min,99%,"
98+
"99.9%,99.99%,Max");
99+
headerPrinted = true;
100+
}
101+
printf("%s,%lu,%lu,%lu,%lu,%lu,%lu,%lu,%lu,%lu\n", label, stats.count, stats.average,
102+
stats.stddev, stats.median, stats.min, stats.P99, stats.P999, stats.P9999,
103+
stats.max);
104+
}
105+
69106
void
70107
printStatistics(const char* label, uint64_t* rawdata, size_t count,
71108
const char* datadir) {
72-
static bool headerPrinted = false;
73-
if (!headerPrinted) {
74-
puts(
75-
"Benchmark,Count,Avg,Median,Min,99%,"
76-
"99.9%,99.99%,Max");
77-
headerPrinted = true;
78-
}
79109
Statistics stats = computeStatistics(rawdata, count);
80-
printf("%s,%zu,%lu,%lu,%lu,%lu,%lu,%lu,%lu\n", label, count, stats.average,
81-
stats.median, stats.min, stats.P99, stats.P999, stats.P9999,
82-
stats.max);
110+
printStatistics(stats, label);
83111

84112
// Dump the data out
85113
if (datadir != NULL) {

src/StatsMinimal.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include <stdint.h>
1919
#include <stddef.h>
20+
#include <stdbool.h>
2021

2122
/**
2223
* This file exists to make the computeStatistics function available from C
@@ -44,4 +45,18 @@ struct Statistics {
4445
};
4546

4647
struct Statistics computeStatistics(uint64_t* rawdata, size_t count);
48+
49+
50+
/**
51+
* Apply a transformation function on all statistics, usually to change the
52+
* units. It is assumed that the units on all statistics are initially
53+
* identical.
54+
*/
55+
struct Statistics transformStatistics(struct Statistics stats, uint64_t (*function)(uint64_t));
56+
57+
/**
58+
* Print out all the statistics in CSV format.
59+
*/
60+
void printStatistics(struct Statistics stats, const char* label);
61+
4762
#endif // PERFUTILS_STATS_MINIMAL_H

src/StatsTest.cc

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@
2323
#include "gmock/gmock.h"
2424
#include "gtest/gtest.h"
2525

26+
/**
27+
* Simple test function for transforming statistics.
28+
*/
29+
uint64_t half(uint64_t n) {
30+
return n / 2;
31+
}
2632

2733
TEST(StatsTest, allStats) {
2834
const int numElements = 100;
@@ -37,3 +43,31 @@ TEST(StatsTest, allStats) {
3743
EXPECT_EQ(49, stats.average);
3844
EXPECT_EQ(28, stats.stddev);
3945
}
46+
47+
TEST(StatsTest, transformStatistics) {
48+
const int numElements = 100;
49+
uint64_t input[numElements];
50+
for (uint64_t i = 0; i < numElements; i++)
51+
input[i] = i;
52+
Statistics stats = computeStatistics(input, numElements);
53+
Statistics halfStats = transformStatistics(stats, half);
54+
55+
EXPECT_EQ(stats.count, halfStats.count);
56+
EXPECT_EQ(half(stats.average), halfStats.average);
57+
EXPECT_EQ(half(stats.stddev), halfStats.stddev);
58+
EXPECT_EQ(half(stats.min), halfStats.min);
59+
EXPECT_EQ(half(stats.median), halfStats.median);
60+
EXPECT_EQ(half(stats.P10), halfStats.P10);
61+
EXPECT_EQ(half(stats.P20), halfStats.P20);
62+
EXPECT_EQ(half(stats.P30), halfStats.P30);
63+
EXPECT_EQ(half(stats.P40), halfStats.P40);
64+
EXPECT_EQ(half(stats.P50), halfStats.P50);
65+
EXPECT_EQ(half(stats.P60), halfStats.P60);
66+
EXPECT_EQ(half(stats.P70), halfStats.P70);
67+
EXPECT_EQ(half(stats.P80), halfStats.P80);
68+
EXPECT_EQ(half(stats.P90), halfStats.P90);
69+
EXPECT_EQ(half(stats.P99), halfStats.P99);
70+
EXPECT_EQ(half(stats.P999), halfStats.P999);
71+
EXPECT_EQ(half(stats.P9999), halfStats.P9999);
72+
EXPECT_EQ(half(stats.max), halfStats.max);
73+
}

0 commit comments

Comments
 (0)