active_model_serializers/test/benchmark/bm_caching.rb
Benjamin Fleischer e554ba23d2 Nicer debug; compare caching by serializer, grouped by caching on/off
bundle exec bin/bench_regression a5eaf6cd7a7fed42d9e64777753a1762e187eadc 1033b711c7d7c231bb5b832e7dfe7f99389f22c4 --pattern bm_caching

["perf/only_calc_associations_once", "a5eaf6cd7a7fed42d9e64777753a1762e187eadc", "1033b711c7d7c231bb5b832e7dfe7f99389f22c4", "a5eaf6c"]
  "version": "0.10.0.rc5",
  "rails_version": "4.2.6",
  "benchmark_run[environment]": "2.2.2p95",

Note: checking out 'a5eaf6cd7a7fed42d9e64777753a1762e187eadc'.

HEAD is now at a5eaf6c... Nicer debug; compare caching by serializer, grouped by caching on/off

caching on: caching serializers: gc off 783.6956866669746/ips; 1355 objects
caching on: non-caching serializers: gc off 798.8629770532652/ips; 1257 objects
caching off: caching serializers: gc off 682.3661326140281/ips; 1355 objects
caching off: non-caching serializers: gc off 721.2175067555897/ips; 1257 objects

HEAD is now at 1033b71... Merge pull request #1638 from bf4/caching_redux

caching on: caching serializers: gc off 570.6905948477781/ips; 1803 objects
caching on: non-caching serializers: gc off 822.8418206976623/ips; 1257 objects
caching off: caching serializers: gc off 523.4174806572001/ips; 1803 objects
caching off: non-caching serializers: gc off 747.6026493097758/ips; 1257 objects
2016-04-18 11:55:20 -05:00

114 lines
3.5 KiB
Ruby

require_relative './benchmarking_support'
require_relative './app'
# https://github.com/ruby-bench/ruby-bench-suite/blob/8ad567f7e43a044ae48c36833218423bb1e2bd9d/rails/benchmarks/actionpack_router.rb
class ApiAssertion
include Benchmark::ActiveModelSerializers::TestMethods
BadRevisionError = Class.new(StandardError)
def valid?
caching = get_caching
caching[:body].delete('meta')
non_caching = get_non_caching
non_caching[:body].delete('meta')
assert_responses(caching, non_caching)
rescue BadRevisionError => e
msg = { error: e.message }
STDERR.puts msg
STDOUT.puts msg
exit 1
end
def get_status(on_off = 'on'.freeze)
get("/status/#{on_off}")
end
def clear
get('/clear')
end
def get_caching(on_off = 'on'.freeze)
get("/caching/#{on_off}")
end
def get_non_caching(on_off = 'on'.freeze)
get("/non_caching/#{on_off}")
end
def debug(msg = '')
if block_given? && ENV['DEBUG'] =~ /\Atrue|on|0\z/i
STDERR.puts yield
else
STDERR.puts msg
end
end
private
def assert_responses(caching, non_caching)
assert_equal(caching[:code], 200, "Caching response failed: #{caching}")
assert_equal(caching[:body], expected, "Caching response format failed: \n+ #{caching[:body]}\n- #{expected}")
assert_equal(caching[:content_type], 'application/json; charset=utf-8', "Caching response content type failed: \n+ #{caching[:content_type]}\n- application/json")
assert_equal(non_caching[:code], 200, "Non caching response failed: #{non_caching}")
assert_equal(non_caching[:body], expected, "Non Caching response format failed: \n+ #{non_caching[:body]}\n- #{expected}")
assert_equal(non_caching[:content_type], 'application/json; charset=utf-8', "Non caching response content type failed: \n+ #{non_caching[:content_type]}\n- application/json")
end
def get(url)
response = request(:get, url)
{ code: response.status, body: JSON.load(response.body), content_type: response.content_type }
end
def expected
@expected ||=
{
'post' => {
'id' => 1337,
'title' => 'New Post',
'body' => 'Body',
'comments' => [
{
'id' => 1,
'body' => 'ZOMG A COMMENT'
}
],
'blog' => {
'id' => 999,
'name' => 'Custom blog'
},
'author' => {
'id' => 42,
'first_name' => 'Joao',
'last_name' => 'Moura'
}
}
}
end
def assert_equal(expected, actual, message)
return true if expected == actual
if ENV['FAIL_ASSERTION'] =~ /\Atrue|on|0\z/i # rubocop:disable Style/GuardClause
fail BadRevisionError, message
else
STDERR.puts message unless ENV['SUMMARIZE']
end
end
end
assertion = ApiAssertion.new
assertion.valid?
assertion.debug { assertion.get_status }
time = 10
{
'caching on: caching serializers: gc off' => { disable_gc: true, send: [:get_caching, 'on'] },
'caching on: non-caching serializers: gc off' => { disable_gc: true, send: [:get_non_caching, 'on'] },
'caching off: caching serializers: gc off' => { disable_gc: true, send: [:get_caching, 'off'] },
'caching off: non-caching serializers: gc off' => { disable_gc: true, send: [:get_non_caching, 'off'] }
}.each do |label, options|
assertion.clear
Benchmark.ams(label, time: time, disable_gc: options[:disable_gc]) do
assertion.send(*options[:send])
end
assertion.debug { assertion.get_status(options[:send][-1]) }
end