summaryrefslogtreecommitdiff
path: root/test/pdf_bench.zig
diff options
context:
space:
mode:
Diffstat (limited to 'test/pdf_bench.zig')
-rw-r--r--test/pdf_bench.zig49
1 files changed, 20 insertions, 29 deletions
diff --git a/test/pdf_bench.zig b/test/pdf_bench.zig
index 0f09f72a..9054698b 100644
--- a/test/pdf_bench.zig
+++ b/test/pdf_bench.zig
@@ -9,10 +9,13 @@
//! ReleaseFast. Timed regions include MuPDF rasterization directly into the
//! allocator-owned RGBA output; tint scenarios additionally include the exact
//! in-place PDF transform used by the core. Checksum/free/reporting stay
-//! outside. "cold" means a fresh Document and MuPDF page cache (the OS
-//! filesystem cache is deliberately uncontrolled), while warm scenarios
-//! retain one Document.
+//! outside. "cold" means a fresh Document and MuPDF page cache, opened from
+//! the file's bytes (read once, untimed, as the view reads a file whole),
+//! while warm scenarios retain one Document.
const std = @import("std");
+const bench_util = @import("bench_util.zig");
+const nowNs = bench_util.nowNs;
+const fatal = bench_util.Fatal("pdf-bench").f;
const pdf = @import("mupdf");
pub const std_options: std.Options = .{ .log_level = .err };
@@ -25,6 +28,8 @@ const checksum_seed: u64 = 0x7061_7264_6573_5044;
const Config = struct {
path: []const u8 = "docs/design.pdf",
+ /// the file at `path`, read whole before anything is timed
+ bytes: []const u8 = "",
page: usize = 0,
reps: usize = default_reps,
warmup: usize = default_warmup,
@@ -137,9 +142,11 @@ const Result = struct {
pub fn main(init: std.process.Init) !void {
const args = try init.minimal.args.toSlice(init.arena.allocator());
- const config = parseArgs(args);
+ var config = parseArgs(args);
+ config.bytes = std.Io.Dir.cwd().readFileAlloc(init.io, config.path, gpa, .unlimited) catch |err|
+ fatal("cannot read {s}: {t}", .{ config.path, err });
- var probe = pdf.Document.open(config.path) catch |err|
+ var probe = pdf.Document.openBytes(config.bytes) catch |err|
fatal("cannot open {s}: {t}", .{ config.path, err });
const document_pages = probe.pages;
probe.deinit();
@@ -205,14 +212,6 @@ fn parseArgs(args: []const []const u8) Config {
return config;
}
-fn parsePositive(name: []const u8, text: []const u8, maximum: usize) usize {
- const value = std.fmt.parseUnsigned(usize, text, 10) catch
- fatal("{s} expects a positive integer", .{name});
- if (value == 0 or value > maximum)
- fatal("{s} must be in 1..{d}", .{ name, maximum });
- return value;
-}
-
fn usage() noreturn {
printUsage();
std.process.exit(2);
@@ -226,13 +225,6 @@ fn printUsage() void {
);
}
-fn nowNs() u64 {
- var ts: std.c.timespec = undefined;
- _ = std.c.clock_gettime(.MONOTONIC, &ts);
- return @as(u64, @intCast(ts.sec)) *| 1_000_000_000 +|
- @as(u64, @intCast(ts.nsec));
-}
-
fn measure(
config: Config,
profile_index: usize,
@@ -251,7 +243,7 @@ fn measure(
if (scenario.lifecycle == .cold_document_page) {
for (0..rounds) |round| {
const timed = try timeCold(
- config.path,
+ config.bytes,
config.page,
profile.request,
scenario.decoration,
@@ -265,7 +257,7 @@ fn measure(
}
}
} else {
- var document = try pdf.Document.open(config.path);
+ var document = try pdf.Document.openBytes(config.bytes);
defer document.deinit();
for (0..rounds) |round| {
const timed = switch (scenario.lifecycle) {
@@ -310,14 +302,14 @@ fn measure(
}
fn timeCold(
- path: []const u8,
+ bytes: []const u8,
page: usize,
request: pdf.RenderRequest,
decoration: Decoration,
tint: pdf.TintMode,
) !Timed {
const started = nowNs();
- var document = try pdf.Document.open(path);
+ var document = try pdf.Document.openBytes(bytes);
errdefer document.deinit();
const rendered = try renderPage(&document, page, request, decoration, tint);
const elapsed = @max(@as(u64, 1), nowNs() -| started);
@@ -376,8 +368,8 @@ fn renderPage(
tint: pdf.TintMode,
) !pdf.Render {
const rendered = switch (decoration) {
- .plain => document.renderAt(gpa, page, request),
- .highlighted => document.renderWithHighlightsAt(gpa, page, request, &highlights),
+ .plain => document.render(gpa, page, request, &.{}),
+ .highlighted => document.render(gpa, page, request, &highlights),
};
const fresh = try rendered;
errdefer gpa.free(fresh.rgba);
@@ -617,7 +609,6 @@ fn reportJson(
std.Io.File.stdout().writeStreamingAll(io, out.buffered()) catch {};
}
-fn fatal(comptime format: []const u8, args: anytype) noreturn {
- std.debug.print("pdf-bench: " ++ format ++ "\n", args);
- std.process.exit(1);
+fn parsePositive(name: []const u8, text: []const u8, maximum: usize) usize {
+ return bench_util.parsePositive(fatal, name, text, maximum);
}