ru-slow 0.0.0 → 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 56c11067f91a74d1954f0efc72aaee33c7f3f1ac
4
- data.tar.gz: 426b9e52cefd4d3f2ab04aa496db7d1aa75a96a6
3
+ metadata.gz: f1962a2d99ee07c1e7a6ecce47f8323d26adc867
4
+ data.tar.gz: 67c54554493d1926262bc584985f22cabfc23405
5
5
  SHA512:
6
- metadata.gz: 976b29e55fa7f6c34ca7c1ce7ac93e405b713bcb1e3ada502950c30fb92a11f7f2be13ccfea873d4502453510067acf51f6e521617d5c5172e120fc4f4421403
7
- data.tar.gz: b86e3c923096ef39178ba4b063530679e299f3ad1f237dcb8a0f94b267d1de5e2e78da819564d239a3261db4f6d5fe5151207d354a1e336d298d4acad8e7cebc
6
+ metadata.gz: e4ea3b0f80494d451105b4b39a042891d68dc5d51156dcd146195e790a5a2be899b9b6e7d487d1846bc8b16a62bc9daf931c4b5f9fe7b2b4799f7bea9182b741
7
+ data.tar.gz: 03af3edefd7b79039cc8d4588f65ecedd582a0a41680e1ed6cecc4f0bf0b67a37443f3b23e62004e6e298ff4091076140823ac1f91bfcc09fe1dfdd5900f790f
data/README.md CHANGED
@@ -17,14 +17,6 @@ Add this line to your application's Gemfile:
17
17
  gem 'ru-slow'
18
18
  ```
19
19
 
20
- And then execute:
21
-
22
- $ bundle
23
-
24
- Or install it yourself as:
25
-
26
- $ gem install ru-slow
27
-
28
20
 
29
21
  Usage
30
22
  -----
@@ -34,6 +26,9 @@ Require the file at the very top of your application:
34
26
  require 'ru-slow'
35
27
  ```
36
28
 
29
+ You **must** have a `Gemfile` in the root of your project in order for
30
+ ru-slow to find the correct project directory.
31
+
37
32
 
38
33
  Contributing
39
34
  ------------
@@ -42,3 +37,24 @@ Contributing
42
37
  3. Commit your changes (`git commit -am 'Add some feature'`)
43
38
  4. Push to the branch (`git push origin my-new-feature`)
44
39
  5. Create new Pull Request
40
+
41
+
42
+ License & Authors
43
+ -----------------
44
+ - Seth Vargo (sethvargo@gmail.com)
45
+
46
+ ```text
47
+ Copyright 2013, Seth Vargo <sethvargo@gmail.com>
48
+
49
+ Licensed under the Apache License, Version 2.0 (the "License");
50
+ you may not use this file except in compliance with the License.
51
+ You may obtain a copy of the License at
52
+
53
+ http://www.apache.org/licenses/LICENSE-2.0
54
+
55
+ Unless required by applicable law or agreed to in writing, software
56
+ distributed under the License is distributed on an "AS IS" BASIS,
57
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
58
+ See the License for the specific language governing permissions and
59
+ limitations under the License.
60
+ ```
data/lib/ru-slow.rb CHANGED
@@ -1,4 +1,78 @@
1
- require 'ru-slow/version'
1
+ require 'benchmark'
2
+ require 'pathname'
3
+
4
+ require 'ru-slow/kernel'
2
5
 
3
6
  module RuSlow
7
+ extend self
8
+
9
+ attr_accessor :root
10
+
11
+ #
12
+ # Add a new call to the profiler.
13
+ #
14
+ # @param [String] name
15
+ # the name of the thing to benchmark (like a label)
16
+ # @param [Benchmark] result
17
+ # the result of the benchmark
18
+ #
19
+ # @return [Boolean]
20
+ # true if the benchmark was added, false if the benchmark already
21
+ # exists
22
+ #
23
+ def add(name, result)
24
+ if profiler.has_key?(name)
25
+ false
26
+ else
27
+ line = caller[1].split(' ', 2).first[0..-4]
28
+
29
+ if line.include?(root)
30
+ profiler[name] = result
31
+ true
32
+ else
33
+ false
34
+ end
35
+ end
36
+ end
37
+
38
+ #
39
+ # Generate the report.
40
+ #
41
+ def report!
42
+ by_time = profiler.sort_by { |_, t| -t.total }
43
+ padding = by_time.map(&:first).group_by(&:size).max.last.first.length
44
+
45
+ puts "\n"*3
46
+ puts "Label".ljust(padding) + ' Real'
47
+ puts '-'*padding + '---------'
48
+
49
+ by_time.each do |label, benchmark|
50
+ puts label.ljust(padding) + ' ' + benchmark.real.to_s
51
+ end
52
+ rescue => e
53
+ Kernel.warn "There was an error running the report: #{e.message}"
54
+ end
55
+
56
+ private
57
+
58
+ # @return [Hash]
59
+ def profiler
60
+ @profiler ||= {}
61
+ end
62
+ end
63
+
64
+ #
65
+ # Calculate the calling lib path when ru-slow is required.
66
+ #
67
+ kaller = Pathname.new(caller.last.split(':', 2).first).expand_path
68
+ root = nil
69
+ kaller.ascend do |dir|
70
+ path = dir.join('Gemfile')
71
+
72
+ if path.exist?
73
+ root = dir.to_s
74
+ break
75
+ end
4
76
  end
77
+
78
+ RuSlow.root = root
@@ -0,0 +1,33 @@
1
+ module Kernel
2
+ alias_method :old_require, :require
3
+ def require(name)
4
+ # Don't benchmark requires outside of this project
5
+ return old_require(name) if name.start_with?('/')
6
+
7
+ # Store a local variable for the result of the benchmark, so the original
8
+ # method can return true/false as it would have.
9
+ old_result = false
10
+
11
+ # Profile the result
12
+ RuSlow.add("require '#{name}'", Benchmark.measure { old_result = old_require(name) })
13
+
14
+ # Return the original result
15
+ return old_result
16
+ end
17
+
18
+ alias_method :old_load, :load
19
+ def load(name, wrap = false)
20
+ # Don't benchmark requires outside of this project
21
+ return old_load(name) if name.start_with?('/')
22
+
23
+ # Profile the result
24
+ RuSlow.add("load '#{name}'", Benchmark.measure { old_load(name, wrap) })
25
+
26
+ # ALways return true
27
+ return true
28
+ end
29
+ end
30
+
31
+ Kernel.at_exit do
32
+ RuSlow.report!
33
+ end
@@ -1,3 +1,3 @@
1
1
  module RuSlow
2
- VERSION = '0.0.0'
2
+ VERSION = '1.0.0'
3
3
  end
data/ru-slow.gemspec CHANGED
@@ -19,6 +19,7 @@ Gem::Specification.new do |spec|
19
19
  spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
20
20
  spec.require_paths = ['lib']
21
21
 
22
+ # Development dependencies
22
23
  spec.add_development_dependency 'bundler', '~> 1.3'
23
24
  spec.add_development_dependency 'rake', '~> 10.1'
24
25
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ru-slow
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Seth Vargo
@@ -52,6 +52,7 @@ files:
52
52
  - README.md
53
53
  - Rakefile
54
54
  - lib/ru-slow.rb
55
+ - lib/ru-slow/kernel.rb
55
56
  - lib/ru-slow/version.rb
56
57
  - ru-slow.gemspec
57
58
  homepage: https://github.com/sethvargo/ru-slow