simplify runner and simplify suite
runner.zig:
- rename shadowed bytes_per_op to declared_bytes
- inline trivial intermediates into the Result return block
suite.zig:
- collapse verbose if/else in parse_duration_ns into a compact
expression-style return
This commit is contained in:
@@ -43,7 +43,7 @@ pub fn run_one(name: []const u8, f: BenchFn, env: Env, opts: Options) !Result {
|
|||||||
var elapsed_ns: u64 = 0;
|
var elapsed_ns: u64 = 0;
|
||||||
var alloc_bytes: u64 = 0;
|
var alloc_bytes: u64 = 0;
|
||||||
var alloc_count: u64 = 0;
|
var alloc_count: u64 = 0;
|
||||||
var bytes_per_op: u64 = 0;
|
var declared_bytes: u64 = 0;
|
||||||
var is_container = false;
|
var is_container = false;
|
||||||
var force_report = false;
|
var force_report = false;
|
||||||
|
|
||||||
@@ -66,7 +66,7 @@ pub fn run_one(name: []const u8, f: BenchFn, env: Env, opts: Options) !Result {
|
|||||||
elapsed_ns = if (b.accumulated_ns <= 0) 0 else @intCast(@min(b.accumulated_ns, std.math.maxInt(u64)));
|
elapsed_ns = if (b.accumulated_ns <= 0) 0 else @intCast(@min(b.accumulated_ns, std.math.maxInt(u64)));
|
||||||
alloc_bytes = env.counter.bytes_allocated;
|
alloc_bytes = env.counter.bytes_allocated;
|
||||||
alloc_count = env.counter.allocs;
|
alloc_count = env.counter.allocs;
|
||||||
bytes_per_op = b.bytes_per_op;
|
declared_bytes = b.bytes_per_op;
|
||||||
is_container = b.is_container;
|
is_container = b.is_container;
|
||||||
force_report = b.force_report_allocs;
|
force_report = b.force_report_allocs;
|
||||||
|
|
||||||
@@ -78,26 +78,19 @@ pub fn run_one(name: []const u8, f: BenchFn, env: Env, opts: Options) !Result {
|
|||||||
}
|
}
|
||||||
|
|
||||||
const fn_n: f64 = @floatFromInt(n);
|
const fn_n: f64 = @floatFromInt(n);
|
||||||
const ns_per_op_v: f64 = if (n == 0) 0 else @as(f64, @floatFromInt(elapsed_ns)) / fn_n;
|
|
||||||
const bytes_alloc_per_op: f64 = if (n == 0) 0 else @as(f64, @floatFromInt(alloc_bytes)) / fn_n;
|
|
||||||
const allocs_per_op: f64 = if (n == 0) 0 else @as(f64, @floatFromInt(alloc_count)) / fn_n;
|
|
||||||
|
|
||||||
const mb_per_sec: ?f64 = if (bytes_per_op == 0 or elapsed_ns == 0)
|
|
||||||
null
|
|
||||||
else blk: {
|
|
||||||
const total_bytes: f64 = @floatFromInt(bytes_per_op *| n);
|
|
||||||
const elapsed_s: f64 = @as(f64, @floatFromInt(elapsed_ns)) / @as(f64, std.time.ns_per_s);
|
|
||||||
break :blk (total_bytes / (1024.0 * 1024.0)) / elapsed_s;
|
|
||||||
};
|
|
||||||
|
|
||||||
return .{
|
return .{
|
||||||
.name = name,
|
.name = name,
|
||||||
.n = n,
|
.n = n,
|
||||||
.elapsed_ns = elapsed_ns,
|
.elapsed_ns = elapsed_ns,
|
||||||
.ns_per_op = ns_per_op_v,
|
.ns_per_op = if (n == 0) 0 else @as(f64, @floatFromInt(elapsed_ns)) / fn_n,
|
||||||
.bytes_per_op = bytes_alloc_per_op,
|
.bytes_per_op = if (n == 0) 0 else @as(f64, @floatFromInt(alloc_bytes)) / fn_n,
|
||||||
.allocs_per_op = allocs_per_op,
|
.allocs_per_op = if (n == 0) 0 else @as(f64, @floatFromInt(alloc_count)) / fn_n,
|
||||||
.mb_per_sec = mb_per_sec,
|
.mb_per_sec = if (declared_bytes == 0 or elapsed_ns == 0) null else blk: {
|
||||||
|
const total_bytes: f64 = @floatFromInt(declared_bytes *| n);
|
||||||
|
const elapsed_s: f64 = @as(f64, @floatFromInt(elapsed_ns)) / @as(f64, std.time.ns_per_s);
|
||||||
|
break :blk (total_bytes / (1024.0 * 1024.0)) / elapsed_s;
|
||||||
|
},
|
||||||
.force_report_allocs = force_report,
|
.force_report_allocs = force_report,
|
||||||
.is_container = is_container,
|
.is_container = is_container,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -258,17 +258,11 @@ pub fn parse_duration_ns(s: []const u8) !u64 {
|
|||||||
|
|
||||||
const value: u64 = try std.fmt.parseInt(u64, num_str, 10);
|
const value: u64 = try std.fmt.parseInt(u64, num_str, 10);
|
||||||
|
|
||||||
if (unit.len == 0 or std.mem.eql(u8, unit, "s")) {
|
return if (std.mem.eql(u8, unit, "ms")) value *| std.time.ns_per_ms
|
||||||
return value *| std.time.ns_per_s;
|
else if (std.mem.eql(u8, unit, "us") or std.mem.eql(u8, unit, "µs")) value *| std.time.ns_per_us
|
||||||
} else if (std.mem.eql(u8, unit, "ms")) {
|
else if (std.mem.eql(u8, unit, "ns")) value
|
||||||
return value *| std.time.ns_per_ms;
|
else if (unit.len == 0 or std.mem.eql(u8, unit, "s")) value *| std.time.ns_per_s
|
||||||
} else if (std.mem.eql(u8, unit, "us") or std.mem.eql(u8, unit, "µs")) {
|
else error.InvalidDuration;
|
||||||
return value *| std.time.ns_per_us;
|
|
||||||
} else if (std.mem.eql(u8, unit, "ns")) {
|
|
||||||
return value;
|
|
||||||
} else {
|
|
||||||
return error.InvalidDuration;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
test "parse_duration_ns" {
|
test "parse_duration_ns" {
|
||||||
|
|||||||
Reference in New Issue
Block a user