25 / 29 lines covered (86.2%)
1unit module BDD::Behave::Benchmark::Baseline;23our constant BASELINE-VERSION = 1;4our constant HEADER-LINE = '# behave-benchmark-baseline v1';5our constant COLUMN-LINE = 'description key iterations min max mean median total';67our class BaselineEntry {8 has Str $.description is required;9 has Str $.key is required;10 has Int $.iterations is required;11 has Real $.min is required;12 has Real $.max is required;13 has Real $.mean is required;14 has Real $.median is required;15 has Real $.total is required;1617 method to-line(--> Str) {18 join "\t",19 $!description,20 $!key,21 $!iterations.Str,22 $!min.Str,23 $!max.Str,24 $!mean.Str,25 $!median.Str,26 $!total.Str;27 }2829 method from-line(Str $line --> BaselineEntry) {30 my @fields = $line.split("\t");31 die "baseline line has wrong column count (expected 8, got @fields.elems()): $line"32 unless @fields.elems == 8;33 BaselineEntry.new(34 :description(@fields[0]),35 :key(@fields[1]),36 :iterations(@fields[2].Int),37 :min(@fields[3].Real),38 :max(@fields[4].Real),39 :mean(@fields[5].Real),40 :median(@fields[6].Real),41 :total(@fields[7].Real),42 );43 }44}4546our sub serialize(@entries --> Str) {47 my @lines = HEADER-LINE, COLUMN-LINE;48 @lines.append: @entries.map(*.to-line);49 @lines.join("\n") ~ "\n";50}5152our sub parse(Str $content --> Array[BaselineEntry]) {53 my @entries;54 my @lines = $content.lines;5556 die "empty baseline content"57 unless @lines.elems;5859 die "baseline missing header (expected '{HEADER-LINE}')"60 unless @lines[0] eq HEADER-LINE;6162 my $saw-columns = False;63 for @lines[1..*] -> $raw {64 next unless $raw.chars;65 next if $raw.starts-with('#');66 unless $saw-columns {67 die "baseline missing column header (expected '{COLUMN-LINE}')"68 unless $raw eq COLUMN-LINE;69 $saw-columns = True;70 next;71 }72 @entries.push: BaselineEntry.from-line($raw);73 }7475 my BaselineEntry @result = @entries;76 @result;77}7879our sub load(IO::Path $path --> Array[BaselineEntry]) {80 die "baseline file does not exist: $path"81 unless $path.e;82 parse($path.slurp);83}8485our sub save(IO::Path $path, @entries --> Nil) {86 $path.spurt: serialize(@entries);87}8889our sub index-by-key(@entries --> Hash) {90 my %h;91 for @entries -> $e {92 %h{$e.description}{$e.key} = $e;93 }94 %h;95}