Project

General

Profile

Bug #15397 » test_gc.rb

lihr (辉荣 李), 12/11/2018 03:27 AM

 
1
require 'json'
2

    
3
$file = File.expand_path("../wiki_pages.json", __FILE__)
4
def main
5
  puts "read content from file #{File.size($file)}[#{File.size($file).to_hmsize}]"
6
  json = IO.read($file)
7
  sleep 30
8

    
9
  puts "parse json string to object"
10
  obj = JSON.parse(json)
11
  sleep 30
12

    
13
  puts "set string&object to nil, and sleep 30 min to check the memory usage."
14
  json = nil
15
  obj = nil
16
end
17

    
18
####  Helpers >>>
19
class Numeric
20
  def to_hmsize
21
    if self > 1024 ** 3
22
      "#{(self * 1.0 / (1024 ** 3)).round(2)} GB"
23
    elsif self > 1024 ** 2
24
      "#{(self * 1.0 / (1024 ** 2)).round(2)} MB"
25
    end
26
  end
27
  def to_hmtime
28
    "#{self / 60}m#{self % 60}s"
29
  end
30
end
31

    
32
def get_total_mem
33
  if RUBY_PLATFORM.index("linux")
34
    `awk '/MemTotal/ {print $2}' /proc/meminfo`.strip.to_i * 1024
35
  elsif RUBY_PLATFORM.index("darwin")
36
    `sysctl -a | grep mem | grep hw.memsize | awk '{print $2}'`.strip.to_i
37
  end
38
end
39
$total_mem = get_total_mem
40
def free_mem
41
  if RUBY_PLATFORM.index("linux")
42
    (`free -b | grep Mem | awk '{print $4}'`.strip.to_i)
43
  elsif RUBY_PLATFORM.index("darwin")
44
    $total_mem - `ps -caxm -orss= | awk '{ sum += $1 } END { print sum }'`.strip.to_i * 1024
45
  end
46
end
47

    
48
if !File.exists?($file)
49
  open($file, 'w') do |f|
50
    array = []
51
    radix = RUBY_PLATFORM.index("darwin") ? 10000 : 1500
52
    ($total_mem / radix).times.each do |i|
53
      array << {pageid: i, title: "title of #{i}", pagename: "pagename_of_#{i}"}
54
    end
55
    f.puts array.to_json
56
  end
57
  puts "created a json file: #{File.size($file)}[#{File.size($file).to_hmsize}]"
58
  puts "init the json file at the first launch, please run again."
59
  exit
60
end
61

    
62
puts RUBY_DESCRIPTION
63
puts Time.now
64

    
65
require 'objspace'
66

    
67
Thread.new do
68
  started = Time.now.to_i
69
  total_mem = $total_mem.to_hmsize
70
  puts "Memory: \e[1;32mProcess RSS\e[0m/\e[1;34mSystem Total Memory\e[0m/\e[1;31mSystem Free Memory\e[0m"
71
  while true
72
    sys_mem = (`ps -o rss= -p #{Process.pid}`.strip.to_i * 1024).to_hmsize
73
    objspace_mem = ObjectSpace.memsize_of_all.to_hmsize
74
    puts "#{(Time.now.to_i - started).to_hmtime} #{$$}\tMemory:\e[1;32m#{sys_mem}\e[0m/\e[1;34m#{total_mem}\e[0m/\e[1;31m#{free_mem.to_hmsize}\e[0m, objspace_mem:#{objspace_mem}"
75
    sleep 3
76
    GC.start
77
  end
78
end
79
####  Helpers <<<<
80

    
81
sleep 10
82
main
83
sleep 1800