perf_spec 0.1.0

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
+ SHA1:
3
+ metadata.gz: 5db3b8a380052067d6c0d6569f95cc0fe03805a7
4
+ data.tar.gz: a32036ba898294191e1ed4233406111f82faa598
5
+ SHA512:
6
+ metadata.gz: cea4cdeb661b5afb7bf56b91c1f8e60c998a9d539724f00186cf91997cd9ac95bc0d5a313d9ee55103c4b20773916bd005c65ef553e4a936fc58587cc0b7c28f
7
+ data.tar.gz: 0825626e91bec8084206153e2f8a3f698767068ea35d2eb3efdb641045c60bed1ac20c8d16fcebc38b6f808809a2b365a194510db14bc19fe95ed6ff787e8643
data/.gitignore ADDED
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+
11
+ /*.gem
12
+ /*.rbc
13
+ /*.bundle
14
+ /.rubocop.yml
15
+ /.ruby-version
data/.rspec ADDED
@@ -0,0 +1,4 @@
1
+ --color
2
+ --require spec_helper
3
+ --format progress
4
+ --no-profile
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in perf_spec.gemspec
4
+ gemspec
data/Guardfile ADDED
@@ -0,0 +1,11 @@
1
+ guard :rspec, cmd: 'rspec' do
2
+ watch(%r{^lib/(.+).rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
3
+ watch(%r{^spec/(.+).rb$}) { |m| "spec/#{m[1]}.rb" }
4
+ watch('spec/spec_helper.rb') { 'spec' }
5
+ watch('Gemfile')
6
+ end
7
+
8
+ guard :rubocop, all_on_start: false, cli: ['--format', 'clang'] do
9
+ watch(%r{.+\.rb$})
10
+ watch(%r{(?:.+/)?\.rubocop\.yml$}) { |m| File.dirname(m[0]) }
11
+ end
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Pawel Niemczyk
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,39 @@
1
+ # PerfSpec
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/perf_spec`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'perf_spec'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install perf_spec
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ 1. Fork it ( https://github.com/[my-github-username]/perf_spec/fork )
36
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
37
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
38
+ 4. Push to the branch (`git push origin my-new-feature`)
39
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "perf_spec"
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
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
data/lib/perf_spec.rb ADDED
@@ -0,0 +1,7 @@
1
+ require 'perf_spec/version'
2
+ require 'perf_spec/why_so_long'
3
+ require 'rspec/matchers/take_less_than'
4
+
5
+ module PerfSpec
6
+
7
+ end
@@ -0,0 +1,3 @@
1
+ module PerfSpec
2
+ VERSION = '0.1.0'
3
+ end
@@ -0,0 +1,93 @@
1
+ require 'benchmark'
2
+ require 'json'
3
+
4
+ module PerfSpec
5
+ class WhySoLong
6
+ attr_reader :expected_time, :real_time, :check_result, :meta_request_id, :bullet_warnings, :bullet_enable_stat
7
+
8
+ def initialize(expected_time)
9
+ @expected_time = expected_time
10
+ end
11
+
12
+ def check(&block)
13
+ @real_time = Benchmark.realtime do
14
+ @check_result = block.call
15
+ end
16
+ end
17
+
18
+ def success?
19
+ @success ||= real_time <= expected_time
20
+ end
21
+
22
+ def failure_message(_actual, matcher)
23
+ @meta_request_id = extract_meta_request_id(matcher)
24
+ return default_failure_message unless meta_request_id
25
+ [
26
+ default_failure_message,
27
+ "\nTakes controller: ",
28
+ META_REQUEST_KEYS.map { |key| "#{key}: #{duration_sum(meta_request_output, key)}" }.join(', '),
29
+ "\n",
30
+ meta_request_output.ai(indent: -2)
31
+ ].join('')
32
+ end
33
+
34
+ private
35
+
36
+ META_REQUEST_KEYS = :controller, :view, :partial_view, :sql, :other
37
+
38
+ def extract_meta_request_id(matcher)
39
+ return nil unless matcher.respond_to?(:response)
40
+ matcher.response.headers['X-Request-Id']
41
+ end
42
+
43
+ def default_failure_message
44
+ "expected that block take less than #{expected_time} but takes #{real_time}"
45
+ end
46
+
47
+ def meta_request_view_path(path_full)
48
+ extract_path(path_full, '/app/view')
49
+ end
50
+
51
+ def meta_request_sql_path(path_full)
52
+ extract_path(path_full, '/app/')
53
+ end
54
+
55
+ def extract_path(path_full, folder)
56
+ path_full[path_full.index(folder)..-1]
57
+ end
58
+
59
+ def duration(val)
60
+ val.to_f / 1000
61
+ end
62
+
63
+ def duration_sum(res, key)
64
+ return 0 unless res[key]
65
+ res[key].inject(0) do |num, el|
66
+ num += el[:duration]
67
+ end
68
+ end
69
+
70
+ def meta_request_json
71
+ @meta_request_json ||= JSON.parse(MetaRequest::Storage.new(meta_request_id).read)
72
+ end
73
+
74
+ def meta_request_output
75
+ @meta_request_output ||= meta_request_json.each_with_object({}) do |el, o|
76
+ base_hash = { duration: duration(el['duration']) }
77
+ case el['name']
78
+ when 'render_partial.action_view'
79
+ (o[:partial_view] ||= []).push(base_hash.merge(path: meta_request_view_path(el['payload']['identifier'])))
80
+ when 'render_template.action_view'
81
+ (o[:view] ||= []).push(base_hash.merge(path: meta_request_view_path(el['payload']['identifier']), layout: el['payload']['layout']))
82
+ when 'process_action.action_controller'
83
+ (o[:controller] ||= []).push(base_hash)
84
+ when 'sql.active_record'
85
+ pl = el['payload']
86
+ (o[:sql] ||= []).push(base_hash.merge(name: pl['name'], sql: pl['sql'], method: pl['method'], filename: meta_request_sql_path(pl['filename'])))
87
+ else
88
+ (o[:other] ||= []).push(base_hash)
89
+ end
90
+ end
91
+ end
92
+ end
93
+ end
@@ -0,0 +1,15 @@
1
+ RSpec::Matchers.define(:take_less_than) do |n|
2
+ match do |block|
3
+ @why_so_long = PerfSpec::WhySoLong.new(n)
4
+ @why_so_long.check(&block)
5
+ @why_so_long.success?
6
+ end
7
+
8
+ def supports_block_expectations?
9
+ true
10
+ end
11
+
12
+ failure_message do |actual|
13
+ @why_so_long.failure_message(actual, self)
14
+ end
15
+ end
data/perf_spec.gemspec ADDED
@@ -0,0 +1,31 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'perf_spec/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = 'perf_spec'
8
+ spec.version = PerfSpec::VERSION
9
+ spec.authors = ['Pawel Niemczyk']
10
+ spec.email = ['pniemczyk@o2.pl']
11
+
12
+ spec.summary = 'Add ability to test performance of request in Rspec tests'
13
+ spec.description = 'Add ability to test performance of request in Rspec tests'
14
+ spec.homepage = 'https://github.com/pniemczyk/perf_spec'
15
+ spec.license = 'MIT'
16
+
17
+ spec.files = `git ls-files -z`.split("\x0")
18
+ spec.bindir = 'exe'
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ['lib']
21
+
22
+ spec.add_development_dependency 'bundler', '~> 1.9'
23
+ spec.add_development_dependency 'rake', '~> 10.0'
24
+ spec.add_development_dependency 'rspec', '~> 3.0'
25
+ spec.add_development_dependency 'guard', '~> 2.12'
26
+ spec.add_development_dependency 'guard-rspec', '~> 4.5'
27
+ spec.add_development_dependency 'guard-rubocop', '~> 1.2'
28
+ spec.add_development_dependency 'coveralls', '~> 0.7'
29
+ spec.add_runtime_dependency 'meta_request', '~> 0.3'
30
+ spec.add_runtime_dependency 'awesome_print', '~> 1.6'
31
+ end
@@ -0,0 +1,13 @@
1
+ require 'spec_helper'
2
+
3
+ describe PerfSpec, type: :request do
4
+ it 'has a version number' do
5
+ expect(PerfSpec::VERSION).not_to be nil
6
+ end
7
+
8
+ it 'pass when action take_less_than' do
9
+ expect do
10
+ sleep 0.1
11
+ end.to take_less_than(0.2)
12
+ end
13
+ end
@@ -0,0 +1,31 @@
1
+ require 'bundler/setup'
2
+ require 'awesome_print'
3
+ Bundler.setup
4
+
5
+ require 'coveralls'
6
+ Coveralls.wear!
7
+
8
+ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
9
+ require 'perf_spec'
10
+
11
+ RSpec.configure do |config|
12
+ config.filter_run :focus
13
+ config.run_all_when_everything_filtered = true
14
+
15
+ config.default_formatter = 'doc' if config.files_to_run.one?
16
+
17
+ config.profile_examples = 10
18
+ config.order = :random
19
+
20
+ Kernel.srand config.seed
21
+
22
+ config.expect_with :rspec do |expectations|
23
+ expectations.syntax = :expect
24
+ end
25
+
26
+ config.mock_with :rspec do |mocks|
27
+ mocks.syntax = :expect
28
+ mocks.verify_partial_doubles = true
29
+ end
30
+ end
31
+ require 'guard/rubocop'
metadata ADDED
@@ -0,0 +1,187 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: perf_spec
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Pawel Niemczyk
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-06-01 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.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: guard
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '2.12'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '2.12'
69
+ - !ruby/object:Gem::Dependency
70
+ name: guard-rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ~>
74
+ - !ruby/object:Gem::Version
75
+ version: '4.5'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ~>
81
+ - !ruby/object:Gem::Version
82
+ version: '4.5'
83
+ - !ruby/object:Gem::Dependency
84
+ name: guard-rubocop
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ~>
88
+ - !ruby/object:Gem::Version
89
+ version: '1.2'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ~>
95
+ - !ruby/object:Gem::Version
96
+ version: '1.2'
97
+ - !ruby/object:Gem::Dependency
98
+ name: coveralls
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ~>
102
+ - !ruby/object:Gem::Version
103
+ version: '0.7'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ~>
109
+ - !ruby/object:Gem::Version
110
+ version: '0.7'
111
+ - !ruby/object:Gem::Dependency
112
+ name: meta_request
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ~>
116
+ - !ruby/object:Gem::Version
117
+ version: '0.3'
118
+ type: :runtime
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ~>
123
+ - !ruby/object:Gem::Version
124
+ version: '0.3'
125
+ - !ruby/object:Gem::Dependency
126
+ name: awesome_print
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ~>
130
+ - !ruby/object:Gem::Version
131
+ version: '1.6'
132
+ type: :runtime
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ~>
137
+ - !ruby/object:Gem::Version
138
+ version: '1.6'
139
+ description: Add ability to test performance of request in Rspec tests
140
+ email:
141
+ - pniemczyk@o2.pl
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - .gitignore
147
+ - .rspec
148
+ - .travis.yml
149
+ - Gemfile
150
+ - Guardfile
151
+ - LICENSE.txt
152
+ - README.md
153
+ - Rakefile
154
+ - bin/console
155
+ - bin/setup
156
+ - lib/perf_spec.rb
157
+ - lib/perf_spec/version.rb
158
+ - lib/perf_spec/why_so_long.rb
159
+ - lib/rspec/matchers/take_less_than.rb
160
+ - perf_spec.gemspec
161
+ - spec/perf_spec_spec.rb
162
+ - spec/spec_helper.rb
163
+ homepage: https://github.com/pniemczyk/perf_spec
164
+ licenses:
165
+ - MIT
166
+ metadata: {}
167
+ post_install_message:
168
+ rdoc_options: []
169
+ require_paths:
170
+ - lib
171
+ required_ruby_version: !ruby/object:Gem::Requirement
172
+ requirements:
173
+ - - '>='
174
+ - !ruby/object:Gem::Version
175
+ version: '0'
176
+ required_rubygems_version: !ruby/object:Gem::Requirement
177
+ requirements:
178
+ - - '>='
179
+ - !ruby/object:Gem::Version
180
+ version: '0'
181
+ requirements: []
182
+ rubyforge_project:
183
+ rubygems_version: 2.4.6
184
+ signing_key:
185
+ specification_version: 4
186
+ summary: Add ability to test performance of request in Rspec tests
187
+ test_files: []