profiling 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c1c0b6ad469919343bcbb11bdb711f0096050ad2
4
- data.tar.gz: 5b61bf891faa8172ea003dbdd8a77d9c47409c8c
3
+ metadata.gz: f20b2a353b190c5e2dd6dcdc5c9bd790ff1cb6b7
4
+ data.tar.gz: 947bd73fe763e35f8f90c57f164f6716e0bb2a8d
5
5
  SHA512:
6
- metadata.gz: df3dabf3d0884f20f400960d20cc3740499b67cb421bf249323c10befd11a5c519a63dd027787194c7ae127cea75490bd1657ad99ae7648f39e30a29f83c48a2
7
- data.tar.gz: ec771cde630858097e2031f7d0bdba8b0eedf0324c874c36a131e3516c5e24b604803bc6e2a740bc97705427f48872dfacfec729baf8ad6fc98fbf7f9b172d99
6
+ metadata.gz: 1c687fe988cf9fc2a79a365e705aaaa7580fef6d1e4e8a7ef80f86b8659dc1f865dae48cae69522861406de6dee2ad5148cf2cb3e108340151c9594f3ec5617a
7
+ data.tar.gz: 36f8235155340ed56731adaff17fc70ee415c4a91c6b1bd76f52f8c1cb5d44a5d0ec233a0a5e6d4bed4cc76a7561bc1096e0727291e71d02803abdb67ee0c7b2
data/README.md CHANGED
@@ -28,18 +28,20 @@ Or install it yourself as:
28
28
  Profile slow code from your friend or colleague like this:
29
29
 
30
30
  ```ruby
31
- Profiling.run do
31
+ Profiler.run do
32
32
  # Slow code here...
33
33
  end
34
34
 
35
35
  # or
36
36
 
37
- Profiling.run("some-label") do
37
+ Profiler.run("some-label") do
38
38
  # Slow code here...
39
39
  end
40
40
  ```
41
41
 
42
- The next time you call the code it will be profiled and three files will be written into a directory `profiling`, and in the second example `profiling/some-label`:
42
+ The next time you call the code it will be profiled and three files will be written into a directory `profiling`, and in the second example `profiling/some-label`.
43
+
44
+ ### Files Generated
43
45
 
44
46
  | File | Description |
45
47
  | ------------- | ------------- |
@@ -52,45 +54,52 @@ The next time you call the code it will be profiled and three files will be writ
52
54
  Change the directory the files will be generated in:
53
55
 
54
56
  ```ruby
55
- Profiling.config = {
57
+ Profiler.config = {
56
58
  dir: '/tmp/my-dir'
57
59
  }
58
60
  ```
59
61
 
62
+ ### Rails Initializer
63
+
64
+ This initializer is recommended if you're planning to profile in Rails:
65
+
66
+ ```ruby
67
+ # config/initializer/profiling.rb
68
+ Profiler.config = {
69
+ dir: Rails.root.join('tmp/profiling')
70
+ }
71
+ ```
72
+
60
73
  ## Conditional Profiling
61
74
 
62
75
  Pass an argument `if:` to enable or disable profiling:
63
76
 
64
77
  ```ruby
65
- Profiling.run(if: user.is_admin?) do
78
+ Profiler.run(if: user.is_admin?) do
66
79
  # Slow code here...
67
80
  end
68
81
  ```
69
82
 
70
- ##
71
83
  ## Preserving artefacts
72
84
 
73
85
  Every time code is profiled the previous files will be overwritten unless the label's dynamic. To keep old files, you could add the current time in the label so new files are generated with each run:
74
86
 
75
87
  ```ruby
76
- # Files will be in `profiling/my-label-1524132842`
77
- Profiling.run("my-label-#{Time.now.to_i}") do
88
+ Profiler.run("my-label-#{Time.now.to_i}") do
78
89
  # Slow code here...
79
90
  end
80
91
  ```
81
92
 
82
- ## Organizing artefacts
93
+ ### Organizing artefacts
83
94
 
84
- Labels translate to directories, so use `/` in your labels if you want to group profiling together logically:
95
+ Labels translate to directories, so use `/` in your labels to group profiling together logically:
85
96
 
86
97
  ```ruby
87
- # Files will be in `profiling/post/create`
88
- Profiling.run("post/create") do
98
+ Profiler.run("post/create") do
89
99
  # Slow code here...
90
100
  end
91
101
 
92
- # Files will be in `profiling/post/update`
93
- Profiling.run("post/update") do
102
+ Profiler.run("post/update") do
94
103
  # Slow code here...
95
104
  end
96
105
  ```
@@ -1,4 +1,4 @@
1
- class Profiling
1
+ class Profiler
2
2
  module Configuration
3
3
 
4
4
  DEFAULT_CONFIG = {
@@ -0,0 +1,44 @@
1
+ class Profiler
2
+ module Engine
3
+
4
+ def run(label=nil, options={})
5
+ enabled = options[:if].nil? ? true : !!options[:if]
6
+ return yield unless enabled
7
+
8
+ # Create directory
9
+ @dir = File.join(config[:dir], label.to_s)
10
+ FileUtils.mkdir_p(@dir) unless File.exist?(@dir)
11
+
12
+ require 'ruby-prof'
13
+
14
+ RubyProf.start
15
+
16
+ begin
17
+ yield
18
+ rescue => e
19
+ RubyProf.stop
20
+ raise e
21
+ end
22
+
23
+ @results = RubyProf.stop
24
+ out()
25
+ end
26
+
27
+ private
28
+
29
+ def out
30
+ File.open(File.join(@dir, "graph.html"), 'w') do |file|
31
+ RubyProf::GraphHtmlPrinter.new(@results).print(file)
32
+ end
33
+
34
+ File.open(File.join(@dir, "flat.txt"), 'w') do |file|
35
+ RubyProf::FlatPrinterWithLineNumbers.new(@results).print(file)
36
+ end
37
+
38
+ File.open(File.join(@dir, "stack.html"), 'w') do |file|
39
+ RubyProf::CallStackPrinter.new(@results).print(file)
40
+ end
41
+ end
42
+
43
+ end
44
+ end
@@ -1,3 +1,3 @@
1
- class Profiling
2
- VERSION = "1.1.0"
1
+ class Profiler
2
+ VERSION = "1.2.0"
3
3
  end
data/lib/profiling.rb CHANGED
@@ -3,42 +3,7 @@ require 'ostruct'
3
3
 
4
4
  Dir[File.dirname(__FILE__) + '/profiling/*.rb'].each { |file| require file }
5
5
 
6
- class Profiling
6
+ class Profiler
7
7
  extend Configuration
8
-
9
- def self.run(label='', options={})
10
- enabled = options[:if].nil? ? true : !!options[:if]
11
-
12
- return yield unless enabled
13
-
14
- # Create directory
15
- subdir = File.join(config[:dir], label)
16
- FileUtils.mkdir_p subdir unless File.exist? subdir
17
-
18
- require 'ruby-prof'
19
-
20
- RubyProf.start
21
-
22
- begin
23
- yield
24
- rescue => e
25
- RubyProf.stop
26
- raise e
27
- end
28
-
29
- results = RubyProf.stop
30
-
31
- File.open(File.join(subdir, "graph.html"), 'w') do |file|
32
- RubyProf::GraphHtmlPrinter.new(results).print(file)
33
- end
34
-
35
- File.open(File.join(subdir, "flat.txt"), 'w') do |file|
36
- RubyProf::FlatPrinterWithLineNumbers.new(results).print(file)
37
- end
38
-
39
- File.open(File.join(subdir, "stack.html"), 'w') do |file|
40
- RubyProf::CallStackPrinter.new(results).print(file)
41
- end
42
- end
43
-
8
+ extend Engine
44
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: profiling
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Luke Duncalfe
@@ -122,6 +122,7 @@ files:
122
122
  - bin/setup
123
123
  - lib/profiling.rb
124
124
  - lib/profiling/configuration.rb
125
+ - lib/profiling/engine.rb
125
126
  - lib/profiling/version.rb
126
127
  homepage: https://github.com/lukes/profiling
127
128
  licenses:
@@ -136,7 +137,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
136
137
  requirements:
137
138
  - - ">="
138
139
  - !ruby/object:Gem::Version
139
- version: '0'
140
+ version: 1.9.3
140
141
  required_rubygems_version: !ruby/object:Gem::Requirement
141
142
  requirements:
142
143
  - - ">="
@@ -144,7 +145,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
144
145
  version: '0'
145
146
  requirements: []
146
147
  rubyforge_project:
147
- rubygems_version: 2.2.2
148
+ rubygems_version: 2.4.5.1
148
149
  signing_key:
149
150
  specification_version: 4
150
151
  summary: 'Non-discriminatory Ruby code profiling: This gem is a small wrapper around