|
| 1 | +#!/usr/bin/env jq -crnf |
| 2 | + |
| 3 | +def fmt: . * 1000 | round | . / 1000; |
| 4 | + |
| 5 | +def hist($max; $bins; src): reduce src as $in ( |
| 6 | + { bins: $bins, max: $max, hist: [range(0; $bins) | 0] }; |
| 7 | + .hist[fmin($in / $max * $bins | floor; $bins - 1)] += 1 |
| 8 | +); |
| 9 | + |
| 10 | +# takes a hist and transforms it into a CDF string |
| 11 | +def cdf: last(.hist[]) as $sum | |
| 12 | + . as { bins: $b, max: $m, hist: $h } | |
| 13 | + ($h | $m / length) as $bin_size | |
| 14 | + $h | keys | reduce .[] as $idx ( |
| 15 | + [0, []]; |
| 16 | + ($h[$idx] / $sum|fmt) as $y | |
| 17 | + if $y == .[0] then . |
| 18 | + else (.[1] += ["(\($idx * $bin_size|fmt),\($y))"]) | .[0] = $y |
| 19 | + end |
| 20 | + ) | .[1] | "CDF[\(join(","))]"; |
| 21 | + |
| 22 | +# `gen`: extract [type, id, time] of data generation, or empty |
| 23 | +# `recv`: extract [type, id, time] of data reception, or empty |
| 24 | +def read_log(in; gen; recv): reduce in as $in ( |
| 25 | + {}; # { [type]: { [id]: [start, latency[]] }} |
| 26 | + def g: ($in | gen) as [$type, $id, $time] | |
| 27 | + setpath([$type, $id]; [$time, []]); |
| 28 | + def r: ($in | recv) as [$type, $id, $time] | |
| 29 | + .[$type][$id] |= . as [$t, $l] | [$t, $l + [$time - $t]]; |
| 30 | + g // r // . |
| 31 | +) | map_values( |
| 32 | + (map(.[1]|length)|max) as $max | |
| 33 | + map_values( |
| 34 | + .[1] | select(length == $max) | hist(10; 100; .[]) |
| 35 | + ) |
| 36 | +); |
| 37 | + |
| 38 | +def cumulative: .hist |= [foreach .[] as $in (0; . + $in; .)]; |
| 39 | + |
| 40 | +# turns an object with hist values into a single hist |
| 41 | +def hist_combine(combine): reduce .[] as $in ( |
| 42 | + null; |
| 43 | + if . == null then $in |
| 44 | + else .hist |= . as $h | keys | [.[] | [$h[.], $in.hist[.] // 0] | combine] |
| 45 | + end |
| 46 | +); |
| 47 | + |
| 48 | +def hist_min: hist_combine(fmin(.[0]; .[1])); |
| 49 | +def hist_max: hist_combine(fmax(.[0]; .[1])); |
| 50 | +def hist_avg: (1 / length) as $scale | hist_combine(.[0] + $scale * .[1]); |
| 51 | + |
| 52 | +def min_max: map(cumulative) | { |
| 53 | + min: (hist_min | cdf), |
| 54 | + max: (hist_max | cdf), |
| 55 | + avg: (hist_avg | cdf) |
| 56 | +}; |
| 57 | + |
| 58 | +def print: . as $all | [ |
| 59 | + paths(type == "string") | |
| 60 | + . as $path | |
| 61 | + "\(join("_")) := \($all | getpath($path))" |
| 62 | +] | join("\n"); |
| 63 | + |
| 64 | +read_log(limit(1000000; inputs); |
| 65 | + select(.event.tag=="generated")|[.event.kind, .event.id, .time_s]; |
| 66 | + select(.event.tag=="received")|[.event.kind, .event.id, .time_s]) |
| 67 | +| map_values(min_max) |
| 68 | +| print |
0 commit comments