-
Notifications
You must be signed in to change notification settings - Fork 115
/
benchmark.rake
139 lines (114 loc) · 3.26 KB
/
benchmark.rake
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# frozen_string_literal: true
# Run using: rake --rakefile benchmark.rake [tasks]
$LOAD_PATH << File.expand_path(File.join(File.dirname(__FILE__), "lib"))
require "benchmark"
require "benchmark/ips"
require "appsignal"
def process_rss
`ps -o rss= -p #{Process.pid}`.to_i
end
GC.disable
task :default => :"benchmark:all"
namespace :benchmark do
task :all => [:memory_inactive, :memory_active, :ips]
task :memory_inactive do
puts "Memory benchmark with AppSignal off"
ENV["APPSIGNAL_PUSH_API_KEY"] = nil
run_benchmark
end
task :memory_active do
puts "Memory benchmark with AppSignal on"
ENV["APPSIGNAL_PUSH_API_KEY"] = "something"
run_benchmark
end
task :ips do
puts "Iterations per second benchmark"
start_agent
Benchmark.ips do |x|
x.config(
:time => 5,
:warmup => 2
)
x.report("monitor transaction inactive") do |times|
ENV["APPSIGNAL_PUSH_API_KEY"] = nil
monitor_transaction("transaction_#{times}")
end
x.report("monitor transaction active") do |times|
ENV["APPSIGNAL_PUSH_API_KEY"] = "something"
monitor_transaction("transaction_#{times}")
end
x.compare!
end
end
end
def start_agent
Appsignal.configure(:production) do |config|
config.endpoint = "http://localhost:8080"
end
Appsignal.start
end
def monitor_transaction(transaction_id)
transaction = Appsignal::Transaction.new(
Appsignal::Transaction::HTTP_REQUEST,
:id => transaction_id
)
Appsignal::Transaction.set_current_transaction(transaction)
transaction.set_action("HomeController#show")
transaction.add_params(:id => 1)
Appsignal.instrument("process_action.action_controller") do
Appsignal.instrument_sql(
"sql.active_record",
nil,
"SELECT `users`.* FROM `users` WHERE `users`.`id` = ?"
)
10.times do
Appsignal.instrument_sql(
"sql.active_record",
nil,
"SELECT `todos`.* FROM `todos` WHERE `todos`.`id` = ?"
)
end
Appsignal.instrument(
"render_template.action_view",
"app/views/home/show.html.erb"
) do
5.times do
Appsignal.instrument(
"render_partial.action_view",
"app/views/home/_piece.html.erb"
) do
3.times do
Appsignal.instrument("cache.read")
end
end
end
end
end
Appsignal::Transaction.complete_current!
end
def run_benchmark
no_transactions = (ENV["NO_TRANSACTIONS"] || 100_000).to_i
no_threads = (ENV["NO_THREADS"] || 1).to_i
total_objects = ObjectSpace.count_objects[:TOTAL]
puts "Initializing, currently #{total_objects} objects"
puts "RSS: #{process_rss}"
start_agent
puts "Appsignal #{Appsignal.active? ? "active" : "not active"}"
threads = []
puts "Running #{no_transactions} normal transactions in #{no_threads} threads"
puts(Benchmark.measure do
no_threads.times do
thread = Thread.new do
no_transactions.times do |i|
monitor_transaction("transaction_#{i}")
end
end
thread.abort_on_exception = true
threads << thread
end
threads.each(&:join)
puts "Finished"
end)
puts "Done, currently #{ObjectSpace.count_objects[:TOTAL] - total_objects} objects created"
puts "RSS: #{process_rss}"
end