class Gladys::Actions::Benchmark

def fetch_client_settings

def fetch_client_settings
  {
    cpu_count: Concurrent.processor_count,
    platform: RUBY_PLATFORM,
    ruby_version: RUBY_VERSION
  }
end

def fetch_metadata

def fetch_metadata
  {
    client: fetch_client_settings,
    postgres: fetch_postgres_settings
  }
end

def fetch_postgres_settings

optional so we should include a setting to disable it.
analyze how certain settings affect benchmark results. This is technically
Fetches metadata from the database so we can include it in the report and
def fetch_postgres_settings
  @database.select(:name, :setting).from(:pg_settings).all.to_h do |row|
    [row[:name], row[:setting]]
  end
rescue Sequel::DatabaseError => e
  Gladys.log_debug "Failed to fetch metadata, skipping... (Error: #{e.message})"
  {}
end

def optimize_database

def optimize_database
  Gladys.log("Running VACUUM ANALYZE before benchmark...")
  begin
    @database.run("VACUUM ANALYZE")
  rescue
    Gladys.log_debug "VACUUM ANALYZE failed, skipping..."
  end
end

def report

def report
  Report.new(
    script: @script,
    threads: @threads,
    time: @time,
    started_at: @started_at,
    finished_at: @started_at + @time,
    database_size: current_database_size,
    metrics: @metrics,
    metadata: fetch_metadata
  )
end

def run(time:, threads:)

def run(time:, threads:)
  @time = time
  @threads = threads
  optimize_database
  @context.preload_inputs
  @started_at = Time.now + WARM_UP_TIME
  @metrics = invoke(:benchmark, threads: @threads, should_stop: lambda do
    Time.now >= (@started_at + @time) + COOL_DOWN_TIME
  end)
end