github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

timocratic / test_benchmark

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 67
    • 4
  • Source
  • Commits
  • Network (4)
  • Issues (3)
  • Downloads (0)
  • Wiki (1)
  • Graphs
  • Tree: c96a0a5

click here to add a description

click here to add a homepage

  • Branches (2)
    • master
    • output_once
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

Rails plugin (and/or ruby gem) for benchmarking your test::units: a rework of the original by topfunky — Read more

  cancel

http://www.timocracy.com/articles/2009/01/13/a-revamped-plugin-gem-for-benchmarking-your-ruby-rails-test-units

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

added a comment so the old hooks are more obvious within my codebase 
timocratic (author)
Tue Jan 13 12:02:24 -0800 2009
commit  c96a0a54fede60e41b52d88c10dc282c836696c0
tree    ecc0dce7b0881ecad1eb08e944ce26d391b666e0
parent  341de746de06473ed3db612ed9ca661e6f04a7bc
test_benchmark / lib / test_benchmark.rb lib/test_benchmark.rb
100644 83 lines (71 sloc) 2.532 kb
edit raw blame history
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
unless %w{false none}.include?(ENV['BENCHMARK'])
 
require 'test/unit'
require 'test/unit/testresult'
require 'test/unit/testcase'
require 'test/unit/ui/console/testrunner'
 
class Test::Unit::UI::Console::TestRunner
  include Loggable if const_defined?(:Loggable)
  
  alias attach_to_mediator_old attach_to_mediator
  # def attach_to_mediator_old
  # @mediator.add_listener(TestResult::FAULT, &method(:add_fault))
  # @mediator.add_listener(TestRunnerMediator::STARTED, &method(:started))
  # @mediator.add_listener(TestRunnerMediator::FINISHED, &method(:finished))
  # @mediator.add_listener(TestCase::STARTED, &method(:test_started))
  # @mediator.add_listener(TestCase::FINISHED, &method(:test_finished))
  # end
  def attach_to_mediator
    attach_to_mediator_old
    @mediator.add_listener(Test::Unit::TestSuite::STARTED, &method(:test_suite_started))
    @mediator.add_listener(Test::Unit::TestSuite::FINISHED, &method(:test_suite_finished))
  end
  
  alias started_old started
  def started(result)
    started_old(result)
    @benchmark_times = {}
  end
  
  alias finished_old finished
  def finished(elapsed_time)
    finished_old(elapsed_time)
    benchmarks = @benchmark_times.sort{|a, b| b[1] <=> a[1]}
    output_benchmarks(benchmarks, true)
    benchmarks = benchmarks.slice(0,10) unless ENV['BENCHMARK'] == 'full'
    output_benchmarks(benchmarks)
  end
  
  alias test_started_old test_started
  def test_started(name)
    test_started_old(name)
    @benchmark_times[name] = Time.now
  end
  
  alias test_finished_old test_finished
  def test_finished(name)
    test_finished_old(name)
    @benchmark_times[name] = Time.now - @benchmark_times[name]
  end
  
  def test_suite_started(name)
  end
  
  def test_suite_finished(name)
    return unless ENV['BENCHMARK'] == 'full'
    benchmarks = @benchmark_times.select{ |k,v| k.include?(name) }.sort{|a, b| b[1] <=> a[1]}
    output_benchmarks(benchmarks, false, name) unless benchmarks.length == 0
  end
  
  def format_benchmark_row(tuple)
    ("%0.3f" % tuple[1]) + " #{tuple[0]}"
  end
  
  def output_benchmarks(benchmarks, use_logger=false, name=nil)
    return if use_logger && !defined?(logger)
    if name
      header = "\nTest Benchmark Times: #{name}"
    else
      header = "\nOVERALL TEST BENCHMARK TIMES"
    end
    strings = benchmarks.map {|tuple| ("%0.3f" % tuple[1]) + " #{tuple[0]}"}
    if use_logger
      logger.debug header
      logger.debug strings.join("\n")
    else
      puts header
      puts strings
    end
  end
end
 
end
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server