Skip to content

Commit ddda79a

Browse files
NikitaNikita
authored andcommitted
[Zero Iteration] add control measuring thread in measurer
1 parent 6b6ca76 commit ddda79a

File tree

2 files changed

+24
-4
lines changed

2 files changed

+24
-4
lines changed

main.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@ def work
112112
end
113113

114114
File.write('result.json', "#{report.to_json}\n")
115-
116-
puts "MEMORY USAGE: #{(`ps -o rss= -p #{Process.pid}`.to_i / 1024)} MB" if is_print_memory_usage
117115
end
118116

119117
def parse_user(user)

measurer.rb

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,30 @@
11
require_relative 'main'
22

33
class Measurer
4-
def call
5-
Main.new(options: { count_lines: 15_000, is_print_memory_usage: true }).call
4+
MEMORY_USAGE_LIMIT_MB = 70
5+
CHECK_USAGE_INTERVAL_SEC = 1
6+
7+
def initialize(count_lines)
8+
@count_lines = count_lines
9+
end
10+
11+
def call = [memory_measurer_thread, work_thread].map(&:join)
12+
13+
private
14+
15+
attr_reader :count_lines
16+
17+
def work_thread = Thread.new do
18+
Main.new(options: { count_lines: }).call
19+
end
20+
21+
def memory_measurer_thread = Thread.new do
22+
sleep CHECK_USAGE_INTERVAL_SEC
23+
current_memory_usage_mb = `ps -o rss= -p #{Process.pid}`.to_i / 1024
24+
if current_memory_usage_mb > MEMORY_USAGE_LIMIT_MB
25+
puts "MEMORY USAGE: #{current_memory_usage_mb} MB, BUT LIMIT: #{MEMORY_USAGE_LIMIT_MB}"
26+
exit
27+
end
628
end
729
end
830

0 commit comments

Comments
 (0)