myronmarston-test_benchmarker 1.0.1 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.rdoc CHANGED
@@ -1,7 +1,7 @@
1
1
  = test_benchmarker
2
2
 
3
- test_benchmarker is a ruby library that benchmarks your Test::Unit test. It's intended to help you
4
- pinpoint which tests take longer so you can improve the speed of your test suite.
3
+ test_benchmarker is a ruby library that benchmarks your Test::Unit test. It's intended to help you
4
+ pinpoint which tests take longer so you can improve the speed of your test suite.
5
5
 
6
6
  == Download
7
7
 
@@ -15,22 +15,27 @@ Gem:
15
15
  Require it in your ruby project:
16
16
  require 'rubygems'
17
17
  require 'test_benchmarker'
18
-
18
+
19
19
  Or in Rails, with Rails 2.1+ gem support, add to your config/environments/test.rb:
20
- config.gem 'myronmarston-test_benchmarker',
21
- :lib => 'test_benchmarker',
20
+ config.gem 'myronmarston-test_benchmarker',
21
+ :lib => 'test_benchmarker',
22
22
  :source => 'http://gems.github.com'
23
23
 
24
- Your tests will only be benchmarked when you set a value for the BENCHMARK_TESTS environment variable.
24
+ Your tests will only be benchmarked when you set the appropriate environment variable. Use BENCHMARK_TESTS
25
+ to have get the benchmark tests written to stdout.
26
+
25
27
  When running tests from a single ruby file:
26
28
  BENCHMARK_TESTS=true ruby path/to/test.rb
27
29
 
28
30
  Or, when using rake:
29
31
  rake test BENCHMARK_TESTS=true
30
32
 
33
+ Alternately, if you want the test benchmarks written to a file, you can use TEST_BENCHMARKS_FILE:
34
+ rake test TEST_BENCHMARKS_FILE=path/to/file
35
+
31
36
  == Example Output
32
37
 
33
- test_benchmarker prints two reports:
38
+ test_benchmarker prints two reports:
34
39
 
35
40
  1. Class Benchmark Results: Lists each of the Test::Unit::TestCase subclasses in the test run, from the slowest average test time to the fastest.
36
41
  2. Test Benchmark Results: Lists each of the tests that ran, from the slowest to the fastest.
@@ -44,9 +49,9 @@ Example output (taken from benchmarking {will_paginate's}[http://wiki.github.com
44
49
  4. 0.000 secs avg time, 0.004 secs total time, 12 tests for: ArrayPaginationTest
45
50
  5. 0.000 secs avg time, 0.000 secs total time, 1 tests for: WillPaginate::ViewTestCase
46
51
  ===============================================================================
47
-
48
-
49
-
52
+
53
+
54
+
50
55
  ============================ Test Benchmark Results ============================
51
56
  1. 0.084 secs total time for: test_ability_to_use_with_custom_finders(FinderTest)
52
57
  2. 0.071 secs total time for: test_paginate_associations(FinderTest)
data/VERSION.yml ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ :minor: 2
3
+ :patch: 0
4
+ :major: 1
@@ -3,65 +3,66 @@ require 'ostruct'
3
3
  module TestBenchmarker
4
4
  class TestBenchmark
5
5
  attr_reader :test_class, :test_name, :benchmark
6
-
6
+
7
7
  def initialize(test_class, test_name, benchmark)
8
8
  @test_class, @test_name, @benchmark = test_class, test_name, benchmark
9
9
  TestBenchmarks.add(self)
10
10
  end
11
11
  end
12
-
12
+
13
13
  class TestBenchmarks
14
14
  def self.clear
15
15
  @@classes = {}
16
16
  @@tests = []
17
17
  end
18
-
18
+
19
19
  self.clear
20
-
20
+
21
21
  def self.add(benchmark)
22
22
  test_class = benchmark.test_class
23
-
23
+
24
24
  # ignore some bogus test classes that get passed here when run in the context of a rails app...
25
25
  return if test_class =~ /(rake_test_loader|::TestCase|::IntegrationTest)/
26
-
26
+
27
27
  begin
28
28
  test_class = test_class.constantize
29
29
  rescue NameError
30
30
  return
31
31
  end
32
32
  return unless test_class.is_subclass_of?(Test::Unit::TestCase)
33
-
33
+
34
34
  @@classes[test_class] ||= OpenStruct.new
35
35
  @@classes[test_class].benchmarks ||= []
36
36
  @@classes[test_class].benchmarks << benchmark
37
37
  @@tests << benchmark
38
38
  end
39
-
40
- def self.print_results
39
+
40
+ def self.results
41
41
  return if @@classes.nil? || @@classes.size == 0
42
-
42
+ results = StringIO.new
43
43
  benchmark_attr = :real
44
-
44
+
45
45
  class_benchmarks = []
46
- @@classes.each do |test_class, obj|
46
+ @@classes.each do |test_class, obj|
47
47
  obj.test_count = obj.benchmarks.size
48
48
  obj.sum = obj.benchmarks.inject(0) {|sum, bmark| sum + bmark.benchmark.send(benchmark_attr)}
49
49
  obj.avg = obj.sum / obj.test_count
50
50
  obj.test_class = test_class
51
51
  class_benchmarks << obj unless obj.benchmarks.nil? || obj.benchmarks.size == 0
52
52
  end
53
-
54
- puts "\n\n#{'=' * 27} Class Benchmark Results #{'=' * 27}"
53
+
54
+ results.puts "\n\n#{'=' * 27} Class Benchmark Results #{'=' * 27}"
55
55
  class_benchmarks.sort {|a, b| b.avg <=> a.avg}.each_with_index do |cb, i|
56
- puts "#{i + 1}.#{' ' * (4 - (i + 1).to_s.length)} #{format("%.3f", cb.avg)} secs avg time, #{format("%.3f", cb.sum)} secs total time, #{cb.test_count} tests for: #{cb.test_class.to_s}"
56
+ results.puts "#{i + 1}.#{' ' * (4 - (i + 1).to_s.length)} #{format("%.3f", cb.avg)} secs avg time, #{format("%.3f", cb.sum)} secs total time, #{cb.test_count} tests for: #{cb.test_class.to_s}"
57
57
  end
58
- puts "#{'=' * 79}\n\n"
59
-
60
- puts "\n\n#{'=' * 28} Test Benchmark Results #{'=' * 28}"
58
+ results.puts "#{'=' * 79}\n\n"
59
+
60
+ results.puts "\n\n#{'=' * 28} Test Benchmark Results #{'=' * 28}"
61
61
  @@tests.sort {|a, b| b.benchmark.send(benchmark_attr) <=> a.benchmark.send(benchmark_attr)}.each_with_index do |t, i|
62
- puts "#{i + 1}.#{' ' * (4 - (i + 1).to_s.length)} #{format("%.3f", t.benchmark.real)} secs total time for: #{t.test_name}"
62
+ results.puts "#{i + 1}.#{' ' * (4 - (i + 1).to_s.length)} #{format("%.3f", t.benchmark.real)} secs total time for: #{t.test_name}"
63
63
  end
64
- puts "#{'=' * 80}\n\n"
64
+ results.puts "#{'=' * 80}\n\n"
65
+ results.string
65
66
  end
66
67
  end
67
68
  end
@@ -1,9 +1,9 @@
1
1
  require 'benchmark'
2
2
 
3
- if ENV['BENCHMARK_TESTS']
3
+ if ENV['BENCHMARK_TESTS'] || ENV['TEST_BENCHMARKS_FILE']
4
4
  class Test::Unit::TestSuite
5
5
  @@run_count = 0
6
-
6
+
7
7
  def run(result, &progress_block)
8
8
  @@run_count += 1
9
9
  begin
@@ -15,7 +15,14 @@ if ENV['BENCHMARK_TESTS']
15
15
  ensure
16
16
  @@run_count -= 1
17
17
  # print the results as we're exiting the very last test run...
18
- TestBenchmarker::TestBenchmarks.print_results if @@run_count == 0
18
+ if @@run_count == 0
19
+ results = TestBenchmarker::TestBenchmarks.results
20
+ if ENV['TEST_BENCHMARKS_FILE']
21
+ File.open(ENV['TEST_BENCHMARKS_FILE'], 'w') { |f| f << results }
22
+ else
23
+ puts results
24
+ end
25
+ end
19
26
  end
20
27
  end
21
28
  end
@@ -1,7 +1,7 @@
1
1
  require File.dirname(__FILE__) + '/test_helper.rb'
2
2
 
3
3
  class Foo; end
4
- class Class1 < Test::Unit::TestCase
4
+ class Class1 < Test::Unit::TestCase
5
5
  def test_truth; end
6
6
  end
7
7
 
@@ -23,33 +23,33 @@ class TestTestBenchmarker < Test::Unit::TestCase
23
23
  TestBenchmarker::TestBenchmark.new("Class#{class_speed}", "test_#{test_speed} (Class#{class_speed})", ::Benchmark.measure { sleep speed })
24
24
  end
25
25
  end
26
-
26
+
27
27
  TestBenchmarker::TestBenchmark.new("Class4", "test_1 (Class4)", ::Benchmark.measure { sleep 0.01 })
28
-
28
+
29
29
  @benchmarked_classes = TestBenchmarker::TestBenchmarks.send(:class_variable_get, '@@classes')
30
30
  @benchmarked_tests = TestBenchmarker::TestBenchmarks.send(:class_variable_get, '@@tests').map{ |t| t.test_name }
31
- @out, @err = util_capture { TestBenchmarker::TestBenchmarks.print_results }
31
+ @out = TestBenchmarker::TestBenchmarks.results
32
32
  end
33
-
33
+
34
34
  def teardown
35
35
  TestBenchmarker::TestBenchmarks.clear
36
36
  end
37
-
37
+
38
38
  def test_has_expected_benchmarked_classes
39
39
  assert_equal 3, @benchmarked_classes.keys.size
40
-
40
+
41
41
  assert @benchmarked_classes.keys.include?(Class1)
42
42
  assert @benchmarked_classes.keys.include?(Class2)
43
43
  assert @benchmarked_classes.keys.include?(Class3)
44
44
  end
45
-
45
+
46
46
  def test_does_not_have_classes_not_descended_from_test_unit_testcase
47
47
  assert !@benchmarked_classes.keys.include?(Class4)
48
48
  end
49
-
49
+
50
50
  def test_has_expected_benchmarked_tests
51
51
  assert_equal 9, @benchmarked_tests.size
52
-
52
+
53
53
  assert @benchmarked_tests.include?('test_1 (Class1)')
54
54
  assert @benchmarked_tests.include?('test_2 (Class1)')
55
55
  assert @benchmarked_tests.include?('test_3 (Class1)')
@@ -60,18 +60,18 @@ class TestTestBenchmarker < Test::Unit::TestCase
60
60
  assert @benchmarked_tests.include?('test_2 (Class3)')
61
61
  assert @benchmarked_tests.include?('test_3 (Class3)')
62
62
  end
63
-
63
+
64
64
  def test_does_not_have_tests_not_descended_from_test_unit_testcase
65
65
  assert !@benchmarked_tests.include?("test_1 (Class4)")
66
66
  end
67
-
67
+
68
68
  def test_class_benchmark_results
69
69
  # should be in order from slowest to fastest
70
70
  assert_match class_benchmark_regex('Class3', 1), @out
71
71
  assert_match class_benchmark_regex('Class2', 2), @out
72
72
  assert_match class_benchmark_regex('Class1', 3), @out
73
73
  end
74
-
74
+
75
75
  def test_test_benchmark_results
76
76
  # should be in order from slowest to fastest
77
77
  assert_match test_benchmark_regex('test_3 (Class3)', 1), @out
@@ -84,9 +84,9 @@ class TestTestBenchmarker < Test::Unit::TestCase
84
84
  assert_match test_benchmark_regex('test_2 (Class1)', 8), @out
85
85
  assert_match test_benchmark_regex('test_1 (Class1)', 9), @out
86
86
  end
87
-
87
+
88
88
  private
89
-
89
+
90
90
  # borrowed from zentest assertions...
91
91
  def util_capture
92
92
  require 'stringio'
@@ -104,12 +104,12 @@ class TestTestBenchmarker < Test::Unit::TestCase
104
104
  $stdout = orig_stdout
105
105
  $stderr = orig_stderr
106
106
  end
107
-
107
+
108
108
  def class_benchmark_regex(class_name, slowness_ranking)
109
109
  # http://rubular.com/regexes/6815
110
110
  /^#{slowness_ranking}\.\s+[\d\.]+ secs avg time, [\d\.]+ secs total time, [\d]+ tests for: #{Regexp.escape(class_name)}$/
111
111
  end
112
-
112
+
113
113
  def test_benchmark_regex(test_name, slowness_ranking)
114
114
  # http://rubular.com/regexes/6816
115
115
  /^#{slowness_ranking}.\s+[\d\.]+ secs total time for: #{Regexp.escape(test_name)}$/
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: myronmarston-test_benchmarker
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Myron Marston
@@ -9,44 +9,36 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-03-27 00:00:00 -07:00
12
+ date: 2009-04-14 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies: []
15
15
 
16
- description:
16
+ description: A tool for benchmarking ruby Test::Unit tests.
17
17
  email: myron.marston@gmail.com
18
18
  executables: []
19
19
 
20
20
  extensions: []
21
21
 
22
22
  extra_rdoc_files:
23
- - CHANGELOG
24
23
  - README.rdoc
25
24
  files:
26
- - CHANGELOG
27
25
  - Manifest.txt
28
26
  - README.rdoc
29
- - Rakefile
30
- - lib
27
+ - VERSION.yml
31
28
  - lib/test_benchmarker
32
- - lib/test_benchmarker.rb
33
29
  - lib/test_benchmarker/core_ext.rb
34
30
  - lib/test_benchmarker/test_benchmarks.rb
35
31
  - lib/test_benchmarker/test_suite.rb
36
- - test
32
+ - lib/test_benchmarker.rb
37
33
  - test/test_core_ext.rb
38
34
  - test/test_helper.rb
39
35
  - test/test_test_benchmarker.rb
40
36
  has_rdoc: true
41
- homepage: http://github.com/myronmarston/test_benchmarker
37
+ homepage: http://github.com/myronmarston/test_benchmarker/
42
38
  post_install_message:
43
39
  rdoc_options:
44
- - --main
45
- - README.rdoc
46
- - --title
47
- - TestBenchmarker Documentation
48
- - --charset
49
- - utf-8
40
+ - --inline-source
41
+ - --charset=UTF-8
50
42
  require_paths:
51
43
  - lib
52
44
  required_ruby_version: !ruby/object:Gem::Requirement
@@ -67,8 +59,6 @@ rubyforge_project:
67
59
  rubygems_version: 1.2.0
68
60
  signing_key:
69
61
  specification_version: 2
70
- summary: A tool for benchmarking ruby Test::Unit tests
71
- test_files:
72
- - test/test_core_ext.rb
73
- - test/test_helper.rb
74
- - test/test_test_benchmarker.rb
62
+ summary: A tool for benchmarking ruby Test::Unit tests.
63
+ test_files: []
64
+
data/CHANGELOG DELETED
@@ -1,7 +0,0 @@
1
- == 1.0.0 2009-03-27
2
-
3
- * Initial release
4
-
5
- == 1.0.1 2009-03-27
6
-
7
- * Changed to ActiveSupport's implementation of constantize so as not to use ObjectSpace.
data/Rakefile DELETED
@@ -1,42 +0,0 @@
1
- require 'rubygems'
2
- require 'rake/gempackagetask'
3
- require 'rake/testtask'
4
- require 'rake/rdoctask'
5
-
6
- task :default => :test
7
-
8
- desc "Run All Tests"
9
- Rake::TestTask.new :test do |test|
10
- test.test_files = ["test/**/*.rb"]
11
- test.verbose = true
12
- end
13
-
14
- desc %{Update ".manifest" with the latest list of project filenames. Respect\
15
- .gitignore by excluding everything that git ignores. Update `files` and\
16
- `test_files` arrays in "*.gemspec" file if it's present.}
17
- task :manifest do
18
- list = Dir['**/*'].sort
19
- spec_file = Dir['*.gemspec'].first
20
- list -= [spec_file] if spec_file
21
-
22
- if File.exist?('.gitignore')
23
- File.read('.gitignore').each_line do |glob|
24
- glob = glob.chomp.sub(/^\//, '')
25
- list -= Dir[glob]
26
- list -= Dir["#{glob}/**/*"] if File.directory?(glob) and !File.symlink?(glob)
27
- puts "excluding #{glob}"
28
- end
29
- end
30
-
31
- if spec_file
32
- spec = File.read spec_file
33
- spec.gsub! /^(\s* s.(test_)?files \s* = \s* )( \[ [^\]]* \] | %w\( [^)]* \) )/mx do
34
- assignment = $1
35
- bunch = $2 ? list.grep(/^test\//) : list
36
- '%s%%w(%s)' % [assignment, bunch.join(' ')]
37
- end
38
-
39
- File.open(spec_file, 'w') {|f| f << spec }
40
- end
41
- File.open('manifest.txt', 'w') {|f| f << list.join("\n") }
42
- end