rack-mp 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 22895b209bd2e1a98ef961edeaba34a147f7f107
4
+ data.tar.gz: 014e5fdf5e5847cc1836c8ff8a726221e579ceb2
5
+ SHA512:
6
+ metadata.gz: ab9631e11ee6057099e88204e73767150f3e58d2f0ea8fcd2f0e4d6a9d37f002062873cec5d3e8b78b5598a2add6f33b5ddff8402c5644b0d49963badc54660b
7
+ data.tar.gz: 71b4e6518226d5bfae0074306b856495b8f2f5e66cf25d5928df22742cb5ed1a56b034e1cf9ccb4a1c1ce569f4ed35409779352077584a79e09c239e4373b4fc
@@ -0,0 +1,23 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ *.bundle
19
+ *.so
20
+ *.o
21
+ *.a
22
+ mkmf.log
23
+ test/*.data
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mp.gemspec
4
+ gemspec
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 JJ Buckley
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.
@@ -0,0 +1,40 @@
1
+ # MP
2
+
3
+ Memory profiler for Rack apps (on Ruby 2)
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'mp'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install mp
18
+
19
+ ## Usage
20
+
21
+ Mount MP as a middleware.
22
+
23
+ Visit a path with `__mp__=counts` somewhere in the query string.
24
+
25
+ Check the response headers for `X-MP-ObjectCounts`.
26
+
27
+
28
+ ## TODO
29
+
30
+ * Make the header smaller (serialize to "OBJNAME:N1:N2,...", for example)
31
+ * Use Ruby 2.1's excellent objspace library to do full object profiling, and
32
+ serve beautiful graphs and whatnot. Mañana.
33
+
34
+ ## Contributing
35
+
36
+ 1. Fork it ( https://github.com/[my-github-username]/mp/fork )
37
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
38
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
39
+ 4. Push to the branch (`git push origin my-new-feature`)
40
+ 5. Create a new Pull Request
@@ -0,0 +1,9 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.test_files = FileList["test/**/*_test.rb"]
6
+ t.libs << "test"
7
+ end
8
+
9
+ task default: :test
@@ -0,0 +1,6 @@
1
+ require 'rack/mp/middleware'
2
+
3
+ module Rack
4
+ module MP
5
+ end
6
+ end
@@ -0,0 +1,64 @@
1
+ require 'multi_json'
2
+
3
+ module Rack
4
+ module MP
5
+ # Small middleware to count Ruby objects before and after a request. Puts
6
+ # the values in the X-MP-ObjectCounts response header. Just pass
7
+ # __mp__=counts in the query string.
8
+ #
9
+ # That's all it does at the moment - probably only useful for identifying
10
+ # which type of object is causing a memory leak. And, possibly, confirming
11
+ # which queries are contributing.
12
+ class Middleware
13
+
14
+ # New middleware instance.
15
+ def initialize(app)
16
+ @app = app
17
+ end
18
+
19
+ # Run the middleware. Will count the objects, run the app, count the
20
+ # objects, and output how long the whole thing took, and the number of
21
+ # objects before and after.
22
+ #
23
+ # NOTE - the header string will be JSON! Yuck. But it was the quickest
24
+ # way to do it for now.
25
+ def call(env)
26
+ return @app.call(env) unless active?(env)
27
+ before, t0 = time { count_objects }
28
+ result, t1 = time { @app.call(env.merge('mp.counting' => true)) }
29
+ after, t2 = time { count_objects }
30
+ timings = { before: t0, request: t1, after: t2 }
31
+ data = serialize(before: before, after: after, timings: timings)
32
+ status, headers, body = *result
33
+ [status, headers.merge('X-MP-ObjectCounts' => data), result]
34
+ end
35
+
36
+ private
37
+ # Check whether we need to do anything.
38
+ # Just looks for __mp__=counts in the query string. Not very
39
+ # sophisticated. Could be extended to check authentication, etc.
40
+ def active?(env)
41
+ env['QUERY_STRING'] =~ %r{__mp__=counts}
42
+ end
43
+
44
+ # The simplest object counter. Could potentially be expanded to ask all
45
+ # apps in a cluster, and sum them!
46
+ def count_objects
47
+ ObjectSpace.count_objects
48
+ end
49
+
50
+ # Make a string for the response.
51
+ # Easiest output for now seems to be JSON
52
+ def serialize(*args)
53
+ MultiJson.dump(*args)
54
+ end
55
+
56
+ # Time an operation, returning the operation's result and the time it
57
+ # took.
58
+ def time(&block)
59
+ t = Time.now
60
+ [yield, Time.now - t]
61
+ end
62
+ end
63
+ end
64
+ end
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ module MP
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rack/mp/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rack-mp"
8
+ spec.version = Rack::MP::VERSION
9
+ spec.authors = ["JJ Buckley"]
10
+ spec.email = ["jj@bjjb.org"]
11
+ spec.summary = %q{Rack Memory Profiler}
12
+ spec.description = %q{Helps analyse memory usage in your Rack apps}
13
+ spec.homepage = "http://bjjb.github.io/rack-mp"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0")
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_development_dependency "bundler", "~> 1.6"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "rack-test"
24
+ spec.add_dependency "rack" # obviously
25
+ spec.add_dependency "multi_json" # chooses the fastest JSON available
26
+ spec.add_dependency "hipsterhash" # faster than an OpenStruct
27
+ end
@@ -0,0 +1,38 @@
1
+ require 'test_helper'
2
+ require 'json'
3
+ require 'rack/mp/middleware'
4
+
5
+ describe Rack::MP::Middleware do
6
+ include Rack::Test::Methods
7
+
8
+ let(:middleware) do
9
+ Rack::MP::Middleware
10
+ end
11
+
12
+ let(:app) do
13
+ Rack::Builder.new do
14
+ use Rack::MP::Middleware
15
+ run lambda { |env| [200, {}, ["OK"]] }
16
+ end
17
+ end
18
+
19
+ let(:data) do
20
+ JSON.parse(last_response.body)
21
+ end
22
+
23
+ let(:header) do
24
+ JSON.parse(last_response.header['X-MP-ObjectCounts'])
25
+ end
26
+
27
+ it "can be used to see object counts" do
28
+ get "/"
29
+ last_response.must_be :ok?
30
+ last_response.header.keys.wont_include 'X-MP-ObjectCounts'
31
+ get "/?__mp__=counts"
32
+ last_response.must_be :ok?
33
+ last_response.header.keys.must_include 'X-MP-ObjectCounts'
34
+ header.keys.must_include("before")
35
+ header.keys.must_include("after")
36
+ header.keys.must_include("timings")
37
+ end
38
+ end
@@ -0,0 +1,6 @@
1
+ require 'test_helper'
2
+ require 'rack/mp'
3
+
4
+ describe Rack::MP do
5
+ # No tests, yet.
6
+ end
@@ -0,0 +1,3 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/spec'
3
+ require 'rack/test'
metadata ADDED
@@ -0,0 +1,143 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-mp
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - JJ Buckley
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-04-11 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.6'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.6'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
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: rack-test
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
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: rack
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: multi_json
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: hipsterhash
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :runtime
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ description: Helps analyse memory usage in your Rack apps
98
+ email:
99
+ - jj@bjjb.org
100
+ executables: []
101
+ extensions: []
102
+ extra_rdoc_files: []
103
+ files:
104
+ - ".gitignore"
105
+ - Gemfile
106
+ - LICENSE.txt
107
+ - README.md
108
+ - Rakefile
109
+ - lib/rack/mp.rb
110
+ - lib/rack/mp/middleware.rb
111
+ - lib/rack/mp/version.rb
112
+ - rack-mp.gemspec
113
+ - test/rack/mp/middleware_test.rb
114
+ - test/rack/mp_test.rb
115
+ - test/test_helper.rb
116
+ homepage: http://bjjb.github.io/rack-mp
117
+ licenses:
118
+ - MIT
119
+ metadata: {}
120
+ post_install_message:
121
+ rdoc_options: []
122
+ require_paths:
123
+ - lib
124
+ required_ruby_version: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - ">="
127
+ - !ruby/object:Gem::Version
128
+ version: '0'
129
+ required_rubygems_version: !ruby/object:Gem::Requirement
130
+ requirements:
131
+ - - ">="
132
+ - !ruby/object:Gem::Version
133
+ version: '0'
134
+ requirements: []
135
+ rubyforge_project:
136
+ rubygems_version: 2.2.2
137
+ signing_key:
138
+ specification_version: 4
139
+ summary: Rack Memory Profiler
140
+ test_files:
141
+ - test/rack/mp/middleware_test.rb
142
+ - test/rack/mp_test.rb
143
+ - test/test_helper.rb