api_bomb 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +3 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +229 -0
- data/Rakefile +2 -0
- data/api_bomb.gemspec +31 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/api_bomb/army.rb +12 -0
- data/lib/api_bomb/commander.rb +67 -0
- data/lib/api_bomb/fighter.rb +27 -0
- data/lib/api_bomb/lambda_hash.rb +41 -0
- data/lib/api_bomb/path.rb +90 -0
- data/lib/api_bomb/signaler.rb +39 -0
- data/lib/api_bomb/strategy.rb +35 -0
- data/lib/api_bomb/version.rb +3 -0
- data/lib/api_bomb.rb +101 -0
- metadata +148 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 73ddb47d82a289fdcb31bdc0416cb4a9cf2ad9d1
|
4
|
+
data.tar.gz: 4e3a67a97ef22c4d09f386dc43b799145e9f2022
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9dc76d94c2f5a75d15058100e5ce9f4f66d3b1d1ef62cbedc436b326fe4f37cc02e0535cc767c5d98b2c503b2d106f747eba1e81ff023fdbe80f42b35b86bfb9
|
7
|
+
data.tar.gz: a1493c33637c339c0d4e49c51b70f248dd1e2c746fff81b2fd25e5b6dbf32f753d32223b8559a662b897f6f8775e1602160ad232ffa090d6d02c08ec16a29b70
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Filippos Vasilakis
|
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,229 @@
|
|
1
|
+
# ApiBomb
|
2
|
+
After investigating all ruby gems and commercial services for testing my API performance,
|
3
|
+
I figured out that none of those was easy to use. Most of them were not built with
|
4
|
+
APIs in mind, were difficult to create a simple test, even more tricky to add
|
5
|
+
more sophisticated requests and paid services didn't work for localhost.
|
6
|
+
|
7
|
+
All failed to my request: test and measure my API performance without spending
|
8
|
+
too much time in this shit.
|
9
|
+
|
10
|
+
So I built my own gem.
|
11
|
+
Started as a funny gist, ended up as a fully fledged gem.
|
12
|
+
|
13
|
+
ApiBomb will allow you to test how much your API can take. It will start firing
|
14
|
+
as many requests as you want for any timespan you want, all of them fully customizable.
|
15
|
+
|
16
|
+
Are you ready to defend your API?
|
17
|
+
|
18
|
+
|
19
|
+
## Installation
|
20
|
+
|
21
|
+
Add this line to your application's Gemfile:
|
22
|
+
|
23
|
+
```ruby
|
24
|
+
gem 'api_bomb'
|
25
|
+
```
|
26
|
+
|
27
|
+
And then execute:
|
28
|
+
|
29
|
+
$ bundle
|
30
|
+
|
31
|
+
Or install it yourself as:
|
32
|
+
|
33
|
+
$ gem install api_bomb
|
34
|
+
|
35
|
+
## Usage
|
36
|
+
First you need to define the global settings.
|
37
|
+
Usually in the global settings hash you just want to define the most common settings.
|
38
|
+
|
39
|
+
```ruby
|
40
|
+
options = {
|
41
|
+
fronts: 4, #concurrent users
|
42
|
+
duration: 60, #seconds
|
43
|
+
base_url: 'http://localhost:3000/api/v1', #base url
|
44
|
+
options: { #this hash is overriden for each path that has its own options
|
45
|
+
headers: { #various headers, like session token etc
|
46
|
+
'Session-Token' => SESSION_TOKEN,
|
47
|
+
'Connection' => 'close',
|
48
|
+
'Content-Type' => 'application/json'
|
49
|
+
},
|
50
|
+
params: { #GET params
|
51
|
+
},
|
52
|
+
json: { #POST/PUT/PATCH HTTP body params
|
53
|
+
}
|
54
|
+
},
|
55
|
+
}
|
56
|
+
```
|
57
|
+
Options keys (like headers, params, json etc) can be overrided later, if needed, per path.
|
58
|
+
Oh btw those options are passed straight to [http](https://github.com/httprb/http) which
|
59
|
+
means you can do anything this gem provides :)
|
60
|
+
|
61
|
+
### Simple mode
|
62
|
+
###### You want to test how many GET rpm an endpoint can hold?
|
63
|
+
```ruby
|
64
|
+
path = 'videos'
|
65
|
+
ApiBomb::War.new(options.merge({path: path})).start!
|
66
|
+
```
|
67
|
+
This will fire tons of requests in the `http://localhost:3000/api/v1/videos`
|
68
|
+
using the headers from the global settings for 60 seconds. 4 concurrent users
|
69
|
+
means that they will always be 4 requests pending from the client perspective.
|
70
|
+
As soon as one is served, a new one will be fired. Once the test duration is
|
71
|
+
elapsed it will report back to you:
|
72
|
+
|
73
|
+
```ruby
|
74
|
+
Elapsed time: 60
|
75
|
+
Concurrency: 4 threads
|
76
|
+
Number of requests: 298
|
77
|
+
Requests per second: 4
|
78
|
+
Requests per minute: 298.0
|
79
|
+
Average response time: 0.6628006505031939
|
80
|
+
Standard deviation: 0.2957473991283155
|
81
|
+
Percentile 90th: 1.0887710852002783
|
82
|
+
Percentile 95th: 1.154147314250258
|
83
|
+
Percentile 99th: 1.5413802972278787
|
84
|
+
Server status stats: [{"2xx"=>298}]
|
85
|
+
```
|
86
|
+
You can also inject your logger in the global options.
|
87
|
+
|
88
|
+
|
89
|
+
###### You want to test how many GET rpm a sequence of endpoints can hold? It will run
|
90
|
+
a separate benchmark for each of your endpoints.
|
91
|
+
```ruby
|
92
|
+
paths = ['videos', 'users', 'comments']
|
93
|
+
ApiBomb::War.new(options.merge({paths: paths})).start!
|
94
|
+
```
|
95
|
+
This will fire tons of requests like in the previous example,
|
96
|
+
first in `http://localhost:3000/api/v1/videos`
|
97
|
+
then in `http://localhost:3000/api/v1/users` and finally in
|
98
|
+
`http://localhost:3000/api/v1/comments`
|
99
|
+
|
100
|
+
###### You want to test a POST endpoint? Then you can the slightly more advanced API:
|
101
|
+
```ruby
|
102
|
+
video_params = { title: 'A new video!', description: 'a new description!', user_id: 1}
|
103
|
+
paths = {path: 'videos', action: :post, options: { json: video_params}}
|
104
|
+
ApiBomb::War.new(options.merge({paths: paths})).start!
|
105
|
+
```
|
106
|
+
|
107
|
+
###### You want to test dynamic endpoints?
|
108
|
+
```ruby
|
109
|
+
paths = {path: Proc.new{ "videos/#{1.upto(10000).to_a.sample}" } #default action is :get
|
110
|
+
ApiBomb::War.new(options.merge({paths: paths})).start!
|
111
|
+
```
|
112
|
+
You can add lambdas or (preferrably) procs in any value of the options hash and
|
113
|
+
path.
|
114
|
+
|
115
|
+
It should be noted that using lambdas/procs, it's almost endless of what you can do.
|
116
|
+
You could use FactoryGirl or whatever to get dynamic attributes when creating/updating
|
117
|
+
a resource in your API. **Be sure to watch out though, that constants and classes have already
|
118
|
+
been initialized because most of such gems do lazy initialization.**
|
119
|
+
|
120
|
+
###### You want to test a sequence of endpoints with dynamic params? It will run a separate
|
121
|
+
benchmark for each one in the array.
|
122
|
+
```ruby
|
123
|
+
paths = [ #get http method is used by default
|
124
|
+
{
|
125
|
+
path: 'videos',
|
126
|
+
params: { per_page: Proc.new{ 1.upto(10).to_a.sample } } #you can have a proc in a specific param only
|
127
|
+
},
|
128
|
+
{
|
129
|
+
path: 'videos',
|
130
|
+
params: { per_page: Proc.new{ 1.upto(10).to_a.sample } } #you can have a proc in a specific param only
|
131
|
+
},
|
132
|
+
{
|
133
|
+
path: 'videos',
|
134
|
+
params: { per_page: Proc.new{ 1.upto(10).to_a.sample } } #you can have a proc in a specific param only
|
135
|
+
},
|
136
|
+
{
|
137
|
+
path: 'videos',
|
138
|
+
params: { per_page: Proc.new{ 1.upto(10).to_a.sample } } #you can have a proc in a specific param only
|
139
|
+
},
|
140
|
+
]
|
141
|
+
```
|
142
|
+
|
143
|
+
###### You want to test dynamic endpoints with dynamic params?
|
144
|
+
```ruby
|
145
|
+
paths = {
|
146
|
+
path: Proc.new{ ['videos', 'users', 'comments', 'likes'].sample,
|
147
|
+
action: :get,
|
148
|
+
params: Proc.new{ {per_page: 1.upto(10).to_a.sample} }
|
149
|
+
}
|
150
|
+
ApiBomb::War.new(options.merge({paths: paths})).start!
|
151
|
+
```
|
152
|
+
|
153
|
+
This is also equivelent to the previous:
|
154
|
+
```ruby
|
155
|
+
paths = {
|
156
|
+
path: Proc.new{ ['videos', 'users', 'comments', 'likes'].sample,
|
157
|
+
action: :get,
|
158
|
+
params: { per_page: Proc.new{ 1.upto(10).to_a.sample } } #you can have a proc in a specific param only
|
159
|
+
}
|
160
|
+
ApiBomb::War.new(options.merge({paths: paths})).start!
|
161
|
+
```
|
162
|
+
|
163
|
+
###### You want to test different endpoints with each having different probability to be requested?
|
164
|
+
The dynamic nature is up to you. But for your convinience we have created a special
|
165
|
+
class that can be used if you want to test random paths using a probability/weight.
|
166
|
+
So if you want for bombard 'videos' path 3 times more than comments and comments path 3 times
|
167
|
+
more users path (so videos will be bombarded 9 times more than users path) in a war (test):
|
168
|
+
|
169
|
+
```ruby
|
170
|
+
paths = ApiBomb::Path::Weighted.new({
|
171
|
+
{path: 'videos'} => 9,
|
172
|
+
{path: 'comments'} => 3,
|
173
|
+
{path: 'users'} => 1,
|
174
|
+
})
|
175
|
+
|
176
|
+
ApiBomb::War.new(options.merge({paths: paths})).start!
|
177
|
+
```
|
178
|
+
|
179
|
+
or a more spophisticated:
|
180
|
+
```ruby
|
181
|
+
paths = ApiBomb::Path::Weighted.new({
|
182
|
+
{path: 'videos', action: :get, options: { params: {per_page: 1}}} => 20,
|
183
|
+
{path: 'videos', action: :get, options: {
|
184
|
+
params: { per_page: Proc.new{ 1.upto(10).to_a.sample } }
|
185
|
+
}} => 10,
|
186
|
+
{path: Proc.new{ "videos/#{video_ids.sample}" }, action: :get} => 70,
|
187
|
+
{path: Proc.new{ "users/#{user_ids.sample}" }, action: :get} => 60,
|
188
|
+
})
|
189
|
+
|
190
|
+
ApiBomb::War.new(options.merge({paths: paths})).start!
|
191
|
+
```
|
192
|
+
|
193
|
+
`Weighted` path uses [Pickup](https://github.com/fl00r/pickup) gem underneath.
|
194
|
+
|
195
|
+
## Advanced
|
196
|
+
You can have lambdas/procs in any hash value (or key for `Weighted` paths).
|
197
|
+
Internally `Path::Single`, `Path::Sequence` and `Path::Weighted` classes are
|
198
|
+
used everywhere, which you can also use
|
199
|
+
but I have ommitted them from the examples for the sake of simplicity (and added
|
200
|
+
a builder that figures out what you actually want).
|
201
|
+
|
202
|
+
You can also create your own Path structure. It only needs to respond to pick
|
203
|
+
method which must return a Path::Single object with all the necessary attributes.
|
204
|
+
|
205
|
+
Furthermore, if you like, you can override the default strategy. You might want to
|
206
|
+
dispatch a new request depending on the response of the previous request. Take
|
207
|
+
a look on `lib/api_bomb/strategies.rb`
|
208
|
+
|
209
|
+
On the global settings hash, you can also specify the number of requests you want
|
210
|
+
to send (which will override the duration unless Timeout exception kicks in first).
|
211
|
+
It's not very well tested and should be used mostly for debugging (requests: 1)
|
212
|
+
|
213
|
+
## Best practices
|
214
|
+
* When starting optimizing, be sure that you test the performance of your API and
|
215
|
+
not of your webserver. For instance, you might fire 2 concurrent users in an
|
216
|
+
endpoint which will result in 300 req/min. Then you might fire 20 concurrent users
|
217
|
+
which could result in 450 req/min. Does it mean that your API is faster? Probably
|
218
|
+
not because your response time must have went > 1 sec which sucks.
|
219
|
+
|
220
|
+
That's why other statistics are included apart from req/min.
|
221
|
+
|
222
|
+
|
223
|
+
## Contributing
|
224
|
+
|
225
|
+
1. Fork it ( https://github.com/vasilakisfil/api_bomb/fork )
|
226
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
227
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
228
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
229
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
data/api_bomb.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 'api_bomb/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "api_bomb"
|
8
|
+
spec.version = ApiBomb::VERSION
|
9
|
+
spec.authors = ["Filippos Vasilakis"]
|
10
|
+
spec.email = ["vasilakisfil@gmail.com"]
|
11
|
+
|
12
|
+
if spec.respond_to?(:metadata)
|
13
|
+
end
|
14
|
+
|
15
|
+
spec.summary = %q{Simple gem that allows you to test/measure your API performance.}
|
16
|
+
spec.description = %q{Simple gem that allows you to test/measure your API performance.}
|
17
|
+
spec.homepage = "https://github.com/vasilakisfil/api_bomb"
|
18
|
+
spec.license = "MIT"
|
19
|
+
|
20
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
22
|
+
spec.require_paths = ["lib"]
|
23
|
+
|
24
|
+
spec.add_dependency "celluloid", "~> 0.17"
|
25
|
+
spec.add_dependency "http", "~> 0.9"
|
26
|
+
spec.add_dependency "pickup", "~> 0.0.11"
|
27
|
+
spec.add_dependency "descriptive_statistics", "~> 2.5"
|
28
|
+
|
29
|
+
spec.add_development_dependency "bundler", "~> 1.8"
|
30
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
31
|
+
end
|
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "api_bomb"
|
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,67 @@
|
|
1
|
+
class ApiBomb::Commander
|
2
|
+
include ApiBomb::Strategy::Naive
|
3
|
+
|
4
|
+
attr_reader :army, :fronts, :duration, :signaler, :logger, :requests
|
5
|
+
|
6
|
+
def initialize(army:, fronts: 1, duration: 10, logger: Logger.new(STDOUT), requests: nil)
|
7
|
+
@duration = duration
|
8
|
+
@army = army
|
9
|
+
@fighters = []
|
10
|
+
@statuses = []
|
11
|
+
@hold_times = []
|
12
|
+
@fronts = fronts
|
13
|
+
@logger = logger
|
14
|
+
@requests = requests
|
15
|
+
end
|
16
|
+
|
17
|
+
def start_attack!
|
18
|
+
begin
|
19
|
+
#I know that Timeout is really bad
|
20
|
+
#but literarly there is no other generic way doing this
|
21
|
+
#Fortunately we only do requests to an API so it shouldn't affect us
|
22
|
+
logger.info "Starts firing requests"
|
23
|
+
Timeout::timeout(duration) {
|
24
|
+
attack
|
25
|
+
}
|
26
|
+
rescue Timeout::Error
|
27
|
+
logger.info "Ceasefire!"
|
28
|
+
end
|
29
|
+
|
30
|
+
report_attack_result
|
31
|
+
end
|
32
|
+
|
33
|
+
private
|
34
|
+
def report_attack_result
|
35
|
+
logger.info "Elapsed time: #{attack_result[:duration]}"
|
36
|
+
logger.info "Concurrency: #{attack_result[:fronts]} threads"
|
37
|
+
logger.info "Number of requests: #{attack_result[:requests]}"
|
38
|
+
logger.info "Requests per second: #{attack_result[:rps]}"
|
39
|
+
logger.info "Requests per minute: #{attack_result[:rpm]}"
|
40
|
+
logger.info "Average response time: #{attack_result[:average_rt]}"
|
41
|
+
logger.info "Standard deviation: #{attack_result[:sd_rq_time]}"
|
42
|
+
logger.info "Percentile 90th: #{attack_result[:percentile_90]}"
|
43
|
+
logger.info "Percentile 95th: #{attack_result[:percentile_95]}"
|
44
|
+
logger.info "Percentile 99th: #{attack_result[:percentile_99]}"
|
45
|
+
logger.info "Server status stats: #{attack_result[:server_status_stats]}"
|
46
|
+
end
|
47
|
+
|
48
|
+
def attack_result
|
49
|
+
{
|
50
|
+
duration: duration,
|
51
|
+
fronts: @fronts,
|
52
|
+
requests: @signaler.fighters_lost,
|
53
|
+
rps: @signaler.fighters_lost / duration,
|
54
|
+
rpm: @signaler.fighters_lost / (duration/60.0),
|
55
|
+
average_rt: @signaler.mean_hold_time,
|
56
|
+
sd_rq_time: @signaler.sd_time,
|
57
|
+
percentile_90: @signaler.percentile(90),
|
58
|
+
percentile_95: @signaler.percentile(95),
|
59
|
+
percentile_99: @signaler.percentile(99),
|
60
|
+
server_status_stats: @signaler.server_status_stats
|
61
|
+
}
|
62
|
+
end
|
63
|
+
|
64
|
+
def signaler
|
65
|
+
@signaler ||= ApiBomb::Signaler.new
|
66
|
+
end
|
67
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
class ApiBomb::Fighter
|
2
|
+
include Celluloid
|
3
|
+
|
4
|
+
attr_reader :paths, :options, :base_url
|
5
|
+
|
6
|
+
def initialize(paths: nil, base_url: nil, options: {})
|
7
|
+
@base_url = base_url
|
8
|
+
@options = options
|
9
|
+
@paths = paths
|
10
|
+
end
|
11
|
+
|
12
|
+
def fire
|
13
|
+
route = @paths.pick
|
14
|
+
|
15
|
+
url = URI::join(base_url, route.path)
|
16
|
+
response = nil
|
17
|
+
hold_time = Benchmark.measure do
|
18
|
+
response = HTTP.send(
|
19
|
+
route.action,
|
20
|
+
url,
|
21
|
+
ApiBomb::LambdaHash.hasharize(options.merge(route.options))
|
22
|
+
)
|
23
|
+
end
|
24
|
+
|
25
|
+
return OpenStruct.new(response: response, hold_time: hold_time.real)
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
#values can respond to call making them dynamic
|
2
|
+
class ApiBomb::LambdaHash < SimpleDelegator
|
3
|
+
def self.hasharize(hash)
|
4
|
+
hash_call = self.new(hash)
|
5
|
+
h = {}
|
6
|
+
hash_call.each do |v, k|
|
7
|
+
h[v] = hash_call[v]
|
8
|
+
if h[v].is_a? self
|
9
|
+
h[v] = self.hasharize(h[v])
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
return h
|
14
|
+
end
|
15
|
+
|
16
|
+
def is_lambda?
|
17
|
+
self.each do |k,v|
|
18
|
+
if self[k].is_a? self.class
|
19
|
+
return self[k].is_lambda?
|
20
|
+
else
|
21
|
+
if self.real[k].respond_to? :call
|
22
|
+
return true
|
23
|
+
else
|
24
|
+
return false
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def [](key)
|
31
|
+
value = self.__getobj__[key]
|
32
|
+
value = value.call if value.respond_to? :call
|
33
|
+
value = self.class.new(value) if value.is_a? Hash
|
34
|
+
|
35
|
+
return value
|
36
|
+
end
|
37
|
+
|
38
|
+
def real
|
39
|
+
self.__getobj__
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,90 @@
|
|
1
|
+
module ApiBomb::Path
|
2
|
+
class Sequence
|
3
|
+
include Enumerable
|
4
|
+
extend Forwardable
|
5
|
+
def_delegators :@paths, :each, :<<
|
6
|
+
|
7
|
+
attr_reader :paths
|
8
|
+
def initialize(paths)
|
9
|
+
@paths = paths
|
10
|
+
end
|
11
|
+
|
12
|
+
def pick
|
13
|
+
return paths.sample
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
class Single
|
18
|
+
HTTP_METHODS = [:get, :post, :put, :patch, :delete, :head].freeze
|
19
|
+
|
20
|
+
def initialize(path:, action: :get, options: {})
|
21
|
+
@path = path
|
22
|
+
@action = action
|
23
|
+
@action = :get if not HTTP_METHODS.include?(action.downcase.to_sym)
|
24
|
+
@options = ApiBomb::LambdaHash.new(options)
|
25
|
+
end
|
26
|
+
|
27
|
+
def pick
|
28
|
+
return self
|
29
|
+
end
|
30
|
+
|
31
|
+
def report(base_url)
|
32
|
+
path_report(base_url) + params_report
|
33
|
+
end
|
34
|
+
|
35
|
+
def path_report(base_url = '')
|
36
|
+
base_url = options[:base_url] if options[:base_url]
|
37
|
+
|
38
|
+
if @path.respond_to? :call
|
39
|
+
"testing random paths like #{action.to_s.upcase} #{URI::join(base_url, path)}"
|
40
|
+
else
|
41
|
+
"testing path #{action.to_s.upcase} #{URI::join(base_url, path)}"
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def params_report
|
46
|
+
if options.is_lambda?
|
47
|
+
" with random params like: #{ApiBomb::LambdaHash.hasharize(options)[:params]}"
|
48
|
+
else
|
49
|
+
" with params: #{options[:params]}"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def path
|
54
|
+
return @path.call if @path.respond_to? :call
|
55
|
+
return @path
|
56
|
+
end
|
57
|
+
|
58
|
+
def action
|
59
|
+
return @action.call if @action.respond_to? :call
|
60
|
+
return @action
|
61
|
+
end
|
62
|
+
|
63
|
+
def options
|
64
|
+
@options
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
class Weighted
|
69
|
+
attr_reader :weighted_paths, :paths
|
70
|
+
def initialize(paths)
|
71
|
+
@weighted_paths = Pickup.new(paths)
|
72
|
+
@paths = paths
|
73
|
+
end
|
74
|
+
|
75
|
+
def pick
|
76
|
+
Single.new(@weighted_paths.pick)
|
77
|
+
end
|
78
|
+
|
79
|
+
def report
|
80
|
+
sum = paths.values.sum
|
81
|
+
|
82
|
+
str = "Load generation over random (weighted) urls \n"
|
83
|
+
paths.sort_by {|_key, value| value}.to_h.each do |path, weight|
|
84
|
+
str += "#{path} with probability #{weight/sum} \n"
|
85
|
+
end
|
86
|
+
|
87
|
+
return str
|
88
|
+
end
|
89
|
+
end
|
90
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
class ApiBomb::Signaler
|
2
|
+
attr_reader :hold_times, :statuses
|
3
|
+
|
4
|
+
def initialize
|
5
|
+
@statuses = []
|
6
|
+
@hold_times = []
|
7
|
+
end
|
8
|
+
|
9
|
+
def report(fighter)
|
10
|
+
@statuses << fighter.value.response.status
|
11
|
+
@hold_times << fighter.value.hold_time
|
12
|
+
end
|
13
|
+
|
14
|
+
def fighters_lost
|
15
|
+
hold_times.count
|
16
|
+
end
|
17
|
+
|
18
|
+
def mean_hold_time
|
19
|
+
hold_times.mean
|
20
|
+
end
|
21
|
+
|
22
|
+
def sd_time
|
23
|
+
hold_times.standard_deviation
|
24
|
+
end
|
25
|
+
|
26
|
+
def percentile(value)
|
27
|
+
hold_times.percentile(value)
|
28
|
+
end
|
29
|
+
|
30
|
+
def server_errors
|
31
|
+
@statuses.select{|s| s >= 500}.count
|
32
|
+
end
|
33
|
+
|
34
|
+
def server_status_stats
|
35
|
+
@server_status_stats ||= @statuses.group_by{|status|
|
36
|
+
status.to_i.to_s.chop.chop.insert(-1, 'xx')
|
37
|
+
}.map{|s| {s.first => s.last.count}}
|
38
|
+
end
|
39
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module ApiBomb::Strategy
|
2
|
+
module Naive
|
3
|
+
def attack
|
4
|
+
if requests
|
5
|
+
attack_in_requests
|
6
|
+
else
|
7
|
+
attack_in_time
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
def attack_in_time
|
12
|
+
fronts.times do |i|
|
13
|
+
@fighters << @army.send_fighter
|
14
|
+
end
|
15
|
+
|
16
|
+
while (@fighters.length > 0) do
|
17
|
+
signaler.report(@fighters[0])
|
18
|
+
@fighters.shift
|
19
|
+
|
20
|
+
@fighters << @army.send_new_fighter
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def attack_in_requests
|
25
|
+
requests.times do |i|
|
26
|
+
@fighters << @army.send_fighter
|
27
|
+
end
|
28
|
+
|
29
|
+
while (@fighters.length > 0) do
|
30
|
+
signaler.report(@fighters[0])
|
31
|
+
@fighters.shift
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/api_bomb.rb
ADDED
@@ -0,0 +1,101 @@
|
|
1
|
+
require_relative "api_bomb/version"
|
2
|
+
require "benchmark"
|
3
|
+
require 'celluloid/current'
|
4
|
+
require 'http'
|
5
|
+
require 'pickup'
|
6
|
+
require 'pry'
|
7
|
+
require 'descriptive_statistics'
|
8
|
+
require 'descriptive_statistics/safe'
|
9
|
+
require 'forwardable'
|
10
|
+
|
11
|
+
require_relative 'api_bomb/lambda_hash'
|
12
|
+
require_relative 'api_bomb/strategy'
|
13
|
+
require_relative 'api_bomb/path'
|
14
|
+
|
15
|
+
module ApiBomb
|
16
|
+
class War
|
17
|
+
attr_reader :fronts, :duration, :paths, :options, :base_url, :logger,
|
18
|
+
:requests
|
19
|
+
#alias_method :path, :paths
|
20
|
+
|
21
|
+
def initialize(opts = {})
|
22
|
+
@fronts = opts[:fronts] || 2
|
23
|
+
@duration = opts[:duration] || 10
|
24
|
+
@paths = opts[:paths]
|
25
|
+
@paths = (opts[:path] || '') if @paths.blank?
|
26
|
+
@options = LambdaHash.new(opts[:options] || {})
|
27
|
+
@base_url = opts[:base_url] || ''
|
28
|
+
@logger = opts[:logger] || Logger.new(STDOUT)
|
29
|
+
@requests = opts[:requests]
|
30
|
+
build_paths
|
31
|
+
end
|
32
|
+
|
33
|
+
def build_paths
|
34
|
+
case @paths
|
35
|
+
when String
|
36
|
+
@paths = Path::Single.new(path: @paths)
|
37
|
+
when Array
|
38
|
+
tmp_paths = []
|
39
|
+
@paths.each do |path|
|
40
|
+
if path.is_a? Hash
|
41
|
+
tmp_paths << Path::Single.new(path)
|
42
|
+
elsif path.is_a? String
|
43
|
+
tmp_paths << Path::Single.new(path: path)
|
44
|
+
else
|
45
|
+
raise 'Unknown path structure'
|
46
|
+
end
|
47
|
+
end
|
48
|
+
@paths = Path::Sequence.new(tmp_paths)
|
49
|
+
when Hash
|
50
|
+
@paths = Path::Single.new(@paths)
|
51
|
+
when Path::Single, Path::Sequence, Path::Weighted
|
52
|
+
else
|
53
|
+
raise 'Unknown path structure'
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
def start!
|
58
|
+
case paths
|
59
|
+
when Path::Single
|
60
|
+
@logger.info("#{paths.report(base_url)}, duration: #{duration} sec")
|
61
|
+
start_attack!(paths)
|
62
|
+
when Path::Sequence
|
63
|
+
paths.each do |path|
|
64
|
+
@logger.info("#{path.report(base_url)}, duration: #{duration} sec")
|
65
|
+
start_attack!(path)
|
66
|
+
end
|
67
|
+
when Path::Weighted
|
68
|
+
@logger.info(paths.report)
|
69
|
+
start_attack!(paths)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
def dereference_path(path)
|
74
|
+
return path.call if path.respond_to? :call
|
75
|
+
return path[:path] if path.respond_to? :[] && path[:path] != nil
|
76
|
+
path
|
77
|
+
end
|
78
|
+
|
79
|
+
def start_attack!(testing_paths)
|
80
|
+
Commander.new(
|
81
|
+
fronts: fronts,
|
82
|
+
duration: duration,
|
83
|
+
army: Army.new(
|
84
|
+
fighters: Fighter.pool(
|
85
|
+
size: 2*fronts, args: [
|
86
|
+
paths: testing_paths, base_url: base_url, options: options
|
87
|
+
]
|
88
|
+
)
|
89
|
+
),
|
90
|
+
logger: logger,
|
91
|
+
requests: requests
|
92
|
+
).start_attack!
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
end
|
97
|
+
|
98
|
+
require_relative 'api_bomb/army'
|
99
|
+
require_relative 'api_bomb/fighter'
|
100
|
+
require_relative 'api_bomb/commander'
|
101
|
+
require_relative 'api_bomb/signaler'
|
metadata
ADDED
@@ -0,0 +1,148 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: api_bomb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Filippos Vasilakis
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-12-18 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: celluloid
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0.17'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0.17'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: http
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0.9'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0.9'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: pickup
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 0.0.11
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 0.0.11
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: descriptive_statistics
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '2.5'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '2.5'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: bundler
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '1.8'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '1.8'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rake
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '10.0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '10.0'
|
97
|
+
description: Simple gem that allows you to test/measure your API performance.
|
98
|
+
email:
|
99
|
+
- vasilakisfil@gmail.com
|
100
|
+
executables: []
|
101
|
+
extensions: []
|
102
|
+
extra_rdoc_files: []
|
103
|
+
files:
|
104
|
+
- ".gitignore"
|
105
|
+
- ".rspec"
|
106
|
+
- ".travis.yml"
|
107
|
+
- CODE_OF_CONDUCT.md
|
108
|
+
- Gemfile
|
109
|
+
- LICENSE.txt
|
110
|
+
- README.md
|
111
|
+
- Rakefile
|
112
|
+
- api_bomb.gemspec
|
113
|
+
- bin/console
|
114
|
+
- bin/setup
|
115
|
+
- lib/api_bomb.rb
|
116
|
+
- lib/api_bomb/army.rb
|
117
|
+
- lib/api_bomb/commander.rb
|
118
|
+
- lib/api_bomb/fighter.rb
|
119
|
+
- lib/api_bomb/lambda_hash.rb
|
120
|
+
- lib/api_bomb/path.rb
|
121
|
+
- lib/api_bomb/signaler.rb
|
122
|
+
- lib/api_bomb/strategy.rb
|
123
|
+
- lib/api_bomb/version.rb
|
124
|
+
homepage: https://github.com/vasilakisfil/api_bomb
|
125
|
+
licenses:
|
126
|
+
- MIT
|
127
|
+
metadata: {}
|
128
|
+
post_install_message:
|
129
|
+
rdoc_options: []
|
130
|
+
require_paths:
|
131
|
+
- lib
|
132
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
133
|
+
requirements:
|
134
|
+
- - ">="
|
135
|
+
- !ruby/object:Gem::Version
|
136
|
+
version: '0'
|
137
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
138
|
+
requirements:
|
139
|
+
- - ">="
|
140
|
+
- !ruby/object:Gem::Version
|
141
|
+
version: '0'
|
142
|
+
requirements: []
|
143
|
+
rubyforge_project:
|
144
|
+
rubygems_version: 2.4.5
|
145
|
+
signing_key:
|
146
|
+
specification_version: 4
|
147
|
+
summary: Simple gem that allows you to test/measure your API performance.
|
148
|
+
test_files: []
|