nginx stubをonにしておく。
Active connections: 17
server accepts handled requests
1177899 1177899 7047859
Reading: 0 Writing: 1 Waiting: 16
差分を取得するrubyメソッド
nginxのstubだと、起動してからの累計しか取れないため、
定期的に差分を取得=>差分秒で割る
req_per_sec.rb
# get request count per sec from nginx stub
def req_per_sec stub_url, conf_path
now = Time.now
last_json = FileTest.exists?(conf_path) && JSON.parse(File.read(conf_path))
txt = `curl #{stub_url}`
req_counts = txt.split("\n")[2].split(" ")[0].to_i
File.write conf_path, JSON::generate({time:now, req_counts:req_counts})
if last_json
diff_sec = now - Time.parse(last_json['time'])
req_per_sec = (req_counts - last_json['req_counts']) / diff_sec
return req_per_sec
else
return nil
end
end
call the method
.rb
p req_per_sec('http://hoge/server-stub','/path/to/hoge.json')