-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathstress_mem.rb
38 lines (30 loc) · 902 Bytes
/
stress_mem.rb
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
@retained = []
SEED=1
MAX_STRING_SIZE = 100
RAND = Random.new(SEED)
LARGE_ALLOC_MAX_SIZE = 100_000
def stress(allocate_count, retain_count, chunk_size)
chunk = []
while retain_count > 0 || allocate_count > 0
if retain_count == 0 || (RAND.rand < 0.5 && allocate_count > 0)
chunk << " " * RAND.rand(MAX_STRING_SIZE)
allocate_count -= 1
if chunk.length > chunk_size
chunk = [" " * RAND.rand(LARGE_ALLOC_MAX_SIZE)]
end
else
@retained << " " * Random.rand(MAX_STRING_SIZE)
retain_count -= 1
end
end
end
start = Time.now
threads = [1, ENV["STRESS_THREADS"].to_i].max
(0...threads).map do
Thread.new do
stress(12_000_000/threads, 600_000/threads, 200_000/threads)
end
end.each(&:join)
duration = (Time.now - start).to_f
_, size = `ps ax -o pid,rss | grep -E "^[[:space:]]*#{$$}"`.strip.split.map(&:to_i)
puts "#{size},#{duration}"