From 586aa14caa4106ac1085bad64d33baf7065ec3bf Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Thu, 26 Oct 2023 14:30:14 +1300 Subject: [PATCH] Lower case normalization benchmark. --- examples/header-lowercase/benchmark.rb | 43 ++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 examples/header-lowercase/benchmark.rb diff --git a/examples/header-lowercase/benchmark.rb b/examples/header-lowercase/benchmark.rb new file mode 100644 index 00000000..58d92fb7 --- /dev/null +++ b/examples/header-lowercase/benchmark.rb @@ -0,0 +1,43 @@ +require 'benchmark/ips' + +class NormalizedHeaders + def initialize(fields) + @fields = fields + end + + def [](key) + @fields[key.downcase] + end +end + +class Headers + def initialize(fields) + @fields = fields + end + + def [](key) + @fields[key] + end +end + +FIELDS = { + 'content-type' => 'text/html', + 'content-length' => '127889', + 'accept-ranges' => 'bytes', + 'date' => 'Tue, 14 Jul 2015 22:00:02 GMT', + 'via' => '1.1 varnish', + 'age' => '0', + 'connection' => 'keep-alive', + 'x-served-by' => 'cache-iad2125-IAD', +} + +NORMALIZED_HEADERS = NormalizedHeaders.new(FIELDS) +HEADERS = Headers.new(FIELDS) + +Benchmark.ips do |x| + x.report('NormalizedHeaders[Content-Type]') { NORMALIZED_HEADERS['Content-Type'] } + x.report('NormalizedHeaders[content-type]') { NORMALIZED_HEADERS['content-type'] } + x.report('Headers') { HEADERS['content-type'] } + + x.compare! +end