benchmark_requires 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7b07e2c540d6ec7f257cd936a9bb92717d539ce3
4
+ data.tar.gz: d6c3acf3bd94e9f79b01d0b3afa27d7ce39fbbe3
5
+ SHA512:
6
+ metadata.gz: 853593d2b6935448749ac4f1e39d7f7aa863af035fe95dae9d239db15f0c576b5934cfa301475182b4a9e14449280e63ff514f6c73416820dbcc494d2d6b59ca
7
+ data.tar.gz: 6ca91dc35d13df8aff6a0f740cfaac89eb074f239fcbc61b844a7ab6b7c219643a142d782646e95b92ecc4bf529c5eaf2aefddcdee95264e73c8287978ee83e4
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2013 Jon Daniel
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
9
+ the Software, and to permit persons to whom the Software is furnished to do so,
10
+ subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
17
+ FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
18
+ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
19
+ IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
20
+ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,58 @@
1
+ benchmark_requires
2
+ ==================
3
+
4
+ BenchmarkRequires is a simple gem that helps identify slow loading libraries.
5
+
6
+ As applications get older and more complex the start-up time tends to increase
7
+ quite dramatically. BenchmarkRequires helps by logging all requires/load and
8
+ load times.
9
+
10
+ Idea inspired by http://nationbuilder.com/blistering_rails_performance_part_1_boot_performance
11
+
12
+ ## Usage
13
+
14
+ To use benchmark_requires simply require and initialize the library as early in
15
+ your application's loading process as possible. All necessary configuration will
16
+ be handled "just in time" when the first require/load is benchmarked.
17
+
18
+ ```ruby
19
+ require 'benchmark_requires'
20
+ BenchmarkRequires.setup!
21
+
22
+ # now load the rest of your app
23
+ ```
24
+
25
+ Startup your application and keep an eye on STDOUT (don't worry, you can
26
+ change this too).
27
+
28
+ ## Using a Custom Logger and Log Action
29
+
30
+ By default benchmark_requires uses `Logger` and all logged actions will be sent
31
+ to `:debug`. Changing these defaults is easy.
32
+
33
+ ```ruby
34
+ require 'benchmark_requires'
35
+
36
+ BenchmarkRequires.logger = Rails.logger
37
+ BenchmarkRequires.logger = Logger.new STDERR
38
+ BenchmarkRequires.logger = Logger.new nil
39
+
40
+ BenchmarkRequires.log_action = proc do |logger, message|
41
+ logger.info message
42
+ end
43
+
44
+ # super basic log action.
45
+ BenchmarkRequires.log_action = proc do |logger, message|
46
+ puts message
47
+ end
48
+
49
+ BenchmarkRequires.setup!
50
+
51
+ # etc, etc, etc
52
+ ```
53
+
54
+ ## TODOs
55
+
56
+ * More tests.
57
+ * Better support for custom loggers.
58
+ * More Documentation.
@@ -0,0 +1,9 @@
1
+ Bundler::GemHelper.install_tasks
2
+
3
+ require 'rspec/core'
4
+ require 'rspec/core/rake_task'
5
+
6
+ desc "Run all specs in spec directory"
7
+ RSpec::Core::RakeTask.new(:test)
8
+
9
+ task :default => [:test]
@@ -0,0 +1,51 @@
1
+ require "benchmark"
2
+ require "logger"
3
+ require "benchmark_requires/message_builder"
4
+ require "benchmark_requires/runner"
5
+
6
+ module BenchmarkRequires
7
+
8
+ class << self
9
+
10
+ attr_accessor :logger, :log_action
11
+ attr_accessor :setup, :runner
12
+
13
+ def benchmark file_name, &block
14
+ setup! unless setup?
15
+ self.runner.benchmark file_name, &block
16
+ end
17
+
18
+ def log message
19
+ self.log_action.call self.logger, message
20
+ end
21
+
22
+ def setup!
23
+ require "benchmark_requires/object_extension"
24
+
25
+ unless self.logger
26
+ logger = Logger.new(STDOUT)
27
+ logger.formatter = proc do |severity, datetime, progname, message|
28
+ "#{message}\n"
29
+ end
30
+ self.logger = logger
31
+ end
32
+
33
+ unless self.log_action
34
+ self.log_action = lambda do |logger, message|
35
+ logger.debug message
36
+ end
37
+ end
38
+
39
+ self.setup = true
40
+ end
41
+
42
+ def setup?
43
+ self.setup == true
44
+ end
45
+
46
+ end
47
+
48
+ self.setup = false
49
+ self.runner = Runner.new
50
+
51
+ end
@@ -0,0 +1,40 @@
1
+ module BenchmarkRequires
2
+
3
+ class MessageBuilder
4
+
5
+ def initialize file_name, time
6
+ @file_name = file_name
7
+ @time = time.round 5
8
+ end
9
+
10
+ def message level
11
+ "#{preamble(level)} -> #{colorize_file_name} took #{colorize_time} seconds."
12
+ end
13
+
14
+ private
15
+
16
+ def preamble level
17
+ (level - 1).times.map{|n| " "}.join("")
18
+ end
19
+
20
+ def colorize_file_name
21
+ "\e[1;37m#{@file_name}\e[0m"
22
+ end
23
+
24
+ def colorize_time
25
+ color = if @time <= 0.01 # green
26
+ "\e[0;32m"
27
+ elsif @time <= 0.25 # yellow
28
+ "\e[1;33m"
29
+ elsif @time <= 0.75 # red
30
+ "\e[1;31m"
31
+ else # OMFG RED!!!!
32
+ "\e[0;31m"
33
+ end
34
+
35
+ color + ("%f" % @time) + "\e[0m"
36
+ end
37
+
38
+ end
39
+
40
+ end
@@ -0,0 +1,20 @@
1
+ class Object
2
+
3
+ def load file_name, wrap=false
4
+ result = nil
5
+ BenchmarkRequires.benchmark(file_name) do
6
+ result = super(file_name, wrap)
7
+ end
8
+
9
+ return result
10
+ end
11
+
12
+ def require file_name
13
+ result = nil
14
+ BenchmarkRequires.benchmark(file_name) do
15
+ result = super(file_name)
16
+ end
17
+ return result
18
+ end
19
+
20
+ end
@@ -0,0 +1,49 @@
1
+ module BenchmarkRequires
2
+
3
+ class Runner
4
+
5
+ def initialize
6
+ @level = 0
7
+ @log_buffer = []
8
+ end
9
+
10
+ def benchmark(file_name, &block)
11
+ increment_level!
12
+ time = Benchmark.realtime do
13
+ yield
14
+ end
15
+
16
+ buffer_log MessageBuilder.new(file_name, time).message(@level)
17
+ decrement_level!
18
+ flush_log_buffer if top_level?
19
+ end
20
+
21
+ private
22
+
23
+ def increment_level!
24
+ @level += 1
25
+ end
26
+
27
+ def decrement_level!
28
+ @level -= 1
29
+ end
30
+
31
+ def top_level?
32
+ @level == 0
33
+ end
34
+
35
+ def buffer_log message
36
+ @log_buffer << message
37
+ end
38
+
39
+ def flush_log_buffer
40
+ @log_buffer.reverse.each{|msg| BenchmarkRequires.log(msg)}
41
+ BenchmarkRequires.log("")
42
+
43
+ @log_buffer = []
44
+ end
45
+
46
+ end
47
+
48
+
49
+ end
@@ -0,0 +1,17 @@
1
+ module BenchmarkRequires
2
+
3
+ VERSION = "1.0.0"
4
+
5
+ SUMMARY = "BenchmarkRequires is a simple gem that helps identify slow loading libraries."
6
+
7
+ DESCRIPTION = <<-EOF
8
+ #{SUMMARY}
9
+
10
+ As applications get older and more complex the start-up time tends to increase
11
+ quite dramatically. BenchmarkRequires helps by logging all requires/load and
12
+ load times.
13
+
14
+ Idea inspired by http://nationbuilder.com/blistering_rails_performance_part_1_boot_performance
15
+ EOF
16
+
17
+ end
@@ -0,0 +1,23 @@
1
+ require 'benchmark_requires'
2
+
3
+ describe BenchmarkRequires::MessageBuilder do
4
+
5
+ it "rounds times to 6 decimal places" do
6
+ time = 0.000001
7
+ builder = described_class.new('test.rb', time)
8
+ builder.message(1).should match /0\.000000/
9
+ end
10
+
11
+ it "includes file name" do
12
+ builder = described_class.new('test.rb', 1.00)
13
+ builder.message(1).should match /test\.rb/
14
+ end
15
+
16
+ it "indents based on level" do
17
+ builder = described_class.new('test.rb', 1.00)
18
+ builder.message(1).should match /^ \-\> /
19
+ builder.message(2).should match /^( \-\> )/
20
+ builder.message(3).should match /^( \-\> )/
21
+ end
22
+
23
+ end
@@ -0,0 +1,28 @@
1
+ require "benchmark_requires"
2
+
3
+ describe BenchmarkRequires::Runner do
4
+
5
+ before(:each) do
6
+ BenchmarkRequires.setup!
7
+ BenchmarkRequires.logger = Logger.new nil
8
+ end
9
+
10
+ it "flushes buffer log after benchmark" do
11
+ BenchmarkRequires.should_receive(:log).twice
12
+
13
+ runner = described_class.new
14
+ runner.benchmark('test.rb') do
15
+ # nothing.
16
+ end
17
+ end
18
+
19
+ it "returns to top level after benchmark is complete" do
20
+ runner = described_class.new
21
+ runner.benchmark('test.rb') do
22
+ # nothing.
23
+ end
24
+
25
+ runner.send(:top_level?).should be_true
26
+ end
27
+
28
+ end
@@ -0,0 +1,49 @@
1
+ require 'benchmark_requires'
2
+
3
+ describe BenchmarkRequires do
4
+
5
+ before(:each) do
6
+ described_class.logger = Logger.new(nil)
7
+ end
8
+
9
+ context "post-setup" do
10
+
11
+ before(:each) do
12
+ described_class.setup!
13
+ end
14
+
15
+ it "reports as being setup" do
16
+ described_class.setup?.should eql true
17
+ end
18
+
19
+ it "sets up sane defaults" do
20
+ logger = described_class.logger
21
+
22
+ logger.should be
23
+ logger.should respond_to :debug
24
+
25
+ log_action = described_class.log_action
26
+ log_action.should be_a Proc
27
+
28
+ logger.should_receive(:debug).with("test123")
29
+ log_action.call(logger, "test123")
30
+ end
31
+
32
+ describe "#log" do
33
+
34
+ it "uses logger and log_action" do
35
+ logger = described_class.logger
36
+ log_action = described_class.log_action
37
+ message = "test123"
38
+
39
+ logger.should_receive(:debug).with message
40
+
41
+ described_class.log message
42
+ end
43
+
44
+ end
45
+
46
+ end
47
+
48
+ end
49
+
metadata ADDED
@@ -0,0 +1,89 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: benchmark_requires
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Jon Daniel
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rake
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - '>='
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rspec
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description: "BenchmarkRequires is a simple gem that helps identify slow loading libraries.\n\nAs
42
+ applications get older and more complex the start-up time tends to increase \nquite
43
+ dramatically. BenchmarkRequires helps by logging all requires/load and \nload times.\n\nIdea
44
+ inspired by http://nationbuilder.com/blistering_rails_performance_part_1_boot_performance\n"
45
+ email: binarycleric@gmail.com
46
+ executables: []
47
+ extensions: []
48
+ extra_rdoc_files: []
49
+ files:
50
+ - lib/benchmark_requires/message_builder.rb
51
+ - lib/benchmark_requires/object_extension.rb
52
+ - lib/benchmark_requires/runner.rb
53
+ - lib/benchmark_requires/version.rb
54
+ - lib/benchmark_requires.rb
55
+ - LICENSE
56
+ - README.md
57
+ - spec/lib/benchmark_requires/message_builder_spec.rb
58
+ - spec/lib/benchmark_requires/runner_spec.rb
59
+ - spec/lib/benchmark_requires_spec.rb
60
+ - Rakefile
61
+ homepage: http://www.github.com/binarycleric/benchmark_requires
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - '>='
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubyforge_project:
81
+ rubygems_version: 2.0.5
82
+ signing_key:
83
+ specification_version: 4
84
+ summary: BenchmarkRequires is a simple gem that helps identify slow loading libraries.
85
+ test_files:
86
+ - spec/lib/benchmark_requires/message_builder_spec.rb
87
+ - spec/lib/benchmark_requires/runner_spec.rb
88
+ - spec/lib/benchmark_requires_spec.rb
89
+ - Rakefile