2019-04-30 08:27:42 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2022-07-28 10:27:38 +08:00
|
|
|
RSpec.describe MethodProfiler do
|
2017-11-28 13:47:20 +08:00
|
|
|
class Sneetch
|
|
|
|
def beach
|
|
|
|
end
|
2018-02-28 07:45:11 +08:00
|
|
|
|
|
|
|
def recurse(count = 5)
|
|
|
|
if count > 0
|
|
|
|
recurse(count - 1)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it "can bypass recursion on demand" do
|
|
|
|
MethodProfiler.patch(Sneetch, [:recurse], :recurse, no_recurse: true)
|
|
|
|
|
|
|
|
MethodProfiler.start
|
|
|
|
Sneetch.new.recurse
|
|
|
|
result = MethodProfiler.stop
|
|
|
|
|
|
|
|
expect(result[:recurse][:calls]).to eq(1)
|
2017-11-28 13:47:20 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
it "can transfer data between threads" do
|
|
|
|
MethodProfiler.patch(Sneetch, [:beach], :at_beach)
|
|
|
|
|
|
|
|
MethodProfiler.start
|
|
|
|
Sneetch.new.beach
|
|
|
|
data = MethodProfiler.transfer
|
|
|
|
result = nil
|
|
|
|
Thread.new do
|
|
|
|
MethodProfiler.start(data)
|
|
|
|
Sneetch.new.beach
|
|
|
|
result = MethodProfiler.stop
|
|
|
|
end.join
|
|
|
|
|
|
|
|
expect(result[:at_beach][:calls]).to eq(2)
|
|
|
|
end
|
|
|
|
end
|