devver-rack-contrib 0.9.3 → 0.9.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,119 @@
1
+ require 'perftools'
2
+ require 'rbconfig'
3
+
4
+ # You'll need graphviz to generate call graphs using dot:
5
+ #
6
+ # sudo port install graphviz # osx
7
+ # sudo apt-get install graphviz # debian/ubuntu
8
+
9
+ # You'll need ps2pdf to generate PDFs (comes with ghostscript)
10
+ # sudo port install ghostscript
11
+
12
+ module Rack
13
+
14
+ # Pass the :printer option to pick a different result format.
15
+ class PerftoolsProfiler
16
+ MODES = %w(
17
+ process_time
18
+ #wall_time
19
+ )
20
+
21
+ DEFAULT_PRINTER = :text
22
+ #DEFAULT_CONTENT_TYPE = 'application/octet-stream'
23
+
24
+ PRINTER_CONTENT_TYPE = {
25
+ :text => 'text/plain',
26
+ :gif => 'image/gif',
27
+ :pdf => 'application/pdf'
28
+ }
29
+
30
+ # Accepts a :printer => [:call_tree|:graph_html|:graph|:flat] option
31
+ # defaulting to :call_tree.
32
+ def initialize(app, options = {})
33
+ @app = app
34
+ @printer = parse_printer(options[:printer])
35
+ @times = (options[:times] || 1).to_i
36
+ end
37
+
38
+ def call(env)
39
+ if mode = profiling?(env)
40
+ profile(env, mode)
41
+ else
42
+ @app.call(env)
43
+ end
44
+ end
45
+
46
+ private
47
+ def profiling?(env)
48
+ unless PerfTools::CpuProfiler.running?
49
+ request = Rack::Request.new(env)
50
+ if mode = request.params.delete('profile')
51
+ true
52
+ #if RubyProf.const_defined?(mode.upcase)
53
+ # mode
54
+ #else
55
+ #env['rack.errors'].write "Invalid RubyProf measure_mode: " +
56
+ # "#{mode}. Use one of #{MODES.to_a.join(', ')}"
57
+ # false
58
+ #end
59
+ end
60
+ end
61
+ end
62
+
63
+ def profile(env, mode)
64
+ #RubyProf.measure_mode = RubyProf.const_get(mode.upcase)
65
+
66
+ #GC.enable_stats if GC.respond_to?(:enable_stats)
67
+ #result = RubyProf.profile do
68
+ temp_file = "/tmp/perftools_file"
69
+ PerfTools::CpuProfiler.start(temp_file) do
70
+ @times.times { @app.call(env) }
71
+ end
72
+ #end
73
+ #GC.disable_stats if GC.respond_to?(:disable_stats)
74
+
75
+ [200, headers(@printer, env, mode), print(@printer, temp_file)]
76
+ end
77
+
78
+ def print(printer, temp_file)
79
+ cmd = "pprof.rb --#{printer} #{temp_file}"
80
+ `#{cmd}`
81
+ #body = StringIO.new
82
+ #printer.new(result).print(body, :min_percent => 0.01)
83
+ #body.rewind
84
+ #body
85
+ end
86
+
87
+ def headers(printer, env, mode)
88
+ headers = { 'Content-Type' => PRINTER_CONTENT_TYPE[printer] || DEFAULT_CONTENT_TYPE }
89
+ if printer==:pdf
90
+ filetype = printer
91
+ filename='profile'
92
+ headers['Content-Disposition'] = %(attachment; filename="#{filename}.#{mode}.#{filetype}")
93
+ end
94
+ headers
95
+ end
96
+
97
+ def parse_printer(printer)
98
+ if printer.nil?
99
+ DEFAULT_PRINTER
100
+ else
101
+ printer.to_sym
102
+ end
103
+ #elsif printer.is_a?(Class)
104
+ # printer
105
+ #else
106
+ # name = "#{camel_case(printer)}Printer"
107
+ # if RubyProf.const_defined?(name)
108
+ # RubyProf.const_get(name)
109
+ # else
110
+ # DEFAULT_PRINTER
111
+ # end
112
+ #end
113
+ end
114
+
115
+ def camel_case(word)
116
+ word.to_s.gsub(/(?:^|_)(.)/) { $1.upcase }
117
+ end
118
+ end
119
+ end
@@ -3,7 +3,7 @@ Gem::Specification.new do |s|
3
3
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
4
4
 
5
5
  s.name = 'devver-rack-contrib'
6
- s.version = '0.9.3'
6
+ s.version = '0.9.4'
7
7
  s.date = '2010-01-10'
8
8
 
9
9
  s.description = "The Devver fork of rack-contrib"
@@ -35,6 +35,7 @@ Gem::Specification.new do |s|
35
35
  lib/rack/contrib/mailexceptions.rb
36
36
  lib/rack/contrib/nested_params.rb
37
37
  lib/rack/contrib/not_found.rb
38
+ lib/rack/contrib/perftools_profiler.rb
38
39
  lib/rack/contrib/post_body_content_type_parser.rb
39
40
  lib/rack/contrib/proctitle.rb
40
41
  lib/rack/contrib/profiler.rb
metadata CHANGED
@@ -1,7 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: devver-rack-contrib
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.3
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 9
8
+ - 4
9
+ version: 0.9.4
5
10
  platform: ruby
6
11
  authors:
7
12
  - rack-devel
@@ -14,44 +19,58 @@ default_executable:
14
19
  dependencies:
15
20
  - !ruby/object:Gem::Dependency
16
21
  name: rack
17
- type: :runtime
18
- version_requirement:
19
- version_requirements: !ruby/object:Gem::Requirement
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
20
24
  requirements:
21
25
  - - ">="
22
26
  - !ruby/object:Gem::Version
27
+ segments:
28
+ - 0
29
+ - 9
30
+ - 1
23
31
  version: 0.9.1
24
- version:
32
+ type: :runtime
33
+ version_requirements: *id001
25
34
  - !ruby/object:Gem::Dependency
26
35
  name: test-spec
27
- type: :development
28
- version_requirement:
29
- version_requirements: !ruby/object:Gem::Requirement
36
+ prerelease: false
37
+ requirement: &id002 !ruby/object:Gem::Requirement
30
38
  requirements:
31
39
  - - ~>
32
40
  - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ - 9
44
+ - 0
33
45
  version: 0.9.0
34
- version:
46
+ type: :development
47
+ version_requirements: *id002
35
48
  - !ruby/object:Gem::Dependency
36
49
  name: tmail
37
- type: :development
38
- version_requirement:
39
- version_requirements: !ruby/object:Gem::Requirement
50
+ prerelease: false
51
+ requirement: &id003 !ruby/object:Gem::Requirement
40
52
  requirements:
41
53
  - - ">="
42
54
  - !ruby/object:Gem::Version
55
+ segments:
56
+ - 1
57
+ - 2
43
58
  version: "1.2"
44
- version:
59
+ type: :development
60
+ version_requirements: *id003
45
61
  - !ruby/object:Gem::Dependency
46
62
  name: json
47
- type: :development
48
- version_requirement:
49
- version_requirements: !ruby/object:Gem::Requirement
63
+ prerelease: false
64
+ requirement: &id004 !ruby/object:Gem::Requirement
50
65
  requirements:
51
66
  - - ">="
52
67
  - !ruby/object:Gem::Version
68
+ segments:
69
+ - 1
70
+ - 1
53
71
  version: "1.1"
54
- version:
72
+ type: :development
73
+ version_requirements: *id004
55
74
  description: The Devver fork of rack-contrib
56
75
  email: rack-devel@googlegroups.com
57
76
  executables: []
@@ -83,6 +102,7 @@ files:
83
102
  - lib/rack/contrib/mailexceptions.rb
84
103
  - lib/rack/contrib/nested_params.rb
85
104
  - lib/rack/contrib/not_found.rb
105
+ - lib/rack/contrib/perftools_profiler.rb
86
106
  - lib/rack/contrib/post_body_content_type_parser.rb
87
107
  - lib/rack/contrib/proctitle.rb
88
108
  - lib/rack/contrib/profiler.rb
@@ -135,18 +155,20 @@ required_ruby_version: !ruby/object:Gem::Requirement
135
155
  requirements:
136
156
  - - ">="
137
157
  - !ruby/object:Gem::Version
158
+ segments:
159
+ - 0
138
160
  version: "0"
139
- version:
140
161
  required_rubygems_version: !ruby/object:Gem::Requirement
141
162
  requirements:
142
163
  - - ">="
143
164
  - !ruby/object:Gem::Version
165
+ segments:
166
+ - 0
144
167
  version: "0"
145
- version:
146
168
  requirements: []
147
169
 
148
170
  rubyforge_project:
149
- rubygems_version: 1.3.5
171
+ rubygems_version: 1.3.6
150
172
  signing_key:
151
173
  specification_version: 2
152
174
  summary: This gem is the Devver fork of rack-contrib (http://github.com/rack/rack-contrib) so we can play around with new features we're building. You should not use this version and use the official rack-contrib gem instead.