Sha256: f9473363e0b10b2eb003efbc7a2ba29f2e6c8777d4e2c6b628c88eea0c4da947

Contents?: true

Size: 1.95 KB

Versions: 20

Compression:

Stored size: 1.95 KB

Contents

# benchmark_tracepoint.rb

def memory_usage
  pid = Process.pid
  `ps -o rss= -p #{pid}`.to_i  # Returns memory usage in KB
end

# Generate a 100 MB string outside the iterations
large_string = 'a' * 10_000_000  # 10 million characters

def test_with_tracepoint(iterations, large_string)
  puts "\nTest with TracePoint capturing bindings:"
  captured_bindings = []

  trace = TracePoint.new(:raise) do |tp|
    captured_bindings << tp.binding
  end

  trace.enable

  puts "Memory usage before exceptions: #{memory_usage} KB"

  iterations.times do
    begin
      # Use the large string within the local scope
      local_large_string = large_string
      raise 'Test exception'
    rescue => e
      raise e rescue nil  # Suppress the exception to continue the loop
    end
  end

  puts "Memory usage after exceptions: #{memory_usage} KB"

  trace.disable
end

def test_without_tracepoint(iterations, large_string)
  puts "\nTest without TracePoint capturing bindings:"

  puts "Memory usage before exceptions: #{memory_usage} KB"

  iterations.times do
    begin
      # Use the large string within the local scope
      local_large_string = large_string
      raise 'Test exception'
    rescue => e
      raise e rescue nil
    end
  end

  puts "Memory usage after exceptions: #{memory_usage} KB"
end

def test_exception_with_large_variable(iterations, large_string)
  puts "\nTest with exceptions storing large variable:"

  puts "Memory usage before exceptions: #{memory_usage} KB"

  iterations.times do
    begin
      raise 'Test exception'
    rescue => e
      # Store a reference to the large string in the exception
      e.instance_variable_set(:@large_string, large_string)
      raise e rescue nil
    end
  end

  puts "Memory usage after exceptions: #{memory_usage} KB"
end

iterations = 10  # Adjust iterations as needed

test_with_tracepoint(iterations, large_string)
test_without_tracepoint(iterations, large_string)
test_exception_with_large_variable(iterations, large_string)

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
enhanced_errors-3.0.3 benchmark/memory_bench.rb
enhanced_errors-3.0.2 benchmark/memory_bench.rb
enhanced_errors-3.0.1 benchmark/memory_bench.rb
enhanced_errors-3.0.0 benchmark/memory_bench.rb
enhanced_errors-2.2.0 benchmark/memory_bench.rb
enhanced_errors-2.1.3 benchmark/memory_bench.rb
enhanced_errors-2.1.2 benchmark/memory_bench.rb
enhanced_errors-2.1.1 benchmark/memory_bench.rb
enhanced_errors-2.1.0 benchmark/memory_bench.rb
enhanced_errors-2.0.6 benchmark/memory_bench.rb
enhanced_errors-2.0.5 benchmark/memory_bench.rb
enhanced_errors-2.0.4 benchmark/memory_bench.rb
enhanced_errors-2.0.3 benchmark/memory_bench.rb
enhanced_errors-2.0.2 benchmark/memory_bench.rb
enhanced_errors-2.0.1 benchmark/memory_bench.rb
enhanced_errors-2.0.0 benchmark/memory_bench.rb
enhanced_errors-1.0.0 benchmark/memory_bench.rb
enhanced_errors-0.1.8 benchmark/memory_bench.rb
enhanced_errors-0.1.7 benchmark/memory_bench.rb
enhanced_errors-0.1.6 benchmark/memory_bench.rb