This repository has been archived by the owner on Nov 24, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheader_aset_raw.rb
89 lines (77 loc) · 1.79 KB
/
header_aset_raw.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# frozen_string_literal: true
class HeaderHash < Hash
def self.new(hash={})
HeaderHash === hash ? hash : super(hash)
end
def initialize(hash={})
super()
@names = {}
hash.each { |k, v| self[k] = v }
end
# on dup/clone, we need to duplicate @names hash
def initialize_copy(other)
super
@names = other.names.dup
end
def [](k)
super(k) || super(@names[k.downcase])
end
def []=(k, v)
canonical = k.downcase.freeze
delete k if @names[canonical] && @names[canonical] != k # .delete is expensive, don't invoke it unless necessary
@names[canonical] = k
super k, v
end
def delete(k)
canonical = k.downcase
result = super @names.delete(canonical)
result
end
end
def warmup(loop_count)
header = HeaderHash.new
i = 0
while i < loop_count
header['foo'] = 'bar'
i += 1
end
end
def run(loop_count)
headers = loop_count.times.map { HeaderHash.new }
before = Process.clock_gettime(Process::CLOCK_MONOTONIC)
i = 0
while i < loop_count
header = headers[i]
header['Content-Type'] = 'text/html;charset=utf-8'
header['Content-Length'] = '25'
header['X-XSS-Protection'] = '1; mode=block'
header['X-Content-Type-Options'] = 'nosniff'
header['X-Frame-Options'] = 'SAMEORIGIN'
i += 1
end
after = Process.clock_gettime(Process::CLOCK_MONOTONIC)
puts "==> #{after-before} (#{loop_count} times)"
end
warmup(10000)
if RubyVM::MJIT.enabled?
RubyVM::MJIT.pause
warmup(1)
RubyVM::MJIT.resume
RubyVM::MJIT.pause
end
if ENV.key?('PERF')
require 'shellwords'
pid = Process.spawn('perf', *ENV['PERF'].shellsplit, '-p', Process.pid.to_s)
end
run(1000000)
run(1000000)
run(1000000)
run(1000000)
run(1000000)
run(1000000)
run(1000000)
run(1000000)
if pid
Process.kill(:INT, pid)
Process.wait(pid)
end