simple-benchmark 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1aebdb166875ed2fa9e2268bb849394d0133e861
4
+ data.tar.gz: 38557edfbb82cec26453b87c0ac64f5be1ccb41c
5
+ SHA512:
6
+ metadata.gz: 4109d128a70c0e07bfe7436e17a8800e2cabdb6ea4f6f0275c380d579a43e1faa74ea0378a7738aa2bc885aaf41553d48a4ed9fe7a07e19522ad97f277aee9fd
7
+ data.tar.gz: 00be7e7fd2260e14e6f20fd58034443b1f00a9a0f0a760980d209e8bdf042b82b93065ca0ec1e427bf1d8233b31854ca16b7edef6bd3290eae56485b611c0397
@@ -0,0 +1 @@
1
+ require 'simple_benchmark'
@@ -0,0 +1,48 @@
1
+ require 'benchmark'
2
+ require 'logger'
3
+
4
+ require 'simple_benchmark/version'
5
+
6
+ class SimpleBenchmark
7
+ autoload :Helper, 'simple_benchmark/helper'
8
+
9
+ class << self
10
+ attr_writer :logger, :enabled
11
+ end
12
+
13
+ self.enabled = true
14
+
15
+ def self.logger
16
+ @logger ||= default_logger
17
+ end
18
+
19
+ def self.enabled?
20
+ @enabled == true
21
+ end
22
+
23
+ def initialize(description, logger = nil)
24
+ @description = description
25
+ @logger = logger
26
+ end
27
+
28
+ def run(&block)
29
+ result = nil
30
+ report = Benchmark.measure do
31
+ result = block.call
32
+ end
33
+
34
+ logger.info("#{@description}: #{report.real} second(s)")
35
+ result
36
+ end
37
+
38
+ private
39
+
40
+ def self.default_logger
41
+ defined?(Rails) ? Rails.logger : Logger.new('benchmark.log')
42
+ end
43
+
44
+ def logger
45
+ @logger ||= self.class.logger
46
+ end
47
+
48
+ end
@@ -0,0 +1,12 @@
1
+ class SimpleBenchmark
2
+ module Helper
3
+ def log_execution_for(description, options = {}, &block)
4
+ if SimpleBenchmark.enabled?
5
+ benchmark = SimpleBenchmark.new(description, options[:logger])
6
+ benchmark.run(&block)
7
+ else
8
+ block.call
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,3 @@
1
+ class SimpleBenchmark
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,61 @@
1
+ require 'spec_helper'
2
+
3
+ class Example
4
+ include SimpleBenchmark::Helper
5
+
6
+ def do_a_thing
7
+ log_execution_for('something') do
8
+ sleep(0.1)
9
+ 'done'
10
+ end
11
+ end
12
+
13
+ def do_a_thing_with_another_logger(logger)
14
+ log_execution_for('something else', :logger => logger) do
15
+ 'done'
16
+ end
17
+ end
18
+ end
19
+
20
+ describe SimpleBenchmark::Helper do
21
+ describe "#log_execution_for" do
22
+ let(:benchmarker) { double(:benchmarker) }
23
+
24
+ subject { Example.new }
25
+
26
+ it "logs execution for the code block" do
27
+ benchmarker.should_receive(:run)
28
+
29
+ SimpleBenchmark.stub(:new).with('something', nil).and_return(benchmarker)
30
+
31
+ subject.do_a_thing
32
+ end
33
+
34
+ it "returns the value of the block" do
35
+ subject.do_a_thing.should == 'done'
36
+ end
37
+
38
+ it "passes in a custom logger" do
39
+ benchmarker.stub(:run)
40
+ logger = 'logger'
41
+
42
+ SimpleBenchmark.should_receive(:new).with('something else', 'logger').and_return(benchmarker)
43
+
44
+ subject.do_a_thing_with_another_logger('logger')
45
+ end
46
+
47
+ context "when benchmarking is disabled" do
48
+ before { SimpleBenchmark.enabled = false }
49
+ after { SimpleBenchmark.enabled = true }
50
+
51
+ it "does not log execution" do
52
+ SimpleBenchmark.should_receive(:new).never
53
+
54
+ subject.do_a_thing
55
+ end
56
+ it "returns the value of the block" do
57
+ subject.do_a_thing.should == 'done'
58
+ end
59
+ end
60
+ end
61
+ end
@@ -0,0 +1,82 @@
1
+ require 'spec_helper'
2
+
3
+ describe SimpleBenchmark do
4
+ def remove_log_files(*names)
5
+ names.each {|n| FileUtils.rm_f(n) }
6
+ end
7
+
8
+ def log_lines(filename)
9
+ File.read(filename).split(/\n+/)
10
+ end
11
+
12
+ def reset_global_logger
13
+ described_class.instance_variable_set(:@logger, nil)
14
+ end
15
+
16
+
17
+ describe ".enabled?" do
18
+ it "is true by default" do
19
+ described_class.enabled?.should be(true)
20
+ end
21
+
22
+ it "is false when set to false" do
23
+ described_class.enabled = false
24
+ described_class.enabled?.should be(false)
25
+ end
26
+ end
27
+
28
+ describe ".logger" do
29
+ before { reset_global_logger }
30
+ after { reset_global_logger }
31
+
32
+ it "uses a default file" do
33
+ Logger.stub(:new).with('benchmark.log').and_return('logger')
34
+ described_class.logger.should == 'logger'
35
+ end
36
+
37
+ it "uses the Rails logger if available" do
38
+ Rails = Class.new { def self.logger; 'rails logger'; end }
39
+
40
+ described_class.logger.should == 'rails logger'
41
+
42
+ Object.send(:remove_const, :Rails)
43
+ end
44
+
45
+ it "uses the user-provided logger" do
46
+ described_class.logger = 'logger'
47
+ described_class.logger.should == 'logger'
48
+ end
49
+ end
50
+
51
+ describe "#run" do
52
+ let(:log_files) { %w(benchmark.log alternate.log) }
53
+
54
+ before { remove_log_files(*log_files) }
55
+ after { remove_log_files(*log_files) }
56
+
57
+ let(:loggable) { lambda { 'done' } }
58
+
59
+ it "logs the response time" do
60
+ subject = described_class.new('code')
61
+ subject.run { sleep(0.1) }
62
+
63
+ lines = log_lines('benchmark.log')
64
+
65
+ lines.length.should == 2
66
+ lines.last.should match(/code:\s+\d+\.\d+\s+second\(s\)/)
67
+ end
68
+
69
+ it "returns the value of the original block" do
70
+ subject = described_class.new('code')
71
+ subject.run { 'done' }.should == 'done'
72
+ end
73
+
74
+ it "uses an alternate logger when requested" do
75
+ logger = Logger.new('alternate.log')
76
+ subject = described_class.new('alternate', logger)
77
+
78
+ expect { subject.run { 'done' } }.to change { log_lines('alternate.log').length }.by(1)
79
+ end
80
+ end
81
+
82
+ end
@@ -0,0 +1,2 @@
1
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
2
+ require 'simple_benchmark'
metadata ADDED
@@ -0,0 +1,96 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: simple-benchmark
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Patrick Reagan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description:
56
+ email:
57
+ - reaganpr@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - lib/simple-benchmark.rb
63
+ - lib/simple_benchmark/helper.rb
64
+ - lib/simple_benchmark/version.rb
65
+ - lib/simple_benchmark.rb
66
+ - spec/simple_benchmark/helper_spec.rb
67
+ - spec/simple_benchmark_spec.rb
68
+ - spec/spec_helper.rb
69
+ homepage: http://viget.com/extend
70
+ licenses:
71
+ - MIT
72
+ metadata: {}
73
+ post_install_message:
74
+ rdoc_options: []
75
+ require_paths:
76
+ - lib
77
+ required_ruby_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - '>='
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ requirements: []
88
+ rubyforge_project:
89
+ rubygems_version: 2.0.14
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: A simple benchmarking tool for Ruby apps.
93
+ test_files:
94
+ - spec/simple_benchmark/helper_spec.rb
95
+ - spec/simple_benchmark_spec.rb
96
+ - spec/spec_helper.rb