request_profiler 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in request_profiler.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,30 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ request_profiler (0.0.1)
5
+ ruby-prof
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ mocha (0.9.10)
11
+ rake
12
+ rack (1.2.1)
13
+ rack-test (0.5.6)
14
+ rack (>= 1.0)
15
+ rake (0.8.7)
16
+ ruby-prof (0.9.2)
17
+ sinatra (1.1.0)
18
+ rack (~> 1.1)
19
+ tilt (~> 1.1)
20
+ tilt (1.1)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ mocha
27
+ rack-test
28
+ request_profiler!
29
+ ruby-prof
30
+ sinatra
data/README.rdoc ADDED
@@ -0,0 +1,3 @@
1
+ = Request Profiler
2
+
3
+ - A Rack middleware wrapper for ruby-prof
data/Rakefile ADDED
@@ -0,0 +1,13 @@
1
+ require 'bundler'
2
+ require 'rake/testtask'
3
+ Bundler::GemHelper.install_tasks
4
+
5
+ task :default => :test
6
+ task :build => :test
7
+ task :release => :test
8
+
9
+ Rake::TestTask.new do |t|
10
+ t.libs << "test"
11
+ t.test_files = FileList['test/*_test.rb']
12
+ t.verbose = true
13
+ end
@@ -0,0 +1,69 @@
1
+ require 'ruby-prof'
2
+
3
+ module Rack
4
+ class RequestProfiler
5
+ def initialize(app, options = {})
6
+ @app = app
7
+ @printer = options[:printer] || ::RubyProf::GraphHtmlPrinter
8
+ @mode = options[:mode] || ::RubyProf::PROCESS_TIME
9
+ @eliminations = options[:eliminations]
10
+
11
+ @path = options[:path]
12
+ @path ||= Rails.root + 'tmp/performance' if defined?(Rails)
13
+ @path ||= ::File.join(ENV["TMPDIR"] + 'performance')
14
+ @path = Pathname(@path)
15
+ end
16
+
17
+ def call(env)
18
+ request = Rack::Request.new(env)
19
+ profile_request = request.params.delete("profile_request") == "true"
20
+
21
+ if profile_request
22
+ ::RubyProf.measure_mode = @mode
23
+ ::RubyProf.start
24
+ end
25
+ status, headers, body = @app.call(env)
26
+
27
+ if profile_request
28
+ result = ::RubyProf.stop
29
+ write_result(result, request)
30
+ end
31
+
32
+ [status, headers, body]
33
+ end
34
+
35
+ def format(printer)
36
+ case printer
37
+ when ::RubyProf::FlatPrinter
38
+ 'txt'
39
+ when ::RubyProf::FlatPrinterWithLineNumbers
40
+ 'txt'
41
+ when ::RubyProf::GraphPrinter
42
+ 'txt'
43
+ when ::RubyProf::GraphHtmlPrinter
44
+ 'html'
45
+ when ::RubyProf::DotPrinter
46
+ 'dot'
47
+ when ::RubyProf::CallTreePrinter
48
+ "out.#{Process.pid}"
49
+ when ::RubyProf::CallStackPrinter
50
+ 'html'
51
+ else
52
+ 'txt'
53
+ end
54
+ end
55
+
56
+ def write_result(result, request)
57
+ printer = @printer.new(result)
58
+ Dir.mkdir(@path) unless ::File.exists?(@path)
59
+ url = request.fullpath.gsub(/[?\/]/, '-')
60
+ filename = "#{Time.now.strftime('%Y-%m-%d-%H-%M-%S')}-#{url}.#{format(printer)}"
61
+ ::File.open(@path + filename, 'w+') do |f|
62
+ # HACK to keep this from crashing under patched 1.9.2
63
+ GC.disable
64
+ printer.print(f)
65
+ GC.enable
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,3 @@
1
+ module RequestProfiler
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1 @@
1
+ require 'rack/request_profiler'
@@ -0,0 +1,26 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "request_profiler/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "request_profiler"
7
+ s.version = RequestProfiler::VERSION
8
+ s.platform = Gem::Platform::RUBY
9
+ s.authors = ["Justin Weiss"]
10
+ s.email = ["justin@uberweiss.org"]
11
+ s.homepage = "https://github.com/justinweiss/request_profiler"
12
+ s.summary = %q{Profile Rack requests with ruby-prof}
13
+ s.description = %q{Request Profiler is a Rack middleware that allows optionally profiling requests with ruby-prof.}
14
+
15
+ s.rubyforge_project = "request_profiler"
16
+
17
+ s.files = `git ls-files`.split("\n")
18
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
+ s.require_paths = ["lib"]
21
+
22
+ s.add_dependency 'ruby-prof'
23
+ s.add_development_dependency 'rack-test'
24
+ s.add_development_dependency 'sinatra'
25
+ s.add_development_dependency 'mocha'
26
+ end
data/test/fake_app.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'sinatra'
2
+
3
+ class FakeApp < Sinatra::Base
4
+ get "/" do
5
+ "foo"
6
+ end
7
+ end
@@ -0,0 +1,44 @@
1
+ require 'test_helper'
2
+ require 'fake_app'
3
+ require 'mocha'
4
+
5
+ class RequestProfilerTest < Test::Unit::TestCase
6
+ include Rack::Test::Methods
7
+
8
+ attr_accessor :app
9
+
10
+ def teardown
11
+ @app = nil
12
+ end
13
+
14
+ def test_basic_request_isnt_profiled
15
+ self.app = Rack::RequestProfiler.new(FakeApp.new)
16
+ RubyProf.expects(:start).never
17
+ RubyProf.expects(:stop).never
18
+ get "/"
19
+ assert last_response.ok?
20
+
21
+ get "/?profile_request=false"
22
+ assert last_response.ok?
23
+ end
24
+
25
+ def test_basic_request_is_profiled
26
+ self.app = Rack::RequestProfiler.new(FakeApp.new)
27
+ RubyProf.expects(:start).once
28
+ RubyProf.expects(:stop).once
29
+ app.expects(:write_result).once
30
+ get "/?profile_request=true"
31
+ assert last_response.ok?
32
+ end
33
+
34
+ def test_file_format
35
+ self.app = Rack::RequestProfiler.new(FakeApp.new)
36
+ RubyProf.start
37
+ results = RubyProf.stop
38
+ printer = self.app.instance_variable_get(:@printer).new(results)
39
+
40
+ assert_equal ::RubyProf::GraphHtmlPrinter, printer.class
41
+ assert_equal 'html', self.app.format(printer)
42
+ end
43
+
44
+ end
@@ -0,0 +1,3 @@
1
+ require 'request_profiler'
2
+ require 'test/unit'
3
+ require 'rack/test'
metadata ADDED
@@ -0,0 +1,129 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: request_profiler
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: false
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1
9
+ version: 0.0.1
10
+ platform: ruby
11
+ authors:
12
+ - Justin Weiss
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2010-12-15 00:00:00 -08:00
18
+ default_executable:
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ name: ruby-prof
22
+ prerelease: false
23
+ requirement: &id001 !ruby/object:Gem::Requirement
24
+ none: false
25
+ requirements:
26
+ - - ">="
27
+ - !ruby/object:Gem::Version
28
+ segments:
29
+ - 0
30
+ version: "0"
31
+ type: :runtime
32
+ version_requirements: *id001
33
+ - !ruby/object:Gem::Dependency
34
+ name: rack-test
35
+ prerelease: false
36
+ requirement: &id002 !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ type: :development
45
+ version_requirements: *id002
46
+ - !ruby/object:Gem::Dependency
47
+ name: sinatra
48
+ prerelease: false
49
+ requirement: &id003 !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ segments:
55
+ - 0
56
+ version: "0"
57
+ type: :development
58
+ version_requirements: *id003
59
+ - !ruby/object:Gem::Dependency
60
+ name: mocha
61
+ prerelease: false
62
+ requirement: &id004 !ruby/object:Gem::Requirement
63
+ none: false
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ segments:
68
+ - 0
69
+ version: "0"
70
+ type: :development
71
+ version_requirements: *id004
72
+ description: Request Profiler is a Rack middleware that allows optionally profiling requests with ruby-prof.
73
+ email:
74
+ - justin@uberweiss.org
75
+ executables: []
76
+
77
+ extensions: []
78
+
79
+ extra_rdoc_files: []
80
+
81
+ files:
82
+ - .gitignore
83
+ - Gemfile
84
+ - Gemfile.lock
85
+ - README.rdoc
86
+ - Rakefile
87
+ - lib/rack/request_profiler.rb
88
+ - lib/request_profiler.rb
89
+ - lib/request_profiler/version.rb
90
+ - request_profiler.gemspec
91
+ - test/fake_app.rb
92
+ - test/request_profiler_test.rb
93
+ - test/test_helper.rb
94
+ has_rdoc: true
95
+ homepage: https://github.com/justinweiss/request_profiler
96
+ licenses: []
97
+
98
+ post_install_message:
99
+ rdoc_options: []
100
+
101
+ require_paths:
102
+ - lib
103
+ required_ruby_version: !ruby/object:Gem::Requirement
104
+ none: false
105
+ requirements:
106
+ - - ">="
107
+ - !ruby/object:Gem::Version
108
+ segments:
109
+ - 0
110
+ version: "0"
111
+ required_rubygems_version: !ruby/object:Gem::Requirement
112
+ none: false
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ segments:
117
+ - 0
118
+ version: "0"
119
+ requirements: []
120
+
121
+ rubyforge_project: request_profiler
122
+ rubygems_version: 1.3.7
123
+ signing_key:
124
+ specification_version: 3
125
+ summary: Profile Rack requests with ruby-prof
126
+ test_files:
127
+ - test/fake_app.rb
128
+ - test/request_profiler_test.rb
129
+ - test/test_helper.rb