rack_prof 0.1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ee20fd9c916fa05afc3ae08d73a5529a218c85ae
4
+ data.tar.gz: 3e91d9dcf5db6dd2702087197d668c612287ca24
5
+ SHA512:
6
+ metadata.gz: 7d5e52dc8447324b2164185a0aa45ff348c74da8659057553b0149bbc536052642cb51c9c74921c227cdf52d323745e7575482c1be66dd0e8683ac9ae622c5f2
7
+ data.tar.gz: 2bc33e4d2b811f13bee3ad6015c617d4a607e394bab54459d277e69861817ed8161480f8938528688de88dc63557d939df1b7436fb1eb7abbc2bc8c6e7b1b0e9
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rack_prof.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Kazuyuki Kohno
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,37 @@
1
+ # RackProf
2
+
3
+ Rack middleware for profiling.
4
+
5
+ ![screencast](https://raw.github.com/jugyo/rack_prof/master/screencast.gif)
6
+
7
+ ## Installation
8
+
9
+ gem 'rack_prof'
10
+
11
+ or
12
+
13
+ $ gem install rack_prof
14
+
15
+ ## Usage
16
+
17
+ Modify `config.ru` like:
18
+
19
+ use RackProf
20
+
21
+ require ::File.expand_path('../config/environment', __FILE__)
22
+ run YourApp::Application
23
+
24
+ You might need below to avoid 'stack level too deep' error:
25
+
26
+ RubyVM::InstructionSequence.compile_option = {
27
+ :tailcall_optimization => true,
28
+ :trace_instruction => false
29
+ }
30
+
31
+ ## Contributing
32
+
33
+ 1. Fork it
34
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
35
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
36
+ 4. Push to the branch (`git push origin my-new-feature`)
37
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,3 @@
1
+ class RackProf
2
+ VERSION = "0.1.0"
3
+ end
data/lib/rack_prof.rb ADDED
@@ -0,0 +1,73 @@
1
+ require "rack_prof/version"
2
+ require 'ruby-prof'
3
+ require 'launchy'
4
+
5
+ class RackProf
6
+ DEFAULT_PRINTER = RubyProf::CallStackPrinter
7
+
8
+ PRINTERS = {
9
+ RubyProf::FlatPrinter => 'flat.txt',
10
+ RubyProf::GraphPrinter => 'graph.txt',
11
+ RubyProf::GraphHtmlPrinter => 'graph.html',
12
+ RubyProf::CallStackPrinter => 'call_stack.html',
13
+ RubyProf::CallTreePrinter => 'call_tree.tree'
14
+ }
15
+
16
+ def initialize(app, options = {})
17
+ @app = app
18
+ end
19
+
20
+ def call(env)
21
+ request = Rack::Request.new(env)
22
+ if request.params.delete('profile')
23
+ profile(env, request)
24
+ else
25
+ @app.call(env)
26
+ end
27
+ end
28
+
29
+ private
30
+
31
+ def profile(env, request)
32
+ response = nil
33
+ result = RubyProf.profile { response = @app.call(env) }
34
+ printer = parse_printer(request.params.delete('printer'))
35
+ file_name = get_file_name(request, printer)
36
+ print(result, printer, file_name)
37
+ Launchy.open(file_name)
38
+ response
39
+ end
40
+
41
+ def print(result, printer, file_name)
42
+ File.open(file_name, 'wb') do |file|
43
+ printer.new(result).print(file, :min_percent => 1)
44
+ end
45
+ file_name
46
+ end
47
+
48
+ def get_file_name(request, printer)
49
+ path = request.path.gsub('/', '-')
50
+ path.slice!(0)
51
+ base_name = PRINTERS[printer]
52
+ File.join(Dir.tmpdir, "#{path}-#{base_name}")
53
+ end
54
+
55
+ def parse_printer(printer)
56
+ if printer.nil?
57
+ DEFAULT_PRINTER
58
+ elsif printer.is_a?(Class)
59
+ printer
60
+ else
61
+ name = "#{camel_case(printer)}Printer"
62
+ if RubyProf.const_defined?(name)
63
+ RubyProf.const_get(name)
64
+ else
65
+ DEFAULT_PRINTER
66
+ end
67
+ end
68
+ end
69
+
70
+ def camel_case(word)
71
+ word.to_s.gsub(/(?:^|_)(.)/) { $1.upcase }
72
+ end
73
+ end
data/rack_prof.gemspec ADDED
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack_prof/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack_prof"
8
+ spec.version = RackProf::VERSION
9
+ spec.authors = ["Kazuyuki Kohno"]
10
+ spec.email = ["jugyo.org@gmail.com"]
11
+ spec.description = %q{Rack middleware for profiling}
12
+ spec.summary = %q{Rack middleware for profiling}
13
+ spec.homepage = "https://githug.com/jugyo/rack_prof"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/) - ['screencast.gif']
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "rack"
22
+ spec.add_dependency "ruby-prof"
23
+ spec.add_dependency "launchy"
24
+ spec.add_development_dependency "bundler", "~> 1.3"
25
+ spec.add_development_dependency "rake"
26
+ end
metadata ADDED
@@ -0,0 +1,121 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack_prof
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Kazuyuki Kohno
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-04-17 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: rack
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - '>='
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
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: ruby-prof
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: launchy
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Rack middleware for profiling
84
+ email:
85
+ - jugyo.org@gmail.com
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - Gemfile
91
+ - LICENSE.txt
92
+ - README.md
93
+ - Rakefile
94
+ - lib/rack_prof.rb
95
+ - lib/rack_prof/version.rb
96
+ - rack_prof.gemspec
97
+ homepage: https://githug.com/jugyo/rack_prof
98
+ licenses:
99
+ - MIT
100
+ metadata: {}
101
+ post_install_message:
102
+ rdoc_options: []
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ requirements:
107
+ - - '>='
108
+ - !ruby/object:Gem::Version
109
+ version: '0'
110
+ required_rubygems_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - '>='
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ requirements: []
116
+ rubyforge_project:
117
+ rubygems_version: 2.0.3
118
+ signing_key:
119
+ specification_version: 4
120
+ summary: Rack middleware for profiling
121
+ test_files: []