Skip to content

Commit ef909e0

Browse files
committed
Include a test/profile directory. Abstract out Rakefile libs and dirs to share in new :performance namespace test tasks. Ignore the output directory.
1 parent 1f605e5 commit ef909e0

File tree

5 files changed

+112
-11
lines changed

5 files changed

+112
-11
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ doc/
66
*.gem
77
.bundle
88
Gemfile.lock
9-
9+
test/profile/output/*

Rakefile

Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,33 @@
11
require 'rake'
22
require 'rake/testtask'
33
require 'rake/rdoctask'
4+
begin ; require 'ruby-prof' ; rescue LoadError ; end
5+
6+
7+
def test_libs(mode='odbc')
8+
['lib',
9+
'test',
10+
"test/connections/native_sqlserver#{mode == 'adonet' ? '' : "_#{mode}"}",
11+
"#{ENV['RAILS_SOURCE']}/activerecord/test"]
12+
end
13+
14+
def test_files
15+
Dir.glob("test/cases/**/*_test_sqlserver.rb").sort +
16+
(Dir.glob("#{ENV['RAILS_SOURCE']}/activerecord/test/cases/**/*_test.rb") -
17+
Dir.glob("#{ENV['RAILS_SOURCE']}/activerecord/test/cases/adapters/**/*_test.rb")).sort
18+
end
419

520

621
task :test => ['test:odbc']
722

23+
824
namespace :test do
925

1026
['odbc','adonet'].each do |mode|
11-
27+
1228
Rake::TestTask.new(mode) do |t|
13-
ENV['ENABLE_DEFAULT_UNICODE_TYPES'] = 'true'
14-
t.libs << "test"
15-
t.libs << "test/connections/native_sqlserver#{mode == 'adonet' ? '' : "_#{mode}"}"
16-
t.libs << "#{ENV['RAILS_SOURCE']}/activerecord/test"
17-
t.test_files = \
18-
Dir.glob("test/cases/**/*_test_sqlserver.rb").sort +
19-
(Dir.glob("#{ENV['RAILS_SOURCE']}/activerecord/test/cases/**/*_test.rb") -
20-
Dir.glob("#{ENV['RAILS_SOURCE']}/activerecord/test/cases/adapters/**/*_test.rb")).sort
29+
t.libs = test_libs(mode)
30+
t.test_files = test_files
2131
t.verbose = true
2232
end
2333

@@ -33,6 +43,22 @@ namespace :test do
3343
end
3444

3545

46+
namespace :profile do
47+
48+
['odbc','adonet'].each do |mode|
49+
namespace mode.to_sym do
50+
51+
Rake::TestTask.new('connection') do |t|
52+
t.libs = test_libs(mode)
53+
t.test_files = ["test/profile/connection.rb"]
54+
t.verbose = true
55+
end
56+
57+
end
58+
end
59+
60+
end if defined?(RubyProf)
61+
3662

3763
namespace :rvm do
3864

test/cases/sqlserver_helper.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ def method_added(method)
6767

6868
# Set weather to test unicode string defaults or not. Used from rake task.
6969

70-
if ENV['ENABLE_DEFAULT_UNICODE_TYPES'] == 'true'
70+
if ENV['ENABLE_DEFAULT_UNICODE_TYPES'] != 'false'
7171
puts "With enabled unicode string types"
7272
ActiveRecord::ConnectionAdapters::SQLServerAdapter.enable_default_unicode_types = true
7373
end

test/profile/connection.rb

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
require 'profile/helper'
2+
require 'models/topic'
3+
require 'models/reply'
4+
5+
class ProfileConnectionCase < ActiveRecord::TestCase
6+
7+
fixtures :topics
8+
9+
def setup
10+
@connection = ActiveRecord::Base.connection
11+
end
12+
13+
def test_select
14+
# Ruby 1.8.7 p302
15+
# 6.180643
16+
ruby_profile :connection_select do
17+
1000.times { @connection.send :select, "SELECT [topics].* FROM [topics]" }
18+
end
19+
end
20+
21+
22+
end
23+
24+

test/profile/helper.rb

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
require 'cases/sqlserver_helper'
2+
require 'ruby-prof'
3+
4+
class ActiveRecord::TestCase
5+
6+
7+
protected
8+
9+
def ruby_profile(name)
10+
result = RubyProf.profile { yield }
11+
[:flat,:graph,:html].each do |printer|
12+
save_ruby_prof_report(result, name, printer)
13+
end
14+
end
15+
16+
def save_ruby_prof_report(result, name, printer)
17+
ptr = case printer
18+
when :flat then RubyProf::FlatPrinter
19+
when :graph then RubyProf::GraphPrinter
20+
when :html then RubyProf::GraphHtmlPrinter
21+
end
22+
file_name = printer == :html ? "#{name}_graph.html" : "#{name}_#{printer}.txt"
23+
file_path = File.join(SQLSERVER_TEST_ROOT, 'profile', 'output', file_name)
24+
File.open(file_path,'w') do |file|
25+
printer == :html ? ptr.new(result).print(file) : ptr.new(result).print(file,0)
26+
end
27+
end
28+
29+
end
30+
31+
# RubyProf::FlatPrinter Creates a flat report in text format
32+
# RubyProf::GraphPrinter Creates a call graph report in text format
33+
# RubyProf::GraphHtmlPrinter Creates a call graph report in HTML (separate files per thread)
34+
35+
# RubyProf::PROCESS_TIME process time
36+
# RubyProf::WALL_TIME wall time
37+
# RubyProf::CPU_TIME cpu time
38+
# RubyProf::ALLOCATIONS object allocations
39+
# RubyProf::MEMORY memory usage
40+
# RubyProf::GC_RUNS garbage collections runs
41+
# RubyProf::GC_TIME garbage collection time
42+
43+
# RubyProf.measure_mode = RubyProf::PROCESS_TIME
44+
# RubyProf.measure_mode = RubyProf::WALL_TIME
45+
# RubyProf.measure_mode = RubyProf::CPU_TIME
46+
# RubyProf.measure_mode = RubyProf::ALLOCATIONS
47+
# RubyProf.measure_mode = RubyProf::MEMORY
48+
# RubyProf.measure_mode = RubyProf::GC_RUNS
49+
# RubyProf.measure_mode = RubyProf::GC_TIME
50+
51+

0 commit comments

Comments
 (0)