benchmark_operations

in benchmark/gem.rb [145:210]


    def benchmark_operations(legacy_report_data, report_data)
      require_relative 'test_data'
      return unless gem_name && client_module_name && operation_benchmarks

      require gem_name

      client_klass = Kernel.const_get(client_module_name).const_get(:Client)

      legacy_report_data[:client_init_ms] = Benchmark.measure_time(300) do
        client_klass.new(stub_responses: true)
      end

      report_data << Result.new(
        "#{service_name(gem_name)}.client.init.time",
        "The time it takes to initialize the #{service_name(gem_name)} client.",
        legacy_report_data[:client_init_ms]
      ).format

      values = legacy_report_data[:client_init_ms]
      ms = format('%.2f', (values.sum(0.0) / values.size))
      puts "\t\t#{gem_name} client init avg: #{ms} ms"

      
      operation_benchmarks.each do |test_name, test_def|
        client = client_klass.new(stub_responses: true)
        req = test_def[:setup].call(client)

        op_name = test_name.to_s.split('_').join
        op_name_pascal = test_name.to_s.split('_').map(&:capitalize).join

        
        2.times { test_def[:test].call(client, req) }

        mem_allocated = 0
        unless defined?(JRUBY_VERSION)
          r = ::MemoryProfiler.report { test_def[:test].call(client, req) }
          mem_allocated = legacy_report_data["#{test_name}_allocated_kb"] =
            r.total_allocated_memsize / 1024.0

          report_data << Result.new(
            "#{service_name(gem_name)}.#{op_name}.allocated.size",
            'The amount of memory allocated to perform the ' \
            "#{op_name_pascal} operation.",
            [mem_allocated / 1024.0]
          ).format
        end

        n = test_def[:n] || 300
        values = Benchmark.measure_time(n) do
          test_def[:test].call(client, req)
        end
        legacy_report_data["#{test_name}_ms"] = values

        report_data << Result.new(
          "#{service_name(gem_name)}.#{op_name}.time",
          "The time it takes to perform the #{op_name_pascal} operation.",
          values
        ).format

        ms = format('%.2f', (values.sum(0.0) / values.size))
        puts "\t\t#{test_name} avg: #{ms} ms\t" \
             "mem_allocated: #{format('%.2f', mem_allocated)} kb"
      end
      
    end