Preface
If you have been a student of credit or a university cadaver in recent years, surely you will not be too far away from the scene of fighting over the web to register to study, to see the results of things. . While the water is boiling, you only get a brief message “500 Internal Server Error”, but turning to the next person, you can still see it is normal, so you tried F5 but the result no better.
And the reason is that there are too many people accessing the website at the same time, so the server cannot load, so some people can access, and some people cannot (the tools say due to human dignity ).
As a web developer, we will have to learn and offer solutions to avoid this situation. And the first step is to test it, certainly you can not send requests bằng cơm
to test the load of the server, so the tool to support the load bearing of the server was born to make the life of the dev easy. more than part.
In this article, let’s take a look at one of our newly enlightened tools, which is Artillery .
content
Before going into learning about how to use Artillery, I also introduced it is always including 2 versions, 1 is Community Edition (free) and Pro (chargeable) . And in the scope of research, personal use, we will use the free version, and you who have conditions or follow the team can try the fee version (cost is about> $ 199 / month)
Setting
Because Artillery is written in Node.js, you need to have Node.js version 10.16.3 or higher installed, and the installation command is very simple.
1 2 3 4 | <span class="token function">npm</span> <span class="token function">install</span> -g artillery <span class="token comment">#cài đặt artillery</span> artillery -V <span class="token comment">#kiểm tra phiên bản, nếu không check được tức là việc cài đặt vẫn chưa thành công</span> |
Configuration and use
After the installation is completed, run some simple test commands
1 2 3 4 5 6 | artillery quick --count 10 -n 20 https://artillery.io/ <span class="token comment"># Với câu lệnh trên, bạn đã tạo ra:</span> <span class="token comment"># - 10 user ảo</span> <span class="token comment"># - mỗi user gửi 20 request </span> <span class="token comment"># đến địa chỉ https://artillery.io/</span> |
However, the above statement is just a very simple case, in fact, users often interact with the server in a script rather than just sending a request. And to run the test in a script, we need to learn some keywords to set up the configuration as follows:
- target : server to test (usually base URL for HTTP application or hostname for WebSocket Server)
- phases : test run time and frequency of requests sent during that time
- in phases can declare several consecutive phases with duration and frequency of different requests
- You can name each phase
- Pauses can be used to create a pause phase, without performing any operations
- rampTo : you can understand it like the
for
loop, for example, when declaringarrivalRate: 0
andrampTo: 10
, the artillery will be divided into consecutive phases witharrialRate
0, 1, 2, …, 10.
- headers : declares the default HTTP header for all requests you send
- environments : when you want to reuse the test for different environments (staging, production, …) with some minor changes to each environment, you can declare the environment as follows:12345678environments:production:target:phases:staging:target:phases:
- scenarios : test script declaration (declared like an
object
, REQUIRED containsflow
attribute and may have some other properties)- flow: is an array, declaring the actions that virtual users will perform in those scenarios (can be request GET / POST to HTTP application)
- name: describe in detail the operation of that scenario
- weight: the weight of the scenarios, which is understood as the percentage that the scenario is selected to perform (for example, 3 scenarios A, B, C have a weight of 2,3,5, then A has 20% selected, B is 30% and C is 50%)
And the result we get is a .yml file with the same structure as below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | config: target: <span class="token string">'https://artillery.io'</span> <span class="token comment">#địa chỉ muốn test</span> phases: <span class="token comment">#có thể khai báo để chạy nhiều phases với thiết lập cụ thể khác nhau </span> - duration: 60 <span class="token comment">#kéo dài trong 60s</span> arrivalRate: 20 <span class="token comment">#trung bình 20 user/giây</span> name: <span class="token string">"tên phase 1"</span> - pause: 60 <span class="token comment">#phase này sẽ không làm gì (pause) trong 60s</span> - duration: arrivalRate: name: <span class="token string">"tên phase 2"</span> defaults: headers: x-my-service-auth: <span class="token string">'987401838271002188298567'</span> scenarios: <span class="token comment">#lên kịch bản cho cho từng user</span> - flow: - get: url: <span class="token string">"/docs"</span> - post: url: "/resource |
And to run the example.yml test above, we execute the command
1 2 | artillery run example.yml |
The result will look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | Complete report @ 2019-01-02T17:32:36.653Z Scenarios launched: 300 <span class="token comment">#số user ảo được tạo ra trong toàn bộ thời gian test</span> Scenarios completed: 300 <span class="token comment">#số user ảo đã hoàn tất kịch bản test trong toàn bộ thời gian test</span> Requests completed: 600 <span class="token comment">#số HTTP request và response hoặc số messages WebSocket đã gửi</span> RPS sent: 18.86 <span class="token comment">#số request trung bình đã hoàn tất mỗi giây (request per second)</span> Request latency: <span class="token comment">#độ trễ request (được tính bằng đơn vị miliseconds)</span> min: 52.1 max: 11005.7 median: 408.2 p95: 1727.4 <span class="token comment">#95% số request cần <= 1727.4 milliseconds để hoàn tất</span> p99: 3144 <span class="token comment">#99% số request cần <= 3144 milliseconds để hoàn tất</span> Scenario counts: 0: 300 <span class="token punctuation">(</span> 100% <span class="token punctuation">)</span> Codes: <span class="token comment">#số lượng response trả về ứng với status code</span> 200: 300 302: 300 |
Based on the above report, we can know the response status that the server returns on the total number of users who have sent requests to the server:
- Of the 600 requests that were completed, 300 responses returned code 200 (OK) and 300 responses returned code 302 (Found). There are also other status codes, you can find out here .
- Time to complete 1 request:
- fastest: 52.1 milliseconds (0.052s)
- longest: 11005.7 milliseconds (11s)
- Average: 408.2 milliseconds (0.4s)
- 95% of requests completed under 1727.4 milliseconds (1.7s)
- 99% of requests completed under 3144 milliseconds (3.1s)
-> from the above statistics you can see that your server is running quite well with the number of requests as on the example.yml
file, you can try increasing the arrivalRate
to see how much the server’s load capacity is.
And when the server load limit is reached, you can learn more ways to increase the server load (for example, refactor code, scale by adding server hardware, etc.).
Epilogue
Within the scope of this article, I have just briefly introduced the tools to support load testing of the server only. As for the problem of how to increase the load on the server, I beg for alms in the following article, because honestly it depends on a lot of issues, so I cannot say it all in one article.
Thank you for following my article, if you find it useful, please support me upvote!
References
- Artillery: https://artillery.io/
- Github: https://github.com/artilleryio/artillery
- In addition, Artillery is also launching a fairly practical program, which is to support the planting of 100,000 trees at https://offset.earth/artilleryio