Last active
December 13, 2015 16:59
-
-
Save pierreozoux/4944109 to your computer and use it in GitHub Desktop.
2 little quick and dirty script.
A shell script to push ressources usage from a process running to a csv file. And a ruby script to draw a graph from a csv file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
require 'erb' | |
require 'csv' | |
csv_path = ARGV[0] | |
@csv_array = CSV.read(csv_path) | |
@csv_array.each_with_index do |row,row_index| | |
row.each_with_index do |cell,col_index| | |
if row_index != 0 then | |
@csv_array[row_index][col_index] = cell.to_f | |
end | |
end | |
end | |
total_time = @csv_array[-1][0] | |
minutes = (total_time/60).truncate | |
secondes = total_time - minutes * 60 | |
total_time_string = "#{minutes}:#{secondes.to_i}" | |
if File.basename(csv_path).include? "mem" | |
type = "Memory (%2Gb)" | |
max = 45 | |
else | |
type = "Cpu (%)" | |
max = 220 | |
end | |
if File.basename(csv_path).include? "32" | |
archi = "32" | |
else | |
archi = "64" | |
end | |
template = | |
%{ | |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> | |
<html xmlns="http://www.w3.org/1999/xhtml"> | |
<head> | |
<meta http-equiv="content-type" content="text/html; charset=utf-8"/> | |
<title> | |
Google Visualization API Sample | |
</title> | |
<script type="text/javascript" src="http://www.google.com/jsapi"></script> | |
<script type="text/javascript"> | |
google.load('visualization', '1', {packages: ['corechart']}); | |
</script> | |
<script type="text/javascript"> | |
function drawVisualization() { | |
// Some raw data (not necessarily accurate) | |
var data = google.visualization.arrayToDataTable([ | |
<% for @csv_line in @csv_array %> | |
<%= @csv_line %>, | |
<% end %> | |
<%= @csv_array[-1] %> | |
]); | |
// Create and draw the visualization. | |
var ac = new google.visualization.AreaChart(document.getElementById('visualization')); | |
ac.draw(data, { | |
title : '<%= type %> consumption - archi : <%= archi %> - total time : <%= total_time_string %>min', | |
isStacked: true, | |
width: 900, | |
height: 400, | |
vAxis: {title: "<%= type %>",maxValue : <%= max %>}, | |
hAxis: {title: "Time(sec)"} | |
}); | |
} | |
google.setOnLoadCallback(drawVisualization); | |
</script> | |
</head> | |
<body style="font-family: Arial;border: 0 none;"> | |
<div id="visualization" style="width: 900px; height: 400px;"></div> | |
</body> | |
</html> | |
} | |
result = ERB.new(template).result(binding) | |
file = File.basename(csv_path) + ".html" | |
File.open(file, "w+") do |f| | |
f.write(result) | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
#ressource2csv.sh | |
#launch it with | |
#ressource2csv.sh bundle exec rake tests | |
output_mem="/tmp/mem.csv" | |
output_cpu="/tmp/cpu.csv" | |
$@ & | |
pid=$(echo $!) | |
timer=0 | |
echo id,mysqld,rake,spec-cukes,java > $output_cpu | |
echo id,mysqld,rake,spec-cukes,java > $output_mem | |
while kill -0 $pid | |
do | |
sjava=$(ps -C java -o %cpu,%mem,command | grep java ) | |
cjava=$(echo $sjava | cut -d " " -f 1) | |
mjava=$(echo $sjava | cut -d " " -f 2) | |
smysqld=$(ps -C mysqld -o %cpu,%mem,command | grep mysqld) | |
cmysqld=$(echo $smysqld | cut -d " " -f 1) | |
mmysqld=$(echo $smysqld | cut -d " " -f 2) | |
sruby_rake=$(ps -C rake -o %cpu,%mem,command | grep rake ) | |
cruby_rake=$(echo $sruby_rake | cut -d " " -f 1) | |
mruby_rake=$(echo $sruby_rake | cut -d " " -f 2) | |
sruby_other=$(ps -C ruby -o %cpu,%mem,command | grep ruby | grep -v rake ) | |
cruby_other=$(echo $sruby_other | cut -d " " -f 1) | |
mruby_other=$(echo $sruby_other | cut -d " " -f 2) | |
echo $timer,$cmysqld,,$cruby_rake,,$cruby_other,$cjava >> $output_cpu | |
echo $timer,$mmysqld,$mruby_rake,$mruby_other,$mjava >> $output_mem | |
sleep 1 | |
let timer=timer+1 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment