-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbenchmak_symb_keys.rb
56 lines (44 loc) · 1.21 KB
/
benchmak_symb_keys.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
# Here I compare mysql query with "symbolize_keys: true" and "symbolize_keys: false"
require 'bundler/setup'
require "mysql2"
require "active_record"
require 'light_record'
require 'process_memory'
require 'benchmark'
require 'benchmark/ips'
client = Mysql2::Client.new(
adapter: 'mysql2',
database: 'light_record',
host: 'localhost',
username: 'root',
password: ''
)
sql = "select s1.*, s2.* from sample s1 left outer join questions s2 on 1=1 limit 100000"
Benchmark.bm(14) do |x|
x.report("Symbols") do
result = client.query(sql, symbolize_keys: true, cache_rows: false)
end
x.report("Strings") do
result = client.query(sql, symbolize_keys: false, cache_rows: false)
end
end
GC.disable
def measure_memory(title)
puts "~~~"
puts "Testing: #{title}"
recorder = ProcessMemory.start_recording
yield
recorder.stop
puts "Time spent: #{Time.now - recorder.instance_variable_get(:@start_time)} sec"
puts recorder.report_per_second_pretty
end
measure_memory("Symbols") do
sleep 0.5
result = client.query(sql, symbolize_keys: true, cache_rows: false)
sleep 0.5
end
measure_memory("String") do
sleep 0.5
result = client.query(sql, symbolize_keys: false, cache_rows: false)
sleep 0.5
end