rack_mem_prof 0.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: a18a36ef5f7dd70c156fd5b1fd75597cd9b3069032e5f5522c13a018197d9e32
4
+ data.tar.gz: 8c39106036de7812b16b9934fafc5678153b758d70a9418ff85e7804888f0c75
5
+ SHA512:
6
+ metadata.gz: 64d47fee0f12c47c88ab71740e6708d2738da0612e6595e227ae572f4827c50477efd35cd718d602d65d180655f3b3eac404d51d4aa6010527aedfcb5545a8ef
7
+ data.tar.gz: a6d75f76fd72cd2689b2b57a62c1c0abb3797db71034d646b413f5bd2f530076524265bf3a6005c22f4f9523344065687a1f6847ed2d7d593dce8aa3b4f70954
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ *.gem
2
+ Gemfile.lock
3
+ .ruby-version
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "http://rubygems.org"
2
+
3
+ gemspec
data/README.md ADDED
@@ -0,0 +1,28 @@
1
+ # About
2
+ Use this middleware to profile API requests.
3
+
4
+ !!! Remember about huge overhead that enabling ObjectSpace stats gives to your application. !!!
5
+
6
+ # Installation
7
+ Add in Gemfile:
8
+
9
+ `gem 'rack_mem_prof'`
10
+
11
+ Install dependencies:
12
+
13
+ `$ bundle install`
14
+
15
+ Install middleware:
16
+
17
+ `config.middleware.use(Rack::MemProf::Middleware)`
18
+ or
19
+ ```
20
+ require 'rack/mem_prof'
21
+
22
+ use Rack::MemProf::Middleware
23
+ ```
24
+
25
+ # Options
26
+ path
27
+
28
+ scale_bytes
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'bundler'
2
+ require 'rake/testtask'
3
+ Bundler::GemHelper.install_tasks
4
+
5
+ task :default => :test
6
+
7
+ Rake::TestTask.new do |t|
8
+ t.libs << "test"
9
+ t.test_files = FileList['test/*_test.rb']
10
+ t.verbose = true
11
+ end
@@ -0,0 +1,6 @@
1
+ require 'rack/mem_prof/middleware'
2
+
3
+ module Rack
4
+ module MemProf
5
+ end
6
+ end
@@ -0,0 +1,64 @@
1
+ require 'fileutils'
2
+ require 'memory_profiler'
3
+ require 'rack'
4
+ require 'pry'
5
+
6
+ module Rack
7
+ module MemProf
8
+ class Middleware
9
+ REPEXT = '.txt'.freeze
10
+ REPDIR = 'rack_mem_prof'.freeze
11
+
12
+ def initialize(app, options = {})
13
+ @app = app
14
+ @path = options[:path] || "/tmp"
15
+ @scale_bytes = options[:scale_bytes] || true
16
+ end
17
+
18
+ def call(env)
19
+ MemoryProfiler.start
20
+ status, headers, body = @app.call(env)
21
+ write_report(MemoryProfiler.stop, env)
22
+
23
+ [status, headers, body]
24
+ rescue => e
25
+ MemoryProfiler.stop
26
+ raise e
27
+ end
28
+
29
+ private
30
+
31
+ # report to "/tmp/rack_mem_prof/<request path>/<query params sha1>.txt"
32
+ def write_report(report, env)
33
+ request_report_path = ensure_path(env)
34
+ report.pretty_print(
35
+ scale_bytes: @scale_bytes,
36
+ to_file: report_file(request_report_path, env)
37
+ )
38
+ end
39
+
40
+ def report_file(request_report_path, env)
41
+ filename = generate_filename(env['QUERY_STRING'])
42
+ ::File.join(request_report_path, filename)
43
+ end
44
+
45
+ def ensure_path(env)
46
+ request_dirname = webpath_to_dirname(env['PATH_INFO'])
47
+ request_report_path = ::File.join(reports_path, request_dirname)
48
+ FileUtils.mkdir_p request_report_path
49
+ end
50
+
51
+ def webpath_to_dirname(path_info)
52
+ path_info.gsub(/[^0-9A-Za-z]/, '').downcase
53
+ end
54
+
55
+ def generate_filename(query_string)
56
+ Digest::SHA1.hexdigest(query_string).concat(REPEXT)
57
+ end
58
+
59
+ def reports_path
60
+ ::File.join(@path, REPDIR)
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ module MemProf
3
+ VERSION = '0.0.2'
4
+ end
5
+ end
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/mem_prof/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'rack_mem_prof'
8
+ spec.version = Rack::MemProf::VERSION
9
+ spec.authors = ['Vitaly Shvedchenko']
10
+ spec.email = ['vitaly.shvedchenko@gmail.com']
11
+ spec.summary = 'Rack memory profiling middleware'
12
+ spec.description = "Sam Saffron's memory profiler made up as a rack middleware that writes reports to tmp folder"
13
+ spec.homepage = 'https://github.com/shved/rack_mem_prof'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files`.split("\n")
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 'memory_profiler', '~> 0.9'
23
+
24
+ spec.add_development_dependency 'bundler', '~> 2.0'
25
+ spec.add_development_dependency 'rake', '~> 13.0'
26
+ spec.add_development_dependency 'rack-test', '~> 1.1'
27
+ spec.add_development_dependency 'test-unit', '~> 3.3'
28
+ spec.add_development_dependency 'roda', '~> 3.29'
29
+ spec.add_development_dependency 'mocha', '~> 1.11'
30
+ spec.add_development_dependency 'pry'
31
+ end
32
+
33
+
data/test/dummy_app.rb ADDED
@@ -0,0 +1,9 @@
1
+ require 'roda'
2
+
3
+ class DummyApp < Roda
4
+ route do |r|
5
+ r.get '' do
6
+ "hi"
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,25 @@
1
+ require 'rack/mem_prof'
2
+ require 'test/unit'
3
+ require 'rack/test'
4
+ require 'mocha'
5
+ require 'mocha/setup'
6
+ require 'dummy_app'
7
+
8
+ class RackMemProfTest < Test::Unit::TestCase
9
+ include Rack::Test::Methods
10
+
11
+ attr_accessor :app
12
+
13
+ def teardown
14
+ @app = nil
15
+ end
16
+
17
+ def test_report_created
18
+ self.app = Rack::MemProf::Middleware.new(DummyApp.freeze.app)
19
+ MemoryProfiler.expects(:start).once
20
+ MemoryProfiler.expects(:stop).once
21
+ app.expects(:write_report).once
22
+ get '/'
23
+ assert last_response.ok?
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,182 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack_mem_prof
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Vitaly Shvedchenko
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-03-02 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: memory_profiler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.9'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.9'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '13.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '13.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rack-test
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.1'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.1'
83
+ - !ruby/object:Gem::Dependency
84
+ name: test-unit
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.3'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.3'
97
+ - !ruby/object:Gem::Dependency
98
+ name: roda
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - "~>"
102
+ - !ruby/object:Gem::Version
103
+ version: '3.29'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - "~>"
109
+ - !ruby/object:Gem::Version
110
+ version: '3.29'
111
+ - !ruby/object:Gem::Dependency
112
+ name: mocha
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '1.11'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '1.11'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: Sam Saffron's memory profiler made up as a rack middleware that writes
140
+ reports to tmp folder
141
+ email:
142
+ - vitaly.shvedchenko@gmail.com
143
+ executables: []
144
+ extensions: []
145
+ extra_rdoc_files: []
146
+ files:
147
+ - ".gitignore"
148
+ - Gemfile
149
+ - README.md
150
+ - Rakefile
151
+ - lib/rack/mem_prof.rb
152
+ - lib/rack/mem_prof/middleware.rb
153
+ - lib/rack/mem_prof/version.rb
154
+ - rack_mem_prof.gemspec
155
+ - test/dummy_app.rb
156
+ - test/rack_mem_prof_test.rb
157
+ homepage: https://github.com/shved/rack_mem_prof
158
+ licenses:
159
+ - MIT
160
+ metadata: {}
161
+ post_install_message:
162
+ rdoc_options: []
163
+ require_paths:
164
+ - lib
165
+ required_ruby_version: !ruby/object:Gem::Requirement
166
+ requirements:
167
+ - - ">="
168
+ - !ruby/object:Gem::Version
169
+ version: '0'
170
+ required_rubygems_version: !ruby/object:Gem::Requirement
171
+ requirements:
172
+ - - ">="
173
+ - !ruby/object:Gem::Version
174
+ version: '0'
175
+ requirements: []
176
+ rubygems_version: 3.1.2
177
+ signing_key:
178
+ specification_version: 4
179
+ summary: Rack memory profiling middleware
180
+ test_files:
181
+ - test/dummy_app.rb
182
+ - test/rack_mem_prof_test.rb