rack-stackprof 0.1.0

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: be98bd41340ec063dc940d7cc493e9273b61e90e8bb35639ea31193c4ce2abdf
4
+ data.tar.gz: 21a35e040a9831d8f1fdb2eaed43c97729b797457e63f28b2115dfb7172a1d05
5
+ SHA512:
6
+ metadata.gz: f74cf3e2060bb1becaa3dad924dbcbddadfdb7a7031a5b24669161a249c7f56fad3f6b4663f951932b710150af9aa46f1b0a3e6492186aa4d45bdb11a882b84e
7
+ data.tar.gz: e633f6b4215cb383392e01e64957798940a8a0e805deba6da3847cdcea50950e80035ed36a0c099bd115b815efb07707d5e672c7089b53fb07da7df52e66a247
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in rack-stackprof.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,22 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ rack-stackprof (0.1.0)
5
+ stackprof
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ rake (12.3.0)
11
+ stackprof (0.2.11)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ bundler
18
+ rack-stackprof!
19
+ rake
20
+
21
+ BUNDLED WITH
22
+ 1.16.1.pre1
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Takashi Kokubun
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,44 @@
1
+ # Rack::Stackprof
2
+
3
+ Periodically dump StackProf profile result to `tmp` with easy-to-understand filenames
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'rack-stackprof'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ ```bash
16
+ $ bundle
17
+ ```
18
+
19
+ ## Usage
20
+
21
+ ```rb
22
+ if ENV['STACKPROF_ENABLE'] == 'true'
23
+ Rails.application.middleware.use(
24
+ Rack::Stackprof,
25
+ profile_interval_seconds: 1,
26
+ sampling_interval_microseconds: 1000,
27
+ result_directory: 'tmp',
28
+ )
29
+ end
30
+ ```
31
+
32
+ After starting application with environment variable `STACKPROF_ENABLE=true`,
33
+ files whose name is like following ones will be periodically (once per second as `profile_interval_seconds: 1`) dumped to `tmp`.
34
+
35
+ ```
36
+ stackprof-20171004_175816-41860-GET_v1_users-0308ms.dump
37
+ stackprof-20171004_175924-41860-GET_v1_accounts-0248ms.dump
38
+ stackprof-20171004_175955-41860-GET_v1_comments-1029ms.dump
39
+ ...
40
+ ```
41
+
42
+ ## License
43
+
44
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
data/Rakefile ADDED
@@ -0,0 +1,2 @@
1
+ require "bundler/gem_tasks"
2
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "rack/stackprof"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,5 @@
1
+ module Rack
2
+ class Stackprof
3
+ VERSION = '0.1.0'
4
+ end
5
+ end
@@ -0,0 +1,47 @@
1
+ require 'stackprof'
2
+ require 'rack/stackprof/version'
3
+
4
+ class Rack::Stackprof
5
+ def initialize(app, options = {})
6
+ @app = app
7
+ @profile_interval_seconds = options.fetch(:profile_interval_seconds)
8
+ @sampling_interval_microseconds = options.fetch(:sampling_interval_microseconds)
9
+ @last_profiled_at = nil
10
+ StackProf::Middleware.path = options.fetch(:result_directory) # for `StackProf::Middleware.save`
11
+ end
12
+
13
+ def call(env)
14
+ # Profile every X seconds (not everytime) to prevent from consuming disk excessively
15
+ profile_every(seconds: @profile_interval_seconds, env: env) do
16
+ @app.call(env)
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def profile_every(seconds:, env:, &block)
23
+ if @last_profiled_at.nil? || @last_profiled_at < Time.now - seconds
24
+ @last_profiled_at = Time.now
25
+ with_profile(env) { block.call }
26
+ else
27
+ block.call
28
+ end
29
+ end
30
+
31
+ def with_profile(env)
32
+ started_at = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_microsecond)
33
+ StackProf.start(mode: :wall, interval: @sampling_interval_microseconds)
34
+ yield
35
+ ensure
36
+ StackProf.stop
37
+ finished_at = Process.clock_gettime(Process::CLOCK_MONOTONIC, :float_microsecond)
38
+
39
+ filename = result_filename(env: env, duration_milliseconds: (finished_at - started_at) / 1000)
40
+ StackProf::Middleware.save(filename)
41
+ end
42
+
43
+ # ex: "stackprof-20171004_175816-41860-GET_v1_users-0308ms.dump"
44
+ def result_filename(env:, duration_milliseconds:)
45
+ "stackprof-#{Time.now.strftime('%Y%m%d_%H%M%S')}-#{Process.pid}-#{env['REQUEST_METHOD']}#{env['REQUEST_PATH'].gsub(/[^\w]/, '_')}-#{'%04d' % duration_milliseconds.to_i}ms.dump"
46
+ end
47
+ end
@@ -0,0 +1,26 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'rack/stackprof/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = 'rack-stackprof'
7
+ spec.version = Rack::Stackprof::VERSION
8
+ spec.authors = ['Takashi Kokubun']
9
+ spec.email = ['takashikkbn@gmail.com']
10
+
11
+ spec.summary = %q{Periodically dump StackProf profile result to tmp directory with easy-to-understand filename}
12
+ spec.description = %q{Periodically dump StackProf profile result to tmp directory with easy-to-understand filename}
13
+ spec.homepage = 'https://github.com/k0kubun/rack-stackprof'
14
+ spec.license = 'MIT'
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = 'exe'
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ['lib']
22
+
23
+ spec.add_dependency 'stackprof'
24
+ spec.add_development_dependency 'bundler'
25
+ spec.add_development_dependency 'rake'
26
+ end
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rack-stackprof
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Takashi Kokubun
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-12-15 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: stackprof
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: bundler
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: rake
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
+ description: Periodically dump StackProf profile result to tmp directory with easy-to-understand
56
+ filename
57
+ email:
58
+ - takashikkbn@gmail.com
59
+ executables: []
60
+ extensions: []
61
+ extra_rdoc_files: []
62
+ files:
63
+ - ".gitignore"
64
+ - Gemfile
65
+ - Gemfile.lock
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - lib/rack/stackprof.rb
72
+ - lib/rack/stackprof/version.rb
73
+ - rack-stackprof.gemspec
74
+ homepage: https://github.com/k0kubun/rack-stackprof
75
+ licenses:
76
+ - MIT
77
+ metadata: {}
78
+ post_install_message:
79
+ rdoc_options: []
80
+ require_paths:
81
+ - lib
82
+ required_ruby_version: !ruby/object:Gem::Requirement
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ version: '0'
87
+ required_rubygems_version: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ">="
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ requirements: []
93
+ rubyforge_project:
94
+ rubygems_version: 2.7.3
95
+ signing_key:
96
+ specification_version: 4
97
+ summary: Periodically dump StackProf profile result to tmp directory with easy-to-understand
98
+ filename
99
+ test_files: []