|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "benchmark" |
| 4 | +require "concurrent-ruby" |
| 5 | +require "connection_pool" |
| 6 | +require "pg_ephemeral" |
| 7 | + |
| 8 | +require_relative "../en57" |
| 9 | + |
| 10 | +module En57 |
| 11 | + module Benchmark |
| 12 | + Result = Data.define(:name, :runs, :mean, :stddev, :verified) |
| 13 | + |
| 14 | + class Table |
| 15 | + def format(results) |
| 16 | + rows = results.select(&:verified) |
| 17 | + return "" if rows.empty? |
| 18 | + |
| 19 | + header = ["Scenario", "Runs", "Mean latency", "Stddev"] |
| 20 | + body = |
| 21 | + rows.map do |result| |
| 22 | + [ |
| 23 | + result.name, |
| 24 | + result.runs.to_s, |
| 25 | + milliseconds(result.mean), |
| 26 | + milliseconds(result.stddev), |
| 27 | + ] |
| 28 | + end |
| 29 | + widths = header.zip(*body).map { |values| values.map(&:length).max } |
| 30 | + rule = "+-#{widths.map { "-" * it }.join("-+-")}-+" |
| 31 | + |
| 32 | + [ |
| 33 | + rule, |
| 34 | + table_row(header, widths, %i[left left left left]), |
| 35 | + rule, |
| 36 | + *body.map { table_row(it, widths, %i[left right right right]) }, |
| 37 | + rule, |
| 38 | + ].join("\n") |
| 39 | + end |
| 40 | + |
| 41 | + private |
| 42 | + |
| 43 | + def table_row(values, widths, alignments) |
| 44 | + cells = |
| 45 | + values |
| 46 | + .zip(widths, alignments) |
| 47 | + .map do |value, width, alignment| |
| 48 | + alignment == :right ? value.rjust(width) : value.ljust(width) |
| 49 | + end |
| 50 | + |
| 51 | + "| #{cells.join(" | ")} |" |
| 52 | + end |
| 53 | + |
| 54 | + def milliseconds(seconds) = Kernel.format("%.2f ms", seconds * 1000) |
| 55 | + end |
| 56 | + |
| 57 | + class Measurement |
| 58 | + def self.from(samples) |
| 59 | + mean = samples.sum.fdiv(samples.size) |
| 60 | + |
| 61 | + new( |
| 62 | + mean:, |
| 63 | + stddev: |
| 64 | + Math.sqrt( |
| 65 | + samples.sum { |sample| (sample - mean)**2 }.fdiv(samples.size), |
| 66 | + ), |
| 67 | + ) |
| 68 | + end |
| 69 | + |
| 70 | + attr_reader :mean, :stddev |
| 71 | + |
| 72 | + def initialize(mean:, stddev:) |
| 73 | + @mean = mean |
| 74 | + @stddev = stddev |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + class Scenario |
| 79 | + def initialize( |
| 80 | + name:, |
| 81 | + database_url:, |
| 82 | + measure:, |
| 83 | + runs:, |
| 84 | + concurrency:, |
| 85 | + batch_size: |
| 86 | + ) |
| 87 | + @name = name |
| 88 | + @batch_size = batch_size |
| 89 | + @concurrency = concurrency |
| 90 | + @database_url = database_url |
| 91 | + @measure = measure |
| 92 | + @runs = runs |
| 93 | + end |
| 94 | + |
| 95 | + attr_reader :name, :runs |
| 96 | + |
| 97 | + def run |
| 98 | + @runs.times { call } |
| 99 | + verify |
| 100 | + end |
| 101 | + |
| 102 | + private |
| 103 | + |
| 104 | + def call = nil |
| 105 | + def verify = true |
| 106 | + |
| 107 | + def concurrently(concurrency) |
| 108 | + Array |
| 109 | + .new(concurrency) do |
| 110 | + Thread.new do |
| 111 | + Thread.report_on_exception = false |
| 112 | + yield |
| 113 | + end |
| 114 | + end |
| 115 | + .each(&:join) |
| 116 | + end |
| 117 | + end |
| 118 | + |
| 119 | + class Runner |
| 120 | + def self.classic |
| 121 | + new( |
| 122 | + formatter: Table.new, |
| 123 | + scenarios: { |
| 124 | + "concurrent-append-non-conflicting-tags" => ->( |
| 125 | + database_url, |
| 126 | + measure |
| 127 | + ) do |
| 128 | + ConcurrentAppendNonConflictingTags.new( |
| 129 | + name: "Concurrent append, non-conflicting tags", |
| 130 | + database_url:, |
| 131 | + measure:, |
| 132 | + runs: ENV.fetch("BENCHMARK_RUNS", 1), |
| 133 | + concurrency: 10, |
| 134 | + batch_size: 100, |
| 135 | + ) |
| 136 | + end, |
| 137 | + "concurrent-append-no-fail-if" => ->(database_url, measure) do |
| 138 | + ConcurrentAppendNoFailIf.new( |
| 139 | + name: "Concurrent append, no fail_if", |
| 140 | + database_url:, |
| 141 | + measure:, |
| 142 | + runs: ENV.fetch("BENCHMARK_RUNS", 1), |
| 143 | + concurrency: 10, |
| 144 | + batch_size: 100, |
| 145 | + ) |
| 146 | + end, |
| 147 | + }, |
| 148 | + ) |
| 149 | + end |
| 150 | + |
| 151 | + def initialize(scenarios:, formatter:) |
| 152 | + @formatter = formatter |
| 153 | + @scenarios = scenarios |
| 154 | + end |
| 155 | + |
| 156 | + def run |
| 157 | + @formatter.format( |
| 158 | + @scenarios.map do |instance_name, mk_scenario| |
| 159 | + PgEphemeral.with_server(instance_name:) do |server| |
| 160 | + samples = [] |
| 161 | + scenario = |
| 162 | + mk_scenario.call( |
| 163 | + server.url, |
| 164 | + ->(&block) { samples << ::Benchmark.realtime { block.call } }, |
| 165 | + ) |
| 166 | + verified = scenario.run |
| 167 | + measurement = Measurement.from(samples) |
| 168 | + |
| 169 | + Result.new( |
| 170 | + name: scenario.name, |
| 171 | + runs: scenario.runs, |
| 172 | + mean: measurement.mean, |
| 173 | + stddev: measurement.stddev, |
| 174 | + verified:, |
| 175 | + ) |
| 176 | + end |
| 177 | + end, |
| 178 | + ) |
| 179 | + end |
| 180 | + end |
| 181 | + |
| 182 | + class ConcurrentAppendNoFailIf < Scenario |
| 183 | + def initialize(...) |
| 184 | + super |
| 185 | + @event_store = |
| 186 | + EventStore.for_pooled_pg(@database_url, max_connections: @concurrency) |
| 187 | + end |
| 188 | + |
| 189 | + private |
| 190 | + |
| 191 | + def call |
| 192 | + type = "event_benchmarked" |
| 193 | + barrier = Concurrent::CyclicBarrier.new(@concurrency) |
| 194 | + |
| 195 | + concurrently(@concurrency) do |
| 196 | + tags = %W[writer:#{SecureRandom.hex(4)}] |
| 197 | + scope = @event_store.read.of_type(type).with_tag(tags) |
| 198 | + events = |
| 199 | + Array.new(@batch_size) { En57::Event.new(type: type, tags: tags) } |
| 200 | + |
| 201 | + barrier.wait |
| 202 | + |
| 203 | + @measure.call do |
| 204 | + begin |
| 205 | + @event_store.append(events) |
| 206 | + rescue AppendConditionViolated |
| 207 | + retry |
| 208 | + end |
| 209 | + end |
| 210 | + end |
| 211 | + end |
| 212 | + |
| 213 | + def verify = |
| 214 | + @event_store.read.each.to_a.size == @runs * @concurrency * @batch_size |
| 215 | + end |
| 216 | + |
| 217 | + class ConcurrentAppendNonConflictingTags < Scenario |
| 218 | + def initialize(...) |
| 219 | + super |
| 220 | + @event_store = |
| 221 | + EventStore.for_pooled_pg(@database_url, max_connections: @concurrency) |
| 222 | + end |
| 223 | + |
| 224 | + private |
| 225 | + |
| 226 | + def call |
| 227 | + type = "event_benchmarked" |
| 228 | + barrier = Concurrent::CyclicBarrier.new(@concurrency) |
| 229 | + |
| 230 | + concurrently(@concurrency) do |
| 231 | + tags = %W[writer:#{SecureRandom.hex(4)}] |
| 232 | + scope = @event_store.read.of_type(type).with_tag(tags) |
| 233 | + events = |
| 234 | + Array.new(@batch_size) { En57::Event.new(type: type, tags: tags) } |
| 235 | + |
| 236 | + barrier.wait |
| 237 | + |
| 238 | + @measure.call do |
| 239 | + begin |
| 240 | + @event_store.append(events, fail_if: scope.after(position = 0)) |
| 241 | + rescue AppendConditionViolated |
| 242 | + retry |
| 243 | + end |
| 244 | + end |
| 245 | + end |
| 246 | + end |
| 247 | + |
| 248 | + def verify = |
| 249 | + @event_store.read.each.to_a.size == @runs * @concurrency * @batch_size |
| 250 | + end |
| 251 | + end |
| 252 | +end |
0 commit comments