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 +4 -4
- data/README.md +23 -14
- data/lib/profiling/configuration.rb +1 -1
- data/lib/profiling/engine.rb +44 -0
- data/lib/profiling/version.rb +2 -2
- data/lib/profiling.rb +2 -37
- metadata +4 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f20b2a353b190c5e2dd6dcdc5c9bd790ff1cb6b7
|
4
|
+
data.tar.gz: 947bd73fe763e35f8f90c57f164f6716e0bb2a8d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
-
|
31
|
+
Profiler.run do
|
32
32
|
# Slow code here...
|
33
33
|
end
|
34
34
|
|
35
35
|
# or
|
36
36
|
|
37
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
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
|
-
|
93
|
+
### Organizing artefacts
|
83
94
|
|
84
|
-
Labels translate to directories, so use `/` in your labels
|
95
|
+
Labels translate to directories, so use `/` in your labels to group profiling together logically:
|
85
96
|
|
86
97
|
```ruby
|
87
|
-
|
88
|
-
Profiling.run("post/create") do
|
98
|
+
Profiler.run("post/create") do
|
89
99
|
# Slow code here...
|
90
100
|
end
|
91
101
|
|
92
|
-
|
93
|
-
Profiling.run("post/update") do
|
102
|
+
Profiler.run("post/update") do
|
94
103
|
# Slow code here...
|
95
104
|
end
|
96
105
|
```
|
@@ -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
|
data/lib/profiling/version.rb
CHANGED
@@ -1,3 +1,3 @@
|
|
1
|
-
class
|
2
|
-
VERSION = "1.
|
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
|
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.
|
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:
|
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.
|
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
|