|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require "json" |
| 4 | +require "rbconfig" |
| 5 | +require_relative "support" |
| 6 | + |
| 7 | +class CountingWakeUp < SolidObjects::WakeUp |
| 8 | + def initialize |
| 9 | + super |
| 10 | + @count_mutex = Mutex.new |
| 11 | + @poll_count = 0 |
| 12 | + end |
| 13 | + |
| 14 | + def wait(timeout:, generation: nil) |
| 15 | + @count_mutex.synchronize { @poll_count += 1 } |
| 16 | + super |
| 17 | + end |
| 18 | + |
| 19 | + def reset_count |
| 20 | + @count_mutex.synchronize { @poll_count = 0 } |
| 21 | + end |
| 22 | + |
| 23 | + def poll_count |
| 24 | + @count_mutex.synchronize { @poll_count } |
| 25 | + end |
| 26 | +end |
| 27 | + |
| 28 | +def measure(interval:, warmup:, duration:, wake_up:) |
| 29 | + SolidObjects.configuration.polling_interval = interval |
| 30 | + SolidObjects.configuration.idle_polling_interval = 1.0 |
| 31 | + components = [ |
| 32 | + SolidObjects::Worker.new, |
| 33 | + SolidObjects::EffectExecutor.new, |
| 34 | + SolidObjects::ReminderScheduler.new, |
| 35 | + SolidObjects::BroadcastExecutor.new |
| 36 | + ] |
| 37 | + threads = components.map { |component| Thread.new { component.run } } |
| 38 | + |
| 39 | + sleep warmup |
| 40 | + wake_up.reset_count |
| 41 | + cpu_started_at = Process.times |
| 42 | + wall_started_at = Process.clock_gettime(Process::CLOCK_MONOTONIC) |
| 43 | + sleep duration |
| 44 | + wall_elapsed = Process.clock_gettime(Process::CLOCK_MONOTONIC) - wall_started_at |
| 45 | + cpu_finished_at = Process.times |
| 46 | + cpu_elapsed = cpu_finished_at.utime + cpu_finished_at.stime - |
| 47 | + cpu_started_at.utime - cpu_started_at.stime |
| 48 | + poll_count = wake_up.poll_count |
| 49 | + |
| 50 | + { |
| 51 | + polling_interval: interval, |
| 52 | + idle_polling_interval: SolidObjects.configuration.idle_polling_interval, |
| 53 | + current_intervals: components.map(&:current_polling_interval), |
| 54 | + polls: poll_count, |
| 55 | + polls_per_second: (poll_count / wall_elapsed).round(3), |
| 56 | + idle_cpu_percent: ((cpu_elapsed / wall_elapsed) * 100).round(3) |
| 57 | + } |
| 58 | +ensure |
| 59 | + components&.each(&:request_shutdown) |
| 60 | + threads&.each { |thread| thread.join(2) } |
| 61 | + components&.each(&:stop) |
| 62 | +end |
| 63 | + |
| 64 | +intervals = ENV.fetch("INTERVALS", "0.02,0.1,0.5").split(",").map do |value| |
| 65 | + Float(value).tap { |interval| raise ArgumentError, "intervals must be positive" unless interval.positive? } |
| 66 | +end |
| 67 | +warmup = Float(ENV.fetch("WARMUP", "3")) |
| 68 | +duration = Float(ENV.fetch("DURATION", "10")) |
| 69 | +raise ArgumentError, "warmup must be positive" unless warmup.positive? |
| 70 | +raise ArgumentError, "duration must be positive" unless duration.positive? |
| 71 | + |
| 72 | +SolidObjectsBenchmark.setup |
| 73 | +wake_up = CountingWakeUp.new |
| 74 | +SolidObjects.configuration.wake_up_adapter = wake_up |
| 75 | +database_version = ActiveRecord::Base.connection.select_value("SELECT sqlite_version()") |
| 76 | +results = intervals.map { |interval| measure(interval:, warmup:, duration:, wake_up:) } |
| 77 | +puts JSON.pretty_generate( |
| 78 | + measured_at: Time.now.utc.iso8601, |
| 79 | + package_version: SolidObjects::VERSION, |
| 80 | + runtime: { |
| 81 | + ruby: RUBY_DESCRIPTION, |
| 82 | + platform: RUBY_PLATFORM, |
| 83 | + cpu: RbConfig::CONFIG.fetch("host_cpu") |
| 84 | + }, |
| 85 | + database: { |
| 86 | + adapter: "sqlite", |
| 87 | + version: database_version, |
| 88 | + path: SolidObjectsBenchmark::DATABASE_PATH |
| 89 | + }, |
| 90 | + methodology: { |
| 91 | + roles: %w[actors effects reminders broadcasts], |
| 92 | + warmup_seconds: warmup, |
| 93 | + duration_seconds: duration, |
| 94 | + cpu_percent: "process user plus system CPU time divided by wall time" |
| 95 | + }, |
| 96 | + results: |
| 97 | +) |
| 98 | +SolidObjectsBenchmark.teardown |
0 commit comments