A platform for community discussion. Free, open, simple.
FIX: stop double counting net calls in logs
Samf295a18e9488
diff --git a/lib/method_profiler.rb b/lib/method_profiler.rbindex 9ad59b007ff..f624ea8f977 100644--- a/lib/method_profiler.rb+++ b/lib/method_profiler.rb@@ -1,7 +1,16 @@# see https://samsaffron.com/archive/2017/10/18/fastest-way-to-profile-a-method-in-rubyclass MethodProfiler- def self.patch(klass, methods, name)+ def self.patch(klass, methods, name, no_recurse: false)patches = methods.map do |method_name|++ recurse_protection = ""+ if no_recurse+ recurse_protection = <<~RUBY+ return #{method_name}__mp_unpatched(*args, &blk) if @mp_recurse_protect_#{method_name}+ @mp_recurse_protect_#{method_name} = true+ RUBY+ end+<<~RUBYunless defined?(#{method_name}__mp_unpatched)alias_method :#{method_name}__mp_unpatched, :#{method_name}@@ -9,6 +18,7 @@ class MethodProfilerunless prof = Thread.current[:_method_profiler]return #{method_name}__mp_unpatched(*args, &blk)end+ #{recurse_protection}beginstart = Process.clock_gettime(Process::CLOCK_MONOTONIC)#{method_name}__mp_unpatched(*args, &blk)@@ -16,6 +26,7 @@ class MethodProfilerdata = (prof[:#{name}] ||= {duration: 0.0, calls: 0})data[:duration] += Process.clock_gettime(Process::CLOCK_MONOTONIC) - startdata[:calls] += 1+ #{"@mp_recurse_protect_#{method_name} = false" if no_recurse}endendenddiff --git a/lib/middleware/request_tracker.rb b/lib/middleware/request_tracker.rbindex d53b029ee82..543a5c60dc2 100644--- a/lib/middleware/request_tracker.rb+++ b/lib/middleware/request_tracker.rb@@ -27,7 +27,7 @@ class Middleware::RequestTrackerMethodProfiler.patch(Net::HTTP, [:request- ], :net)+ ], :net, no_recurse: true)MethodProfiler.patch(Excon::Connection, [:requestdiff --git a/spec/components/method_profiler_spec.rb b/spec/components/method_profiler_spec.rbindex cbbfcff65a8..480280a5fae 100644--- a/spec/components/method_profiler_spec.rb+++ b/spec/components/method_profiler_spec.rb@@ -5,6 +5,22 @@ describe MethodProfiler doclass Sneetchdef beachend++ 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)endit "can transfer data between threads" do