Skip to content

Commit a3c2f54

Browse files
committed
Add GC profiling for ruby 1.9.
1 parent f0202fc commit a3c2f54

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

test/profile/gc_profile_case.rb

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
require 'benchmark'
2+
require 'cases/sqlserver_helper'
3+
require 'models/topic'
4+
require 'models/reply'
5+
6+
raise "GC allocation benchmarks only supported on Ruby 1.9!" unless RUBY_VERSION >= '1.9'
7+
8+
class GcProfileCase < ActiveRecord::TestCase
9+
10+
fixtures :topics
11+
12+
def setup
13+
create_mass_topics unless @created_mass_topics
14+
@connection = ActiveRecord::Base.connection
15+
@select_statement = "SELECT [topics].* FROM [topics]"
16+
end
17+
18+
def test_coercion
19+
bench_allocations('coercion') do
20+
Topic.all(:limit => 100).each do |t|
21+
t.attributes.keys.each do |k|
22+
t.send(k.to_sym)
23+
end
24+
end
25+
end
26+
end
27+
28+
def test_select
29+
bench_allocations('select') do
30+
@connection.send :select, @select_statement
31+
end
32+
end
33+
34+
def test_select_one
35+
bench_allocations('select_one') do
36+
100.times { @connection.select_one(@select_statement) }
37+
end
38+
end
39+
40+
41+
protected
42+
43+
def create_mass_topics
44+
GC::Profiler.clear
45+
GC::Profiler.disable
46+
all_topics = Topic.all
47+
100.times { all_topics.each { |t| Topic.create! t.attributes } }
48+
@created_mass_topics = true
49+
GC.start
50+
GC::Profiler.enable
51+
GC::Profiler.clear
52+
end
53+
54+
def bench_allocations(feature, iterations=10, &blk)
55+
puts "\nGC overhead for #{feature}"
56+
GC::Profiler.clear
57+
GC::Profiler.enable
58+
iterations.times{ blk.call }
59+
GC::Profiler.report(STDOUT)
60+
GC::Profiler.disable
61+
end
62+
63+
end
64+
65+
66+
67+
68+

0 commit comments

Comments
 (0)