From 04d06d238d52c5312837fb9e164607270d7de010 Mon Sep 17 00:00:00 2001 From: Mike Blumtritt Date: Mon, 13 May 2024 19:39:38 +0200 Subject: [PATCH 1/2] First draft of time method --- lib/im-lost.rb | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/lib/im-lost.rb b/lib/im-lost.rb index 0db728e..266c092 100644 --- a/lib/im-lost.rb +++ b/lib/im-lost.rb @@ -153,6 +153,29 @@ def here(test = true) test end + # + # Print runtime of a given block. + # + # @param title [#to_s] optional title for output + # @yield + # @yieldreturn [Object] return result + # + def time(title = nil) + raise(ArgumentError, 'no block given') unless block_given? + @output.puts( + if title + "T #{title}:" + else + loc = Kernel.caller_locations(1, 1)[0] + "T #{loc.path}:#{loc.lineno}" + end + ) + tm = now + ret = yield + @output.puts(" #{now - tm} sec.") + ret + end + # # Trace objects. # @@ -257,7 +280,7 @@ def vars(object) @trace[traced] = traced if traced end - protected + private def as_sig(prefix, info, args) args = args.join(', ') @@ -269,7 +292,11 @@ def as_sig(prefix, info, args) end end - private + if defined?(Process::CLOCK_MONOTONIC) + def now = Process.clock_gettime(Process::CLOCK_MONOTONIC) + else + def now = ::Time.now + end def _trace(arg) id = arg.__id__ From 2f23e082b8f9a328e57e1d9b2cae2d0c1de3ec23 Mon Sep 17 00:00:00 2001 From: Mike Blumtritt Date: Tue, 28 May 2024 09:03:53 +0200 Subject: [PATCH 2/2] Tidy-up --- README.md | 2 ++ lib/im-lost.rb | 22 ++++++++++++---------- spec/lib/im-lost_spec.rb | 8 ++++---- 3 files changed, 18 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index b03b051..c071a3f 100644 --- a/README.md +++ b/README.md @@ -19,6 +19,7 @@ File.open('test.txt', 'w') do |file| file.puts(:world!) end end + # output will look like # > IO#<<(?) # /projects/test.rb:1 @@ -38,6 +39,7 @@ ImLost.trace_exceptions do rescue SystemCallError raise('something went wrong!') end + # output will look like # x Errno::EEXIST: File exists @ rb_sysopen - / # /projects/test.rb:2 diff --git a/lib/im-lost.rb b/lib/im-lost.rb index 0db728e..3ec02f4 100644 --- a/lib/im-lost.rb +++ b/lib/im-lost.rb @@ -79,6 +79,7 @@ def trace_calls=(value) # rescue SystemCallError # raise('something went wrong!') # end + # # # output will look like # # x Errno::EEXIST: File exists @ rb_sysopen - / # # /projects/test.rb:2 @@ -170,15 +171,16 @@ def here(test = true) # file.puts(:world!) # end # end - # output will look like - # > IO#<<(?) - # /projects/test.rb:1 - # > IO#write(*) - # /projects/test.rb:1 - # > IO#puts(*) - # /projects/test.rb:2 - # > IO#write(*) - # /projects/test.rb:2 + # + # # output will look like + # # > IO#<<(?) + # # /projects/test.rb:1 + # # > IO#write(*) + # # /projects/test.rb:1 + # # > IO#puts(*) + # # /projects/test.rb:2 + # # > IO#write(*) + # # /projects/test.rb:2 # # @overload trace(*args) # @param args [[Object]] one or more objects to be traced @@ -392,7 +394,7 @@ def _local_vars(binding) end ] - supported = RUBY_VERSION >= '3.3.0' ? %i[raise rescue] : %i[raise] + supported = RUBY_VERSION.to_f >= 3.3 ? %i[raise rescue] : %i[raise] @trace_exceptions = TracePoint.new(*supported) do |tp| ex = tp.raised_exception.inspect diff --git a/spec/lib/im-lost_spec.rb b/spec/lib/im-lost_spec.rb index 26ae900..938a90c 100644 --- a/spec/lib/im-lost_spec.rb +++ b/spec/lib/im-lost_spec.rb @@ -99,7 +99,7 @@ def bar = :bar expect(output).to eq "> TestSample#insp(**{})\n" end - if RUBY_VERSION < '3.1.0' + if RUBY_VERSION.to_f < 3.1 it 'handles argument forwarding' do sample.fwd(40, 2) @@ -206,7 +206,7 @@ def bar = :bar expect(output).to eq "< TestSample#insp(**{})\n = \"{}\"\n" end - if RUBY_VERSION < '3.1.0' + if RUBY_VERSION.to_f < 3.1 it 'handles argument forwarding' do sample.fwd(40, 2) @@ -240,7 +240,7 @@ def bar = :bar end end - if RUBY_VERSION >= '3.3.0' + if RUBY_VERSION.to_f > 3.3 context '.trace_exceptions' do it 'traces exceptions and rescue blocks' do ImLost.trace_exceptions do @@ -299,7 +299,7 @@ def bar = :bar end end - if RUBY_VERSION < '3.3.0' + if RUBY_VERSION.to_f < 3.3 context '.trace_exceptions' do it 'traces exceptions and rescue blocks' do ImLost.trace_exceptions do