perfetto 0.1.4 → 0.1.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: e191222d4cb7de42dac47a09ed1e2a8db6ec203418e9420b87cf89008d2e24b2
4
- data.tar.gz: e703c2fba901b08e837267ff50344d8b0f3dcfee760c752517126993e1188379
3
+ metadata.gz: 9989cb452ab1dd59f10629d8b29dd9ea035954d24fd013120bbeef3787adfcc9
4
+ data.tar.gz: 017cfdfd0a27a8e4c93d0d3ec74575b2455a4139491c6ef131608cc389487dd8
5
5
  SHA512:
6
- metadata.gz: a5a9dc11b2e8ddb7c37a8024629984281f7ca38efb834bb4b939f84ecfe4f9a4197147dd782b88cd849ec4b0267aaf299b2f6ee0ac954773ca79d43b6bf90063
7
- data.tar.gz: 844ee5efe6639edf96b4afcb89ec89973eb04c2818bacde53fe9f9139bfe6cfa7a89b2c4177a1f5959b34c11aa7cd5dfef984f3916d13947da11ee8eeaaba1fa
6
+ metadata.gz: 6af238ffdf8008e4edb49b35bd8e10de5005c7d2cd307158651ef88ba711b28e16a8d4f04d5481a87f9871ee16f5ae573151bda33f0f02475cf62b49e0b2f3be
7
+ data.tar.gz: a27cf64202e4731cf07a9b3752d6b3afce376585c91b9f2609aa3971ecd06c5d15cbe06ef3cee0935692cb37d4c8cfb9003efaf2afe1e5ce35b970480b5a35d0
data/README.md CHANGED
@@ -7,7 +7,6 @@ https://ui.perfetto.dev/
7
7
  # Usage
8
8
 
9
9
  ```ruby
10
-
11
10
  require "perfetto"
12
11
 
13
12
  Perfetto.setup enable_tracing: true
@@ -44,6 +43,10 @@ Perfetto.trace_event_instant_with_debug_info "cpu_work", "example_instant2", "ke
44
43
  sleep 0.1
45
44
 
46
45
  class Foo
46
+ # Intercept instance methods
47
+ include Perfetto::Interceptor
48
+ perfetto_trace_all
49
+
47
50
  def bar(a, b = 1, c: 2)
48
51
  yield(a + b + c)
49
52
  end
@@ -53,15 +56,26 @@ class Foo
53
56
  sleep 0.1
54
57
  end
55
58
 
56
- # Intercept instance methods
57
- include Perfetto::Interceptor
58
- pftrace_all
59
+ def self.buf
60
+ puts "buf"
61
+ sleep 0.1
62
+ end
63
+ end
64
+
65
+ class Bar < Foo
66
+ def say
67
+ puts "hello"
68
+ sleep 0.1
69
+ end
59
70
  end
60
71
 
61
72
  f = Foo.new
73
+ b = Bar.new
62
74
  f.bar(1, 2, c: 3) do |n|
63
75
  n.times do |x|
64
76
  f.baz x + n
77
+ Foo.buf
78
+ b.say
65
79
  end
66
80
  end
67
81
 
data/example/example.png CHANGED
Binary file
data/example/example.rb CHANGED
@@ -3,7 +3,7 @@
3
3
  # rubocop:disable Naming/MethodParameterName
4
4
  # rubocop:disable Style/Documentation
5
5
 
6
- require "perfetto"
6
+ require_relative "../lib/perfetto"
7
7
 
8
8
  Perfetto.setup enable_tracing: true
9
9
 
@@ -39,6 +39,10 @@ Perfetto.trace_event_instant_with_debug_info "cpu_work", "example_instant2", "ke
39
39
  sleep 0.1
40
40
 
41
41
  class Foo
42
+ # Intercept instance methods
43
+ include Perfetto::Interceptor
44
+ perfetto_trace_all
45
+
42
46
  def bar(a, b = 1, c: 2)
43
47
  yield(a + b + c)
44
48
  end
@@ -48,15 +52,26 @@ class Foo
48
52
  sleep 0.1
49
53
  end
50
54
 
51
- # Intercept instance methods
52
- include Perfetto::Interceptor
53
- pftrace_all
55
+ def self.buf
56
+ puts "buf"
57
+ sleep 0.1
58
+ end
59
+ end
60
+
61
+ class Bar < Foo
62
+ def say
63
+ puts "hello"
64
+ sleep 0.1
65
+ end
54
66
  end
55
67
 
56
68
  f = Foo.new
69
+ b = Bar.new
57
70
  f.bar(1, 2, c: 3) do |n|
58
71
  n.times do |x|
59
72
  f.baz x + n
73
+ Foo.buf
74
+ b.say
60
75
  end
61
76
  end
62
77
 
@@ -4,31 +4,100 @@ module Perfetto
4
4
  # To intercept method calls in other classes
5
5
  module Interceptor
6
6
  def self.included(base)
7
- base.extend(ClassMethods)
7
+ if Perfetto::Configure.enable_tracing
8
+ base.extend ImplMethods
9
+ else
10
+ base.extend StubMethods
11
+ end
8
12
  end
9
13
 
10
- # Class methods for the interceptor
11
- module ClassMethods
12
- if Perfetto::Configure.enable_tracing
13
- def pftrace(method_name)
14
- original_method = instance_method(method_name)
15
- alias_method "pftrace_#{method_name}", method_name
16
- define_method(method_name) do |*args, **kwargs, &block|
17
- Perfetto.trace_event_begin self.class.name, method_name.to_s
18
- original_method.bind(self).call(*args, **kwargs, &block)
19
- ensure
20
- Perfetto.trace_event_end self.class.name
21
- end
14
+ # Stub methods
15
+ module StubMethods
16
+ def perfetto_trace_instance_method(_method_name); end
17
+ def perfetto_trace_class_method(_method_name); end
18
+ def perfetto_trace_all; end
19
+
20
+ def perfetto_traced_instance_method?(_method_name)
21
+ false
22
+ end
23
+
24
+ def perfetto_traced_class_method?(_method_name)
25
+ false
26
+ end
27
+
28
+ def perfetto_traced_instance_methods
29
+ []
30
+ end
31
+
32
+ def perfetto_traced_class_methods
33
+ []
34
+ end
35
+ end
36
+
37
+ # Real Implementation
38
+ module ImplMethods
39
+ def perfetto_trace_instance_method(method_name)
40
+ return if method_name.to_s.start_with? "_pfi_"
41
+
42
+ # warn "Perfetto[InstanceMethod]: #{name}##{method_name}"
43
+ perfetto_traced_instance_methods << method_name
44
+
45
+ original_method = instance_method(method_name)
46
+ alias_method "_pfi_#{method_name}", method_name
47
+
48
+ define_method(method_name) do |*args, **kwargs, &block|
49
+ Perfetto.trace_event_begin self.class.name, method_name.to_s
50
+ original_method.bind(self).call(*args, **kwargs, &block)
51
+ ensure
52
+ Perfetto.trace_event_end self.class.name
22
53
  end
54
+ end
55
+
56
+ def perfetto_trace_class_method(method_name)
57
+ return if method_name.to_s == "singleton_method_added" || method_name.to_s.start_with?("_pfc_")
23
58
 
24
- def pftrace_all
25
- instance_methods(false).each do |method_name|
26
- pftrace method_name
27
- end
59
+ # warn "Perfetto[ClassMethod]: #{name}.#{method_name}"
60
+ perfetto_traced_class_methods << method_name
61
+
62
+ original_method = method(method_name)
63
+ singleton_class.send(:alias_method, "_pfc_#{method_name}", method_name)
64
+
65
+ define_singleton_method(method_name) do |*args, **kwargs, &block|
66
+ Perfetto.trace_event_begin name, method_name.to_s
67
+ original_method.call(*args, **kwargs, &block)
68
+ ensure
69
+ Perfetto.trace_event_end name
70
+ end
71
+ end
72
+
73
+ def perfetto_trace_all
74
+ define_singleton_method(:method_added) do |method_name|
75
+ return if perfetto_traced_instance_method?(method_name)
76
+
77
+ perfetto_trace_instance_method method_name
78
+ end
79
+
80
+ define_singleton_method(:singleton_method_added) do |method_name|
81
+ return if perfetto_traced_class_method?(method_name)
82
+
83
+ perfetto_trace_class_method method_name
28
84
  end
29
- else # When tracing is disabled
30
- def pftrace(_method_name); end
31
- def pftrace_all; end
85
+ end
86
+
87
+ def perfetto_traced_instance_method?(method_name)
88
+ perfetto_traced_instance_methods.include? method_name
89
+ end
90
+
91
+ def perfetto_traced_class_method?(method_name)
92
+ perfetto_traced_class_methods.include? method_name
93
+ end
94
+
95
+ def perfetto_traced_instance_methods
96
+ @perfetto_traced_instance_methods ||= []
97
+ end
98
+
99
+ def perfetto_traced_class_methods
100
+ @perfetto_traced_class_methods ||= []
32
101
  end
33
102
  end
34
103
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Perfetto
4
- VERSION = "0.1.4"
4
+ VERSION = "0.1.5"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: perfetto
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.1.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Kowalski Dark