-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofiler.rb
49 lines (40 loc) · 1.04 KB
/
profiler.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
#!/usr/bin/env ruby
module Profiler
def self.prepended(base)
(base.instance_methods(false) + ['initialize']).each do |name|
Profiler.send :define_method, name do |*params|
start = Time.now
result = super *params
duration = Time.now - start
puts "[Profiler] (#{name}) execution time: #{duration.to_i} seconds"
result
end
end
end
end
class TestClass
def initialize(str)
puts "initialize: #{str}"
sleep 1
foo1 'text for foo1'
end
def foo1(str)
sleep 1
puts "foo1: #{str}"
end
def foo2(str, count)
sleep 1
puts "foo2: #{str}, #{count}"
end
# prepend Profiler # Variant #1
end
# test = TestClass.new 'hello'
test = TestClass.prepend(Profiler).new 'hello' # Variant #2
# test.class.prepend Profiler # Variant #3
test.foo2 'text for foo2', 3
# initialize: hello
# foo1: text for foo1
# [Profiler] (foo1) execution time: 1 seconds
# [Profiler] (initialize) execution time: 2 seconds
# foo2: text for foo2, 3
# [Profiler] (foo2) execution time: 1 seconds