rails_api_benchmark 0.2.1 → 0.2.2

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
  SHA1:
3
- metadata.gz: 6bb55b14a6661c98fb64522d6b5fe943c0febdd7
4
- data.tar.gz: d8c3b85abd768f3f93824145504d397ac7cad33f
3
+ metadata.gz: fc553c272bb5f42a3b765748336af37be9503e74
4
+ data.tar.gz: 6cff65ef9d213d09b809fdc7d3b237a53707e824
5
5
  SHA512:
6
- metadata.gz: 16d5d896be871a3c49d11a048249177993b07b6ba8c495462b915f8db1854636367127e35a85e1916e6054f875fdd6c94266d39a0f0108c31063d579d15b3dfa
7
- data.tar.gz: bd060c86bbc5e8b03cf4432777d19989f5883c70c4685db42c5ff1d2d8923e26ad4b26750a822d18daa078c690f675e2e9c5475c60bbb8277523e2fec4a304ac
6
+ metadata.gz: 144655c6afb4d8711864152918a5f78e6bbbdc0f2a5d56e61cd59fa62e205eb36c8b8453794a024903d0baf0df9a06acda0a5cd5fb05ffdae260951eec6f7f20
7
+ data.tar.gz: faeda2140659f2f4d7a509c447ddde94813b770013b997963f1d91ef0651cfb90043f42fd301bc5c30d0960c3888450cb666e3c2cd5d260e8c48b7b2e46ee290
data/README.md CHANGED
@@ -1,10 +1,59 @@
1
1
  # RailsApiBenchmark
2
2
 
3
- Work in progress, yet you can use it like this.
3
+ Work in progress, you can use it like this, see [Important](#important)
4
4
 
5
5
  ## Usage
6
6
 
7
- Run it with rake api:benchmark
7
+ Run it with:
8
+ ```bash
9
+ rails api:benchmark
10
+ ```
11
+
12
+ Display your configuration with:
13
+ ```bash
14
+ rails api:benchmark:config
15
+ ```
16
+
17
+ Example output:
18
+
19
+ ```json
20
+ {
21
+ "concurrency": 2,
22
+ "host": "localhost:5000",
23
+ "nb_requests": 1000,
24
+ "results_folder": "benchmark",
25
+ "auth_header": "Authorization: Token token=benchToken",
26
+ "curl_cmd": "curl -H \"%{auth_header}\" http://%{host}%{route}",
27
+ "bench_cmd": "ab -n %{nb_requests} -c %{concurrency} -g plot.tsv -H \"%{auth_header}\" http://%{host}%{route}",
28
+ "server_cmd": "bundle exec puma",
29
+ "env_vars": {
30
+ "RAILS_MAX_THREADS": "2"
31
+ },
32
+ "regexps": [
33
+ {
34
+ "key": "response_time",
35
+ "name": "Average time per request (ms)",
36
+ "regexp": "(?-mix:Time\\s+per\\s+request:\\s+([0-9.]*).*\\(mean\\))"
37
+ },
38
+ {
39
+ "key": "req_per_sec",
40
+ "name": "Requests per second (#)",
41
+ "regexp": "(?-mix:Requests\\s+per\\s+second:\\s+([0-9.]*).*\\(mean\\))"
42
+ }
43
+ ],
44
+ "routes": [
45
+ {
46
+ "name": "candidates_per_25",
47
+ "route": "/candidates",
48
+ "method": "get",
49
+ "title": "GET /candidates",
50
+ "description": "Get first page of candidates (default 25 per page)"
51
+ }
52
+ ]
53
+ }
54
+
55
+ ```
56
+
8
57
 
9
58
  ## Important
10
59
 
@@ -14,73 +63,42 @@ Run it with rake api:benchmark
14
63
 
15
64
  ## Installation
16
65
 
17
- Install gnuplot (Google)
66
+ * Install gnuplot
67
+ * Install ApacheBench
18
68
 
19
- Add this line to your application's Gemfile:
69
+ For ubuntu:
70
+ ```bash
71
+ sudo apt-get install gnuplot
72
+ sudo apt-get install apache2-utils
73
+ ```
74
+ * Add this line to your application's Gemfile:
20
75
 
21
76
  ```ruby
22
77
  gem 'rails_api_benchmark'
23
78
  ```
24
79
 
25
- And then execute:
80
+ * And then execute:
26
81
  ```bash
27
- $ bundle
82
+ bundle
28
83
  ```
29
84
 
30
- Provide necessary configuration in initializer, example config (mine):
85
+ * Provide necessary configuration in initializer, generate it with:
31
86
 
32
- ```ruby
33
- unless Rails.env.production?
34
- RailsApiBenchmark.configure do |config|
35
- # Try different configs. You may need to run the
36
- # benchmark in a production ready environment to get reliable results
37
- config.concurrency = 2
38
- config.host = 'localhost:5000' # example.com
39
- config.nb_requests = 3000
40
- config.results_folder = 'benchmark'
41
- config.auth_header = 'Authorization: Token token=benchToken'
42
- # Use only if you want to log the responses
43
- config.curl_cmd = 'curl -H "%{auth_header}" http://%{host}%{route}'
44
- # Use Apache Bench
45
- config.bench_cmd = 'ab -n %{nb_requests} -c %{concurrency} -g plot.tsv -e plot.csv -H "%{auth_header}" http://%{host}%{route}'
46
- config.server_cmd = 'bundle exec puma'
47
- config.env_vars = {
48
- 'RAILS_MAX_THREADS' => '2',
49
- 'SECRET_KEY_BASE' => 'bench',
50
- 'RAILS_ENV' => 'production',
51
- 'SSL_DISABLE' => 'yup',
52
- 'PORT' => '5000'
53
- }
54
- config.regexps = [ # Used to get results from the output of benchmark tools
55
- {
56
- key: :response_time,
57
- name: 'Average time per request (ms)',
58
- regexp: /Time\s+per\s+request:\s+([0-9.]*).*\(mean\)/
59
- }, {
60
- key: :req_per_sec,
61
- name: 'Requests per second (#)',
62
- regexp: /Requests\s+per\s+second:\s+([0-9.]*).*\(mean\)/
63
- }
64
- ]
65
- config.routes = [
66
- {
67
- name: 'candidates_per_25',
68
- route: '/candidates',
69
- method: :get,
70
- title: 'GET /candidates'
71
- }
72
- ].freeze
73
- end
74
- end
87
+ ```bash
88
+ rails g rails_api_benchmark:config
75
89
  ```
76
90
 
77
- Next, add this to your Rakefile:
91
+ * Next, add this to your Rakefile:
78
92
 
79
93
  ```ruby
80
94
  require 'rails_api_benchmark/benchmark_tasks'
81
95
  ```
82
96
 
83
- You can now run `rake api:benchmark` !
97
+ You can now run:
98
+
99
+ ```bash
100
+ rails api:benchmark
101
+ ```
84
102
 
85
103
  ## Contributing
86
104
  Contributions are welcome
@@ -89,7 +107,7 @@ Contributions are welcome
89
107
  The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
90
108
 
91
109
  ### TODO
92
- * POST requests
93
- * Create generators (for config initializer first)
110
+ * Summary file (.md), to show response time for each endpoint and compare with others to track the slowest
111
+ * POST requests handling
94
112
  * Add simplecov to permit controller coverage for example
95
- * Generate documentation page(s) (markdown) to list the results
113
+ * Document configuration template file
@@ -0,0 +1,22 @@
1
+ module RailsApiBenchmark
2
+ module Generators
3
+ # rails g kaminari:config
4
+ class ConfigGenerator < Rails::Generators::Base # :nodoc:
5
+ source_root File.expand_path(
6
+ File.join(
7
+ File.dirname(__FILE__),
8
+ 'templates'
9
+ )
10
+ )
11
+
12
+ desc <<-DESC.strip_heredoc
13
+ Description:
14
+ Copies RailsApiBenchmark configuration to your app initializers folder
15
+ DESC
16
+ def copy_config_file
17
+ template 'rails_api_benchmark_config.rb',
18
+ 'config/initializers/rails_api_benchmark_config.rb'
19
+ end
20
+ end
21
+ end
22
+ end
@@ -0,0 +1,41 @@
1
+ # unless Rails.env.production?
2
+ RailsApiBenchmark.configure do |config|
3
+ # config.concurrency = 2
4
+ # config.host = 'localhost:5000'
5
+ # config.nb_requests = 1000
6
+ # config.results_folder = 'benchmark'
7
+ # config.auth_header = 'Authorization: Token token=benchToken'
8
+ # Use only if you want to log the responses
9
+ # config.curl_cmd = 'curl -H "%{auth_header}" http://%{host}%{route}'
10
+ # Keep plot.tsv to render graph
11
+ # config.bench_cmd = 'ab -n %{nb_requests} -c %{concurrency} -g plot.tsv' \
12
+ # ' -H "%{auth_header}" http://%{host}%{route}'
13
+ # config.server_cmd = 'bundle exec puma'
14
+ # config.env_vars = {
15
+ # 'RAILS_MAX_THREADS' => '2',
16
+ # 'SECRET_KEY_BASE' => 'bench',
17
+ # 'RAILS_ENV' => 'production',
18
+ # 'PORT' => '5000'
19
+ # }
20
+ # config.regexps = [
21
+ # {
22
+ # key: :response_time,
23
+ # name: 'Average time per request (ms)',
24
+ # regexp: /Time\s+per\s+request:\s+([0-9.]*).*\(mean\)/
25
+ # }, {
26
+ # key: :req_per_sec,
27
+ # name: 'Requests per second (#)',
28
+ # regexp: /Requests\s+per\s+second:\s+([0-9.]*).*\(mean\)/
29
+ # }
30
+ # ]
31
+ config.routes = [
32
+ {
33
+ name: 'todo_list',
34
+ route: '/todos',
35
+ method: :get,
36
+ title: 'GET /todos',
37
+ description: 'Get first page of todos (default 25 per page)'
38
+ }
39
+ ].freeze
40
+ end
41
+ # end
@@ -13,6 +13,15 @@ module RailsApiBenchmark
13
13
 
14
14
  at_exit { RailsApiBenchmark::Subprocess.kill_all }
15
15
  end
16
+
17
+ namespace :benchmark do
18
+ desc 'Prints RailsApiBenchmark config'
19
+ task config: :environment do
20
+ require 'json'
21
+
22
+ puts JSON.pretty_generate(RailsApiBenchmark.config.all)
23
+ end
24
+ end
16
25
  end
17
26
  end
18
27
  end
@@ -1,3 +1,3 @@
1
1
  module RailsApiBenchmark
2
- VERSION = '0.2.1'.freeze
2
+ VERSION = '0.2.2'.freeze
3
3
  end
@@ -8,10 +8,42 @@ require 'rails_api_benchmark/endpoint'
8
8
  require 'rails_api_benchmark/views/view' # Requires all the views
9
9
 
10
10
  module RailsApiBenchmark
11
- class Config
11
+ class Configuration
12
12
  attr_accessor :concurrency, :nb_requests, :auth_header,
13
13
  :server_cmd, :bench_cmd, :curl_cmd, :results_folder,
14
14
  :regexps, :env_vars, :routes, :host
15
+
16
+ # Default values, INSANE. Must be refactored
17
+ # Maybe create a yaml or json file to import for default values
18
+ def initialize
19
+ self.concurrency = 2
20
+ self.nb_requests = 1000
21
+ self.server_cmd = 'bundle exec puma'
22
+ self.bench_cmd = 'ab -n %{nb_requests} -c %{concurrency} -g plot.tsv' \
23
+ ' -H "%{auth_header}" http://%{host}%{route}'
24
+ self.curl_cmd = 'curl -H "%{auth_header}" http://%{host}%{route}'
25
+ self.results_folder = 'benchmark'
26
+ self.regexps = [
27
+ {
28
+ key: :response_time,
29
+ name: 'Average time per request (ms)',
30
+ regexp: /Time\s+per\s+request:\s+([0-9.]*).*\(mean\)/
31
+ }, {
32
+ key: :req_per_sec,
33
+ name: 'Requests per second (#)',
34
+ regexp: /Requests\s+per\s+second:\s+([0-9.]*).*\(mean\)/
35
+ }
36
+ ]
37
+ self.env_vars = {
38
+ 'RAILS_MAX_THREADS' => 2,
39
+ 'SSL_DISABLE' => true,
40
+ 'SECRET_KEY_BASE' => '123',
41
+ 'PORT' => 5000
42
+ }
43
+ self.routes = []
44
+ self.host = 'localhost:5000'
45
+ end
46
+
15
47
  def all
16
48
  instance_variables.inject({}) do |h, v|
17
49
  var = v.to_s.sub('@', '')
@@ -24,7 +56,7 @@ module RailsApiBenchmark
24
56
  attr_writer :config
25
57
 
26
58
  def config
27
- @config ||= Config.new
59
+ @config ||= Configuration.new
28
60
  end
29
61
 
30
62
  def configure
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_api_benchmark
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Terry Raimondo
@@ -56,6 +56,8 @@ files:
56
56
  - README.md
57
57
  - Rakefile
58
58
  - gnuplotscript
59
+ - lib/generators/rails_api_benchmark/config_generator.rb
60
+ - lib/generators/rails_api_benchmark/templates/rails_api_benchmark_config.rb
59
61
  - lib/rails_api_benchmark.rb
60
62
  - lib/rails_api_benchmark/benchmark_tasks.rb
61
63
  - lib/rails_api_benchmark/core.rb