|
1 | 1 | # QAToolKit.Engine.Bombardier |
2 | 2 |  |
3 | 3 |
|
4 | | -`QAToolKit.Engine.Bombardier` is a .NET standard library, which takes `IList<HttpTestRequest>` object and runs load tests with tool called [Bombardier](https://github.com/codesenberg/bombardier). |
| 4 | +`QAToolKit.Engine.Bombardier` is a .NET standard library, which takes `IEnumerable<HttpTestRequest>` object and runs load tests with tool called [Bombardier](https://github.com/codesenberg/bombardier). |
5 | 5 |
|
6 | 6 | Library is a thin wrapper, that generates requests and parses results so you can export them to whatever format you prefer. |
7 | 7 |
|
8 | 8 | Major features: |
9 | 9 |
|
10 | | -- Library takes `IList<HttpTestRequest>` object, which can be produced in your code or can be imported from other sources. One example can be QAToolKit Swagger library that can produce that object with many options. Check it out [here](https://github.com/qatoolkit/qatoolkit-source-swagger-net). |
| 10 | +- Library takes `IEnumerable<HttpTestRequest>` object, which can be produced in your code or can be imported from other sources. One example can be QAToolKit Swagger library that can produce that object with many options. Check it out [here](https://github.com/qatoolkit/qatoolkit-source-swagger-net). |
11 | 11 | - Generate a Bombardier report that can be exported to the format you want. |
12 | 12 |
|
| 13 | +Bombardier version [1.2.5](https://github.com/codesenberg/bombardier/releases/tag/v1.2.5) is used for `windows-amd64` and `linux-amd64`. |
| 14 | + |
13 | 15 | ## Sample |
14 | 16 |
|
15 | 17 | ```csharp |
| 18 | +//Generate requests from previously stored JSON file: |
| 19 | +var content = File.ReadAllText("Assets/getPetById.json"); |
| 20 | +var httpRequest = JsonConvert.DeserializeObject<IEnumerable<HttpRequest>>(content); |
16 | 21 |
|
17 | | -//Instantiate a Bombardier test generator, by specifying Bombardier options |
| 22 | +//Create bombardier tests generator |
18 | 23 | var bombardierTestsGenerator = new BombardierTestsGenerator(options => |
19 | 24 | { |
20 | | - //if your api endpoints you are testing are protected by Oauth2 access tokens |
21 | | - options.AddOAuth2Token("eyJhbGciOiJSUzI1N....", AuthenticationType.Customer); |
22 | | - //if your api endpoints are protected by simple "ApiKey: <apikey>" authentication header |
23 | | - options.AddApiKey("myAPIKey123423456"); |
24 | | - //if your api endpoints are protected by basic authentication |
25 | | - options.AddBasicAuthentication("username", "password"); |
26 | 25 | options.BombardierConcurrentUsers = 1; |
27 | 26 | options.BombardierDuration = 1; |
28 | 27 | options.BombardierTimeout = 30; |
29 | 28 | options.BombardierUseHttp2 = true; |
30 | 29 | }); |
31 | | -//Generate Bombardier Tests |
32 | | -var bombardierTests = await bombardierTestsGenerator.Generate(requests); |
| 30 | + |
| 31 | +//Generate bomardier tests |
| 32 | +var bombardierTests = await bombardierTestsGenerator.Generate(httpRequest); |
33 | 33 |
|
34 | 34 | //Run Bombardier Tests |
35 | | -var bombardierTestsRunner = new BombardierTestsRunner(bombardierTests.ToList(), |
36 | | - options => |
37 | | - { |
38 | | - options.ObfuscateAuthenticationHeader = false; |
39 | | - }); |
| 35 | +var bombardierTestsRunner = new BombardierTestsRunner(bombardierTests.ToList(), options => |
| 36 | +{ |
| 37 | + options.ObfuscateAuthenticationHeader = true; |
| 38 | +}); |
40 | 39 | var bombardierResults = await bombardierTestsRunner.Run(); |
41 | 40 | ``` |
42 | 41 |
|
@@ -80,27 +79,65 @@ Use `AddApiKey` if your APIs are protected by simple API Key. Pass Api Key in th |
80 | 79 | ##### 1.3 AddBasicAuthentication |
81 | 80 | Use `AddBasicAuthentication` if your APIs are protected by basic authentication. Pass username and password in the method. A basic `Authentication` HTTP header will be generated. This is not best practice, but it's here if you need it. |
82 | 81 |
|
83 | | -#### 2. Bombardier parameters |
| 82 | +##### 1.4 AddReplacementValues |
| 83 | +When you use `AddReplacementValues` values those can set or replace URL and HTTP body parameters before executing the tests. Replacement values have precedence over the `example` values that are set in Swagger file. |
84 | 84 |
|
85 | | -You can set 5 Bombardier properties: |
86 | | - |
87 | | -- `BombardierConcurrentUsers`: How many concurrent users should be used in Bombardier tests. |
88 | | -- `BombardierDuration`: How long the Bombardier tests should execute in seconds. Use this depending on the type of test you want to perform and should not be used with `BombardierRateLimit`. |
89 | | -- `BombardierTimeout`: What is the Bombardier timeout to wait for the requests to finish. |
90 | | -- `BombardierUseHttp2`: Use HTTP2? |
91 | | -- `BombardierRateLimit`: Rate limit Bombardier tests per second. Use this depending on the type of test you want to perform and should not be used with `BombardierDuration`. |
| 85 | +#### 2. Bombardier parameters |
92 | 86 |
|
| 87 | +You can also set those `BombardierGeneratorOptions` options: |
| 88 | + |
| 89 | +- `BombardierConcurrentUsers`: How many concurrent users should be used in Bombardier tests. Default is `3`. |
| 90 | +- `BombardierDuration`: How long the Bombardier tests should execute in seconds. Use this depending on the type of test you want to perform and should not be used with `BombardierRateLimit`. Default is `30` seconds. |
| 91 | +- `BombardierTimeout`: What is the Bombardier timeout to wait for the requests to finish. Default is `30` seconds. |
| 92 | +- `BombardierUseHttp2`: Use HTTP2 protocol. Otherwise HTTP1 is used. By default this is set to `true`. |
| 93 | +- `BombardierRateLimit`: Rate limit Bombardier tests per second. Use this depending on the type of test you want to perform and should not be used with `BombardierDuration`. By default rate limit is not set. |
| 94 | +- `BombardierNumberOfTotalRequests`: Limit the test to run only certain amount of requests. By default total number of requests is not set. |
| 95 | +- `BombardierInsecure`: Instead of HTTPS use HTTP protocol. Default value is `false`. |
| 96 | +- `BombardierBodyContentType`: Force only certain HTTP Content type. By default is set to `application/json`. |
| 97 | + |
93 | 98 | #### 3. Obfuscate Auth tokens for Bombardier output |
94 | 99 |
|
95 | 100 | Output is obfuscated by default, but you can turn it off with `options.ObfuscateAuthenticationHeader = false;` in `BombardierTestsRunner` options. |
96 | 101 |
|
97 | 102 | ## How to use |
98 | 103 |
|
99 | | -TO-DO |
| 104 | +In the sample code above we generate HTTP requests from previously generated object which was serialized to JSON. |
| 105 | + |
| 106 | +If you use Swagger files, you need to check the `QAToolKit.Source.Swagger` NuGet package, where you can generate that object from the Swagger file. |
| 107 | + |
| 108 | +Let's replace |
| 109 | + |
| 110 | +```csharp |
| 111 | +//Generate requests from previously stored JSON file: |
| 112 | +var content = File.ReadAllText("Assets/getPetById.json"); |
| 113 | +var httpRequest = JsonConvert.DeserializeObject<IEnumerable<HttpRequest>>(content); |
| 114 | +``` |
| 115 | + |
| 116 | +with |
| 117 | + |
| 118 | +```csharp |
| 119 | +//Setup Swagger source |
| 120 | +var urlSource = new SwaggerUrlSource(options => |
| 121 | +{ |
| 122 | + options.AddBaseUrl(new Uri("https://qatoolkitapi.azurewebsites.net/")); |
| 123 | + options.AddRequestFilters(new RequestFilter() |
| 124 | + { |
| 125 | + EndpointNameWhitelist = new string[] { "GetAllBikes" } |
| 126 | + }); |
| 127 | + options.UseSwaggerExampleValues = true; |
| 128 | +}); |
| 129 | + |
| 130 | +//Load requests object |
| 131 | +var requests = await urlSource.Load(new Uri[] { |
| 132 | + new Uri("https://qatoolkitapi.azurewebsites.net/swagger/v1/swagger.json") |
| 133 | +}); |
| 134 | +``` |
| 135 | + |
| 136 | +in the sample code above. Check the [QAToolKit.Source.Swagger](https://github.com/qatoolkit/qatoolkit-source-swagger-net) library for more details. |
100 | 137 |
|
101 | 138 | ## TO-DO |
102 | 139 |
|
103 | | -N/A |
| 140 | +- Currently tested for GET, POST, PUT and DELETE HTTP methods. Need to extend support. |
104 | 141 |
|
105 | 142 | ## License |
106 | 143 |
|
|
0 commit comments