|
| 1 | +# frozen_string_literal: true |
| 2 | +# typed: false |
| 3 | + |
| 4 | +# Used to quickly run benchmark under RSpec as part of the usual test suite, to validate it didn't bitrot |
| 5 | +VALIDATE_BENCHMARK_MODE = ENV['VALIDATE_BENCHMARK'] == 'true' |
| 6 | + |
| 7 | +return unless __FILE__ == $PROGRAM_NAME || VALIDATE_BENCHMARK_MODE |
| 8 | + |
| 9 | +require 'benchmark/ips' |
| 10 | +require 'ddtrace' |
| 11 | +require 'webrick' |
| 12 | +require_relative 'dogstatsd_reporter' |
| 13 | + |
| 14 | +class TracingHttpTransportBenchmark |
| 15 | + def initialize |
| 16 | + @port = Datadog::Transport::HTTP::DO_NOT_USE_ENVIRONMENT_AGENT_SETTINGS.port |
| 17 | + @transport = Datadog::Transport::HTTP.default |
| 18 | + @spans = test_traces(50) |
| 19 | + end |
| 20 | + |
| 21 | + def start_fake_webserver |
| 22 | + ready_queue = Queue.new |
| 23 | + |
| 24 | + require 'webrick' |
| 25 | + |
| 26 | + server = WEBrick::HTTPServer.new( |
| 27 | + Port: @port, |
| 28 | + StartCallback: -> { ready_queue.push(1) } |
| 29 | + ) |
| 30 | + server_proc = proc do |req, res| |
| 31 | + res.body = '{}' |
| 32 | + end |
| 33 | + |
| 34 | + server.mount_proc('/', &server_proc) |
| 35 | + Thread.new { server.start } |
| 36 | + ready_queue.pop |
| 37 | + end |
| 38 | + |
| 39 | + # Return some test traces |
| 40 | + def test_traces(n, service: 'test-app', resource: '/traces', type: 'web') |
| 41 | + traces = [] |
| 42 | + |
| 43 | + n.times do |
| 44 | + trace_op = Datadog::Tracing::TraceOperation.new |
| 45 | + |
| 46 | + trace_op.measure('client.testing', service: service, resource: resource, type: type) do |
| 47 | + trace_op.measure('client.testing', service: service, resource: resource, type: type) do |
| 48 | + end |
| 49 | + end |
| 50 | + |
| 51 | + traces << trace_op.flush! |
| 52 | + end |
| 53 | + |
| 54 | + traces |
| 55 | + end |
| 56 | + |
| 57 | + def run_benchmark |
| 58 | + Benchmark.ips do |x| |
| 59 | + benchmark_time = VALIDATE_BENCHMARK_MODE ? { time: 0.01, warmup: 0 } : { time: 70, warmup: 2 } |
| 60 | + x.config(**benchmark_time, suite: report_to_dogstatsd_if_enabled_via_environment_variable(benchmark_name: 'tracing_http_transport')) |
| 61 | + |
| 62 | + x.report("http_transport #{ENV['CONFIG']}") do |
| 63 | + run_once |
| 64 | + end |
| 65 | + |
| 66 | + x.save! 'tmp/tracing-http-transport-results.json' unless VALIDATE_BENCHMARK_MODE |
| 67 | + x.compare! |
| 68 | + end |
| 69 | + end |
| 70 | + |
| 71 | + def run_forever |
| 72 | + while true |
| 73 | + 100.times { run_once } |
| 74 | + print '.' |
| 75 | + end |
| 76 | + end |
| 77 | + |
| 78 | + def run_once |
| 79 | + success = @transport.send_traces(@spans) |
| 80 | + |
| 81 | + raise('Unexpected: Export failed') unless success |
| 82 | + end |
| 83 | +end |
| 84 | + |
| 85 | +puts "Current pid is #{Process.pid}" |
| 86 | + |
| 87 | +TracingHttpTransportBenchmark.new.instance_exec do |
| 88 | + if ARGV.include?('--forever') |
| 89 | + run_forever |
| 90 | + else |
| 91 | + run_benchmark |
| 92 | + end |
| 93 | +end |
0 commit comments