lib/bench/src/stats/totals.zig

daab053ee43316e1809a84551d573ddd1e5bf3d2

  1 const std = @import("std");
  2 
  3 pub fn IndexedDeltaTotals(comptime count: usize) type {
  4     return struct {
  5         before_total: [count]u128 = @as([count]u128, @splat(0)),
  6         after_total: [count]u128 = @as([count]u128, @splat(0)),
  7         delta_total: [count]i128 = @as([count]i128, @splat(0)),
  8 
  9         pub fn record(
 10             self: *@This(),
 11             index: usize,
 12             before: u64,
 13             after: u64,
 14             delta: i128,
 15         ) void {
 16             std.debug.assert(index < count);
 17             self.before_total[index] += before;
 18             self.after_total[index] += after;
 19             self.delta_total[index] += delta;
 20         }
 21 
 22         pub fn meanBefore(self: *const @This(), index: usize, sample_count: u32) f64 {
 23             std.debug.assert(index < count);
 24             return meanU128(self.before_total[index], sample_count);
 25         }
 26 
 27         pub fn meanAfter(self: *const @This(), index: usize, sample_count: u32) f64 {
 28             std.debug.assert(index < count);
 29             return meanU128(self.after_total[index], sample_count);
 30         }
 31 
 32         pub fn meanDelta(self: *const @This(), index: usize, sample_count: u32) f64 {
 33             std.debug.assert(index < count);
 34             return meanI128(self.delta_total[index], sample_count);
 35         }
 36     };
 37 }
 38 
 39 pub fn IndexedMemoryTotals(comptime count: usize) type {
 40     return struct {
 41         alloc_count_total: [count]u128 = @as([count]u128, @splat(0)),
 42         free_count_total: [count]u128 = @as([count]u128, @splat(0)),
 43         alloc_bytes_total: [count]u128 = @as([count]u128, @splat(0)),
 44 
 45         pub fn record(
 46             self: *@This(),
 47             index: usize,
 48             alloc_count: u64,
 49             free_count: u64,
 50             alloc_bytes: u64,
 51         ) void {
 52             std.debug.assert(index < count);
 53             self.alloc_count_total[index] += alloc_count;
 54             self.free_count_total[index] += free_count;
 55             self.alloc_bytes_total[index] += alloc_bytes;
 56         }
 57 
 58         pub fn meanAllocCount(self: *const @This(), index: usize, sample_count: u32) f64 {
 59             std.debug.assert(index < count);
 60             return meanU128(self.alloc_count_total[index], sample_count);
 61         }
 62 
 63         pub fn meanFreeCount(self: *const @This(), index: usize, sample_count: u32) f64 {
 64             std.debug.assert(index < count);
 65             return meanU128(self.free_count_total[index], sample_count);
 66         }
 67 
 68         pub fn meanAllocBytes(self: *const @This(), index: usize, sample_count: u32) f64 {
 69             std.debug.assert(index < count);
 70             return meanU128(self.alloc_bytes_total[index], sample_count);
 71         }
 72     };
 73 }
 74 
 75 pub fn nsPerUnit(ns: f64, units: u64) f64 {
 76     return valuePerUnit(ns, units);
 77 }
 78 
 79 pub fn valuePerUnit(value: f64, units: u64) f64 {
 80     if (units == 0) return 0;
 81     return value / @as(f64, @floatFromInt(units));
 82 }
 83 
 84 pub fn nsPerFloatUnit(ns: f64, units: f64) f64 {
 85     return valuePerFloatUnit(ns, units);
 86 }
 87 
 88 pub fn valuePerFloatUnit(value: f64, units: f64) f64 {
 89     if (units == 0) return 0;
 90     return value / units;
 91 }
 92 
 93 fn meanU128(total: u128, sample_count: u32) f64 {
 94     if (sample_count == 0) return 0;
 95     return @as(f64, @floatFromInt(total)) / @as(f64, @floatFromInt(sample_count));
 96 }
 97 
 98 fn meanI128(total: i128, sample_count: u32) f64 {
 99     if (sample_count == 0) return 0;
100     return @as(f64, @floatFromInt(total)) / @as(f64, @floatFromInt(sample_count));
101 }