Skip to content

Instantly share code, notes, and snippets.

@janx
Last active December 17, 2015 18:59
Show Gist options
  • Select an option

  • Save janx/5657484 to your computer and use it in GitHub Desktop.

Select an option

Save janx/5657484 to your computer and use it in GitHub Desktop.
ObjectSpace.count_objects graph middleware
class ObjectSpaceMiddleware
GRANULITY = 50
@@files = {}
@@count = 0
def self.file(type)
unless @@files[type]
@@files[type] = File.open File.expand_path("../../../tmp/count_#{type}", __FILE__), 'a'
end
@@files[type]
end
def initialize(app, options={})
@app = app
@options = options
end
def call(env)
#call_with_recorder(env)
call_with_type_analyzer(env, Array)
end
private
# T_ARRAY -317191
# T_STRING -267536
# T_NODE -124612
# T_DATA -42698
# T_MATCH -11245
# T_HASH -5209
# T_OBJECT -4007
# T_REGEXP -1629
# T_STRUCT -1108
# T_CLASS -562
# T_BIGNUM -288
# T_FILE -52
# T_FLOAT -40
# T_ICLASS -15
# T_MODULE 0
# T_COMPLEX 0
# TOTAL 0
# T_RATIONAL 0
# FREE 776192
def call_with_type_analyzer(env, type)
GC.disable
before = ObjectSpace.each_object(type).to_a
result = @app.call(env)
diff = ObjectSpace.each_object(type).to_a - before
File.open(File.expand_path("../../../tmp/object_dump_#{type}", __FILE__), 'w') do |f|
diff.each do |a|
f.puts a.inspect[0,300]
end
end
diff = before = nil
GC.enable
GC.start
result
end
def call_with_recorder(env)
request = Rack::Request.new(env)
result = @app.call(env)
@@count += 1
record if @@count % GRANULITY == 0
result
end
def record
ObjectSpace.count_objects.each do |type, count|
f = ObjectSpaceMiddleware.file(type)
f.puts count
f.flush
end
end
end
if $0 == __FILE__
require 'erubis'
require 'json'
n = 10000
c = 2
url = "http://local.apartmentguide.com/apartments/Georgia/Norcross/Park-Trace/37/"
ab = "ab -n #{n} -c #{c} #{url}"
puts `rm #{File.expand_path("../../../tmp/count_*", __FILE__)}`
puts `#{ab}`
input = File.expand_path "../object_space_template.html.erb", __FILE__
output = "/home/jan/projects/work/some-pages/ag/object_space_benchmark_#{n}_#{c}_#{ObjectSpaceMiddleware::GRANULITY}.html"
eruby = Erubis::Eruby.new File.read(input)
colors = %w(steelblue tan tomato turquoise violet wheat yellow yellowgreen springgreen skyblue sandybrown salmon royalblue saddlebrown seashell olive navy orange peru pink purple red moccasin mistyrose lime limegreen khaki magenta maroon gold)
sources = Dir[File.expand_path("../../../tmp/count_*", __FILE__)]
series = []
sources.each_with_index do |f, i|
f =~ /count_(.+)$/
name = $1
data = []
File.readlines(f).each_with_index do |l, j|
data << {x:(j+1)*ObjectSpaceMiddleware::GRANULITY, y:l.strip.to_i}
end
series.push({
name: name,
color: colors[i],
data: data
})
end
File.open(output, 'w') do |f|
f.write eruby.result(binding())
end
else
use ObjectSpaceMiddleware
end
<!doctype html>
<link type="text/css" rel="stylesheet" href="rickshaw/src/css/graph.css">
<link type="text/css" rel="stylesheet" href="rickshaw/rickshaw.css">
<script src="rickshaw/vendor/d3.v2.js"></script>
<script src="rickshaw/rickshaw.js"></script>
<style>
body {
padding-left: 40px;
}
.chart_container {
}
.chart {
float: left;
}
.y_axis {
float: left;
margin-left: -50px;
}
.x_axis {
float: left;
}
.legend {
float: left;
margin: -600px 0 0 -60px;
}
</style>
<h1>ObjectSpace Benchmark</h1>
<p>Linux vaio 3.8.11-1-ARCH #1 SMP PREEMPT, ruby 1.9.3p327 (2012-11-10 revision 37606) [x86_64-linux], Intel(R) Core(TM) i5-2430M CPU @ 2.40GHz, 8G Memory</p>
<p>Granulity: <%= ObjectSpaceMiddleware::GRANULITY %></p>
<div id="graph">
<h2>Object Counts</h2>
<h5><%= ab %></h5>
<div class="chart_container">
<div class="y_axis"></div>
<div class="chart"></div>
<div class="x_axis"></div>
</div>
<div class="legend"></div>
</div>
<div style='clear:both;'></div>
<script>
var graph = new Rickshaw.Graph( {
element: document.querySelector("#graph .chart"),
width: 1000,
height: 600,
renderer: 'line',
series: <%= JSON.dump series %>
});
new Rickshaw.Graph.Axis.X( {
graph: graph,
orientation: 'bottom',
element: document.querySelector('#graph .x_axis'),
pixelsPerTick: 100
} );
new Rickshaw.Graph.Axis.Y( {
graph: graph,
orientation: 'left',
element: document.querySelector('#graph .y_axis'),
pixelsPerTick: 50
} );
var legend = new Rickshaw.Graph.Legend( {
graph: graph,
element: document.querySelector('#graph .legend')
} );
var shelving = new Rickshaw.Graph.Behavior.Series.Toggle({
graph: graph,
legend: legend
});
var order = new Rickshaw.Graph.Behavior.Series.Highlight({
graph: graph,
legend: legend
});
graph.render();
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment