-
Notifications
You must be signed in to change notification settings - Fork 135
Expand file tree
/
Copy pathrunner.rb
More file actions
78 lines (63 loc) · 1.79 KB
/
Copy pathrunner.rb
File metadata and controls
78 lines (63 loc) · 1.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# frozen_string_literal: true
require_relative 'task-2'
require 'benchmark'
require 'memory_profiler'
require 'stackprof'
require 'ruby-prof'
size, mode, profiler = ARGV
FILENAME = "data/data#{size}.txt"
def memory_usage
(`ps -o rss= -p #{Process.pid}`.to_i / 1024)
end
def run_stackprof
StackProf.run(mode: :object, out: 'stackprof_reports/stackprof.dump', raw: true) do
work(FILENAME)
end
end
def run_memory_profiler
report = MemoryProfiler.report do
work(FILENAME)
end
report.pretty_print(scale_bytes: true)
end
def run_ruby_prof(measure_mode)
result = RubyProf::Profile.profile(track_allocations: true, measure_mode: measure_mode) do
work(FILENAME)
end
printer = RubyProf::GraphHtmlPrinter.new(result)
printer.print(File.open("ruby_prof_reports/graph_#{measure_mode}.html", 'w+'), :min_percent=>0)
printer = RubyProf::CallStackPrinter.new(result)
printer.print(File.open("ruby_prof_reports/callstack_#{measure_mode}.html", 'w+'))
end
def run_profiler(profiler)
case profiler
when 'stackprof'
run_stackprof
when 'memory_profiler'
run_memory_profiler
when 'ruby-prof-memory'
run_ruby_prof(:memory)
when 'ruby-prof-allocations'
run_ruby_prof(:allocations)
else
puts "Unknown profiler type: #{profiler}"
end
end
def run_memory_monitor
io = File.open('memory_usage.txt', 'w')
io << format("INITIAL MEMORY USAGE: %d MB\n", memory_usage)
monitor_thread = Thread.new do
while true
io << format("MEMORY USAGE: %d MB\n", memory_usage)
sleep(1)
end
ensure
io << format("FINAL MEMORY USAGE: %d MB\n", memory_usage)
io.close
end
work(FILENAME)
monitor_thread.kill
end
run_memory_monitor if mode == 'memory'
puts Benchmark.measure { work(FILENAME) } if mode == 'time'
run_profiler(profiler) if mode == 'profile'