Class: Omnizip::Profiler
- Inherits:
-
Object
- Object
- Omnizip::Profiler
- Defined in:
- lib/omnizip/profiler.rb,
lib/omnizip/profiler/memory_profiler.rb,
lib/omnizip/profiler/method_profiler.rb,
lib/omnizip/profiler/report_generator.rb
Overview
Main profiler interface using Strategy pattern for different profiling approaches
Defined Under Namespace
Classes: MemoryProfiler, MethodProfiler, ReportGenerator
Instance Attribute Summary collapse
-
#enabled ⇒ Object
readonly
Returns the value of attribute enabled.
-
#report ⇒ Object
readonly
Returns the value of attribute report.
Instance Method Summary collapse
-
#analyze_hot_paths(threshold_percentage: 10.0) ⇒ Object
Analyze collected results and identify hot paths.
-
#disable! ⇒ Object
Disable profiling.
-
#enable! ⇒ Object
Enable profiling.
-
#generate_suggestions ⇒ Object
Generate optimization suggestions based on profiling data.
-
#identify_bottlenecks ⇒ Object
Identify performance bottlenecks.
-
#initialize(profile_name: "default", enabled: true) ⇒ Profiler
constructor
A new instance of Profiler.
-
#profile(operation_name, profiler_type: :method, &block) ⇒ Object
Profile a block of code with the specified profiler strategy.
-
#profile_method(object, method_name, *args, profiler_type: :method, **kwargs) ⇒ Object
Profile a method call with automatic naming.
-
#register_profiler(name, profiler) ⇒ Object
Register a profiler strategy.
-
#reset! ⇒ Object
Reset profiler state.
Constructor Details
#initialize(profile_name: "default", enabled: true) ⇒ Profiler
Returns a new instance of Profiler.
11 12 13 14 15 16 |
# File 'lib/omnizip/profiler.rb', line 11 def initialize(profile_name: "default", enabled: true) @profile_name = profile_name @enabled = enabled @report = Models::ProfileReport.new(profile_name: profile_name) @profilers = {} end |
Instance Attribute Details
#enabled ⇒ Object (readonly)
Returns the value of attribute enabled.
9 10 11 |
# File 'lib/omnizip/profiler.rb', line 9 def enabled @enabled end |
#report ⇒ Object (readonly)
Returns the value of attribute report.
9 10 11 |
# File 'lib/omnizip/profiler.rb', line 9 def report @report end |
Instance Method Details
#analyze_hot_paths(threshold_percentage: 10.0) ⇒ Object
Analyze collected results and identify hot paths
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/omnizip/profiler.rb', line 48 def analyze_hot_paths(threshold_percentage: 10.0) total_time = report.total_execution_time return [] if total_time.zero? threshold_time = total_time * (threshold_percentage / 100.0) hot_operations = report.results.select do |result| result.total_time && result.total_time >= threshold_time end hot_operations.each do |op| @report.add_hot_path( operation: op.operation_name, time: op.total_time, percentage: (op.total_time / total_time) * 100, ) end hot_operations end |
#disable! ⇒ Object
Disable profiling
154 155 156 |
# File 'lib/omnizip/profiler.rb', line 154 def disable! @enabled = false end |
#enable! ⇒ Object
Enable profiling
149 150 151 |
# File 'lib/omnizip/profiler.rb', line 149 def enable! @enabled = true end |
#generate_suggestions ⇒ Object
Generate optimization suggestions based on profiling data
115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/omnizip/profiler.rb', line 115 def generate_suggestions # Analyze hot paths for optimization opportunities suggestions = analyze_hot_paths.map do |hot_op| Models::OptimizationSuggestion.new( title: "Optimize hot path: #{hot_op.operation_name}", description: "Operation consuming #{hot_op.total_time}s " \ "(#{((hot_op.total_time / report.total_execution_time) * 100).round(1)}% of total time)", severity: :high, category: :hotpath, impact_estimate: hot_op.total_time / report.total_execution_time, related_operations: [hot_op.operation_name], metrics: { time: hot_op.total_time }, ) end # Analyze memory usage report.memory_intensive_operations(limit: 3).each do |mem_op| next unless mem_op.memory_allocated > 1_000_000 # > 1MB suggestions << Models::OptimizationSuggestion.new( title: "Reduce memory allocation: #{mem_op.operation_name}", description: "Operation allocating #{format_bytes(mem_op.memory_allocated)}", severity: calculate_memory_severity(mem_op), category: :memory, impact_estimate: mem_op.memory_allocated / report.total_memory_allocated.to_f, related_operations: [mem_op.operation_name], metrics: { memory_allocated: mem_op.memory_allocated }, ) end suggestions.sort_by(&:priority_score).reverse end |
#identify_bottlenecks ⇒ Object
Identify performance bottlenecks
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 |
# File 'lib/omnizip/profiler.rb', line 70 def identify_bottlenecks bottlenecks = [] # Memory bottlenecks memory_intensive = report.memory_intensive_operations(limit: 3) memory_intensive.each do |op| bottlenecks << { type: :memory, operation: op.operation_name, allocated: op.memory_allocated, severity: calculate_memory_severity(op), } @report.add_bottleneck(bottlenecks.last) end # CPU bottlenecks cpu_intensive = report.slowest_operations(limit: 3) cpu_intensive.each do |op| bottlenecks << { type: :cpu, operation: op.operation_name, time: op.total_time, severity: calculate_cpu_severity(op), } @report.add_bottleneck(bottlenecks.last) end # GC pressure bottlenecks high_gc = report.results.select do |r| r.gc_pressure && r.gc_pressure > 1.0 end high_gc.each do |op| bottlenecks << { type: :gc, operation: op.operation_name, gc_pressure: op.gc_pressure, severity: :high, } @report.add_bottleneck(bottlenecks.last) end bottlenecks end |
#profile(operation_name, profiler_type: :method, &block) ⇒ Object
Profile a block of code with the specified profiler strategy
24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/omnizip/profiler.rb', line 24 def profile(operation_name, profiler_type: :method, &block) return yield unless enabled profiler = @profilers[profiler_type] unless profiler raise ArgumentError, "Unknown profiler type: #{profiler_type}" end result = profiler.profile(operation_name, &block) @report.add_result(result) result end |
#profile_method(object, method_name, *args, profiler_type: :method, **kwargs) ⇒ Object
Profile a method call with automatic naming
39 40 41 42 43 44 45 |
# File 'lib/omnizip/profiler.rb', line 39 def profile_method(object, method_name, *args, profiler_type: :method, **kwargs) operation_name = "#{object.class}##{method_name}" profile(operation_name, profiler_type: profiler_type) do object.public_send(method_name, *args, **kwargs) end end |
#register_profiler(name, profiler) ⇒ Object
Register a profiler strategy
19 20 21 |
# File 'lib/omnizip/profiler.rb', line 19 def register_profiler(name, profiler) @profilers[name] = profiler end |
#reset! ⇒ Object
Reset profiler state
159 160 161 |
# File 'lib/omnizip/profiler.rb', line 159 def reset! @report = Models::ProfileReport.new(profile_name: @profile_name) end |