rack_mem_prof 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: a18a36ef5f7dd70c156fd5b1fd75597cd9b3069032e5f5522c13a018197d9e32
4
- data.tar.gz: 8c39106036de7812b16b9934fafc5678153b758d70a9418ff85e7804888f0c75
3
+ metadata.gz: 148ad3e85a052b4acf18e795977790a7862fa4dda7cdee2ff99b6557e12e736c
4
+ data.tar.gz: a8405090d40a1c2607088a240d6eeb615d4459e501d4f7d256ba82448efdf262
5
5
  SHA512:
6
- metadata.gz: 64d47fee0f12c47c88ab71740e6708d2738da0612e6595e227ae572f4827c50477efd35cd718d602d65d180655f3b3eac404d51d4aa6010527aedfcb5545a8ef
7
- data.tar.gz: a6d75f76fd72cd2689b2b57a62c1c0abb3797db71034d646b413f5bd2f530076524265bf3a6005c22f4f9523344065687a1f6847ed2d7d593dce8aa3b4f70954
6
+ metadata.gz: 6419abdeac526777a0900d00fde152dacd0f425bf3e3f1de2a0354b4cbd565e62a65183eded03bac8234e8f90123b7c1be1d368016f6f18fe6028b044a622480
7
+ data.tar.gz: 74762a5687acf6b840e7df7b93ee6bd2f98551594c2ad0942c47211120ba1af4d54e00300b98c2cc736d1b851288f854ee8cbb45f445579f2eab7ff1923b3fda
@@ -1,13 +1,14 @@
1
+ require 'digest'
1
2
  require 'fileutils'
2
3
  require 'memory_profiler'
3
4
  require 'rack'
4
- require 'pry'
5
5
 
6
6
  module Rack
7
7
  module MemProf
8
8
  class Middleware
9
9
  REPEXT = '.txt'.freeze
10
10
  REPDIR = 'rack_mem_prof'.freeze
11
+ UNSLUGGABLE = 'stub_for_unslaggable_requests'.freeze
11
12
 
12
13
  def initialize(app, options = {})
13
14
  @app = app
@@ -16,9 +17,20 @@ module Rack
16
17
  end
17
18
 
18
19
  def call(env)
19
- MemoryProfiler.start
20
- status, headers, body = @app.call(env)
21
- write_report(MemoryProfiler.stop, env)
20
+ report_path, report_filename = build_report_path(env)
21
+ path_with_file = ::File.join(report_path, report_filename)
22
+
23
+ if ::File.exists?(path_with_file)
24
+ status, headers, body = @app.call(env)
25
+ else
26
+ FileUtils.mkdir_p report_path
27
+
28
+ MemoryProfiler.start
29
+
30
+ status, headers, body = @app.call(env)
31
+
32
+ write_report(MemoryProfiler.stop, path_with_file)
33
+ end
22
34
 
23
35
  [status, headers, body]
24
36
  rescue => e
@@ -29,27 +41,26 @@ module Rack
29
41
  private
30
42
 
31
43
  # 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)
44
+ def write_report(report, path)
34
45
  report.pretty_print(
35
46
  scale_bytes: @scale_bytes,
36
- to_file: report_file(request_report_path, env)
47
+ to_file: path,
37
48
  )
38
49
  end
39
50
 
40
- def report_file(request_report_path, env)
41
- filename = generate_filename(env['QUERY_STRING'])
42
- ::File.join(request_report_path, filename)
51
+ def build_report_path(env)
52
+ return generate_path(env['PATH_INFO']), generate_filename(env['QUERY_STRING'])
43
53
  end
44
54
 
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
55
+ def generate_path(path_info)
56
+ dirname_for_request = generate_dirname(path_info)
57
+ ::File.join(reports_path, dirname_for_request)
49
58
  end
50
59
 
51
- def webpath_to_dirname(path_info)
52
- path_info.gsub(/[^0-9A-Za-z]/, '').downcase
60
+ def generate_dirname(path_info)
61
+ dirname = path_info.gsub(/[^0-9A-Za-z]/, '').downcase
62
+
63
+ return UNSLUGGABLE if dirname.length.zero?
53
64
  end
54
65
 
55
66
  def generate_filename(query_string)
@@ -1,5 +1,5 @@
1
1
  module Rack
2
2
  module MemProf
3
- VERSION = '0.0.2'
3
+ VERSION = '0.0.4'
4
4
  end
5
5
  end
@@ -27,7 +27,7 @@ Gem::Specification.new do |spec|
27
27
  spec.add_development_dependency 'test-unit', '~> 3.3'
28
28
  spec.add_development_dependency 'roda', '~> 3.29'
29
29
  spec.add_development_dependency 'mocha', '~> 1.11'
30
- spec.add_development_dependency 'pry'
30
+ spec.add_development_dependency 'pry-byebug'
31
31
  end
32
32
 
33
33
 
@@ -1,3 +1,4 @@
1
+ require 'fileutils'
1
2
  require 'rack/mem_prof'
2
3
  require 'test/unit'
3
4
  require 'rack/test'
@@ -16,10 +17,13 @@ class RackMemProfTest < Test::Unit::TestCase
16
17
 
17
18
  def test_report_created
18
19
  self.app = Rack::MemProf::Middleware.new(DummyApp.freeze.app)
20
+
19
21
  MemoryProfiler.expects(:start).once
20
22
  MemoryProfiler.expects(:stop).once
21
23
  app.expects(:write_report).once
24
+
22
25
  get '/'
26
+
23
27
  assert last_response.ok?
24
28
  end
25
29
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack_mem_prof
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vitaly Shvedchenko
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-03-02 00:00:00.000000000 Z
11
+ date: 2020-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -123,7 +123,7 @@ dependencies:
123
123
  - !ruby/object:Gem::Version
124
124
  version: '1.11'
125
125
  - !ruby/object:Gem::Dependency
126
- name: pry
126
+ name: pry-byebug
127
127
  requirement: !ruby/object:Gem::Requirement
128
128
  requirements:
129
129
  - - ">="