active_model_serializers/test/dummy/benchmarking_support.rb
Benjamin Fleischer 4cc454d49b Setup benchmarking structure
- Setup dummy app files in `test/dummy`
- Setup dummy test server `bin/serve_dummy
  - Note:  Serializer caching can be completely disabled by passing in
  `CACHE_ON=off bin/serve_dummy start` since Serializer#_cache is only
  set at boot.
- run with
  - ./bin/bench
  - `bin/bench` etc adapted from ruby-bench-suite
  - target files are `test/dummy/bm_*.rb`. Just add another to run it.
  - benchmark cache/no cache
  - remove rake dependency that loads unnecessary files
- remove git gem dependency
  - Running over revisions to be added in subsequent PR
2016-03-09 12:42:25 -06:00

67 lines
1.7 KiB
Ruby

require 'benchmark/ips'
require 'json'
# Add benchmarking runner from ruby-bench-suite
# https://github.com/ruby-bench/ruby-bench-suite/blob/master/rails/benchmarks/support/benchmark_rails.rb
module Benchmark
module ActiveModelSerializers
module TestMethods
def request(method, path)
response = Rack::MockRequest.new(DummyApp).send(method, path)
if response.status.in?([404, 500])
fail "omg, #{method}, #{path}, '#{response.status}', '#{response.body}'"
end
response
end
end
# extend Benchmark with an `ams` method
def ams(label = nil, time:, disable_gc: true, warmup: 3, &block)
fail ArgumentError.new, 'block should be passed' unless block_given?
if disable_gc
GC.disable
else
GC.enable
end
report = Benchmark.ips(time, warmup, true) do |x|
x.report(label) { yield }
end
entry = report.entries.first
output = {
label: label,
version: ::ActiveModel::Serializer::VERSION.to_s,
iterations_per_second: entry.ips,
iterations_per_second_standard_deviation: entry.stddev_percentage,
total_allocated_objects_per_iteration: count_total_allocated_objects(&block)
}.to_json
puts output
output
end
def count_total_allocated_objects
if block_given?
key =
if RUBY_VERSION < '2.2'
:total_allocated_object
else
:total_allocated_objects
end
before = GC.stat[key]
yield
after = GC.stat[key]
after - before
else
-1
end
end
end
extend Benchmark::ActiveModelSerializers
end