require-prof 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,22 @@
1
+ require-prof allows you to easily profile your Ruby project's imports:
2
+
3
+ ```shell
4
+ irb -r require-prof -r <path to your code>
5
+ ```
6
+
7
+ Then just run
8
+
9
+ ```ruby
10
+ RequireProf.print_timing_infos
11
+ ```
12
+
13
+ to see the time breakdown of your requires in chronological order. Run
14
+
15
+ ```ruby
16
+ RequireProf.print_timing_infos_for_optimization
17
+ ```
18
+
19
+ to see the time breakdown by amount of time spent.
20
+
21
+ If you set the environment variable RUBY_REQUIRE_PRINT_LIVE to 'true',
22
+ require-prof will print out import timing information as it happens.
@@ -0,0 +1,71 @@
1
+ module RequireProf
2
+ @@print_live = (print_live = ENV['RUBY_REQUIRE_PRINT_LIVE']) && (print_live != 'false')
3
+
4
+ @@orig_require = method(:require)
5
+ @@orig_load = method(:load)
6
+ @@global_start = Time.now
7
+ @@level = 0
8
+ @@lower_level_progress = 0
9
+ @@timing_info = []
10
+
11
+ def self.backend_requiring(orig_method, name, args)
12
+ initial_lower_level_progress = @@lower_level_progress
13
+ spacing = ' ' * @@level
14
+ start_at = Time.now
15
+ cumulative_duration = start_at - @@global_start
16
+
17
+ if @@print_live
18
+ $stderr.puts "#{spacing}[#{cumulative_duration}s] BEGIN #{name} #{args.inspect}..."
19
+ end
20
+
21
+ @@level += 1
22
+ orig_method.call(*args)
23
+ @@level -= 1
24
+
25
+ end_at = Time.now
26
+ cumulative_duration = end_at - @@global_start
27
+ total_duration = end_at - start_at
28
+ my_duration = total_duration - (@@lower_level_progress - initial_lower_level_progress)
29
+
30
+ if @@print_live
31
+ print_timing_entry(my_duration, total_duration, cumulative_duration, spacing, "END #{name}", args)
32
+ end
33
+
34
+ @@timing_info << [my_duration, total_duration, cumulative_duration, spacing, name, args]
35
+ @@lower_level_progress += my_duration
36
+ end
37
+
38
+ def self.require(*args)
39
+ backend_requiring(@@orig_require, 'requiring', args)
40
+ end
41
+
42
+ def self.load(*args)
43
+ backend_requiring(@@orig_load, 'loading', args)
44
+ end
45
+
46
+ def self.print_timing_infos_for_optimization
47
+ @@timing_info.sort_by { |timing| timing.first }.each do |my_duration, _, _, _, name, args|
48
+ $stderr.puts "#{my_duration} -- #{name} #{args.inspect}"
49
+ end
50
+ nil
51
+ end
52
+
53
+ def self.print_timing_infos
54
+ @@timing_info.each { |entry| print_timing_entry(*entry) }
55
+ nil
56
+ end
57
+
58
+ private
59
+
60
+ def self.print_timing_entry(my_duration, total_duration, cumulative_duration, spacing, name, args)
61
+ $stderr.puts "#{spacing}[#{cumulative_duration}s] #{name} #{args.inspect}. Took a cumulative #{total_duration}s (#{my_duration}s outside of sub-requires)."
62
+ end
63
+ end
64
+
65
+ def require(*args)
66
+ RequireProf.require(*args)
67
+ end
68
+
69
+ def load(*args)
70
+ RequireProf.load(*args)
71
+ end
@@ -0,0 +1,12 @@
1
+ Gem::Specification.new do |s|
2
+ s.name = 'require-prof'
3
+ s.version = '0.0.1'
4
+ s.authors = ["Greg Brockman"]
5
+ s.email = ["gdb@gregbrockman.com"]
6
+ s.homepage = 'https://github.com/gdb/require-prof'
7
+ s.summary = %q{Profile your project's requires}
8
+ s.description = %q{Use require-prof to see how long you spend importing various code}
9
+
10
+ s.files = `git ls-files`.split("\n")
11
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
12
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: require-prof
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease:
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Greg Brockman
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2012-01-30 00:00:00 Z
19
+ dependencies: []
20
+
21
+ description: Use require-prof to see how long you spend importing various code
22
+ email:
23
+ - gdb@gregbrockman.com
24
+ executables: []
25
+
26
+ extensions: []
27
+
28
+ extra_rdoc_files: []
29
+
30
+ files:
31
+ - README.markdown
32
+ - lib/require-prof.rb
33
+ - require-prof.gemspec
34
+ homepage: https://github.com/gdb/require-prof
35
+ licenses: []
36
+
37
+ post_install_message:
38
+ rdoc_options: []
39
+
40
+ require_paths:
41
+ - lib
42
+ required_ruby_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ hash: 3
48
+ segments:
49
+ - 0
50
+ version: "0"
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ hash: 3
57
+ segments:
58
+ - 0
59
+ version: "0"
60
+ requirements: []
61
+
62
+ rubyforge_project:
63
+ rubygems_version: 1.8.8
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Profile your project's requires
67
+ test_files: []
68
+