Replies: 2 comments 5 replies
-
That's exactly the use case for Artillery. Bearing in mind that throughput will depend on the environment and exact nature of the workload of course, the process is: run some load on your app, see what CPU usage is like under some level of load, increase load to understand the relationship between workload increase and increase in CPU usage. |
Beta Was this translation helpful? Give feedback.
-
Getting startedArtillery can be installed like any other Node package, using NPM:
Doing a quick testArtillery can be used to do one-off tests to a specific URL in a similar way of tools like Apache Benchmark. That is done using the
This will launch 10 virtual users that will do 20 requests each, to the specified URL. The For example, the following command will run the test during 10s, with 2 new users arriving each second, resulting in around 20 requests in the total.
You can combine all these flags, to achieve the desired pattern of load for your test. In terms of other options, I found this command to be pretty limited as you can´t configure almost anything more of the request, like defining request headers. It also only allows GET requests. So, if you just want to do some one-off test directly from the command line and need a little more flexibility configuring the request, there are probably better tools like Apache Benchmark. But the power of Artillery lies in being able to simulate realistic user behavior with scenarios and flows, using declarative YAML files. Scnearios and YAML configurationThe most basic test case can be defined like this:
The In this example, we define one phase, which will last 60 seconds with 20 new virtual users (arriving every second (on average)). The We will see this in more detail, later in this article. In this particular case, we have defined one single scenario, with a single step, which is a GET request to the You can run this test using the
This is very similar to the "quick" example, just a request to a specific endpoint. Here is a more advanced example demonstrating how to do a POST request with payload data loaded from a CSV file.
This test will do a "POST" request to the "/auth" endpoint, with a JSON body containing the fields "username" and "password", which values are loaded from a CSV file "users.csv", from the specified fields. You can find out about all the possibilities in the Documentation. more complete user flowWe have seen how to define a scenario with a simple flow. Considering a typical E-commerce site. You might want to simulate a user flow, starting from the catalog page, selecting a product, and add it to the cart. This can be achieved with the following configuration:
Some things we haven´t seen before. A flow can consist of multiple steps, representing the user journey in the application. Each user spawned by Artillery will execute all the defined steps sequentially. In this particular example, The first step is doing a POST request to the "/search" endpoint with a body, containing keywords, loaded from a CSV file.
The "capture" block allows us to get a value from the response and save it as a variable for later use. You can use "JSONPath", "XPath", "Regex" and more to parse the response and get the value you want. For example, to get a URL form an anchor tag in HTML response you could do as following:
Back to our E-commerce flow, We will use the captured "id" variable from the "search" step, to go to the "product details" page and to add the product to cart:
As you can see with a few lines of YAML we can define an entire user flow to test. Artillery supports many more functions to work with HTTP requests and responses. You can find then in the http reference section of the documentation. Multiple ScenariosIn the previous example, we saw how to define a single scenario with multiple steps. Let´s say you want to simulate users searching and buying products at the same time. You can create two different scenarios to test both flows. Note that each user will only execute one of the scenarios. The scenario supports a "weight" property which you can use to indicate how likely it is that scenario to be picked by a particular user. By default, each scenario will have the same probability. If you define a scenario with weight 2, that scenario is twice as likely to be picked as the one with 1 (which is the default value). Setting success conditionsIt´s possible to configure Artillery to return a non-zero exit code if the test run doesn't comply with specified conditions based on a set of parameters like error rate, minimum, maximum and percentile based latency. This is really useful in a CI environment as you can make the test fail if it doesn´t meet your performance requirements. The following configuration will ensure that at least 95% of requests are executed below 200ms, otherwise, the command will exit with an error.
Extend ArtilleryHooksArtillery has support for "hooks", which allow for custom JS functions to be executed at certain points during the execution of a scenario. The following extension points are available:
You can use these hooks, to log extra information you need or to modify the request of some sort before doing a request. this article shows a very good example of this, by leveraging Faker.js to generate random data for the tests. PluginsArtillery supports custom plugins that you can write using Javascript. Artillery has some official plugins like artillery-plugin-expect that can be used to add expectations/assertions to your HTTP scenarios, allowing you to do some basic functional tests together with your load tests or the artillery-publish-metrics, which allows sending metrics about your test runs to external services like Datadog, InfluxDB or StatsD. Here is an example of the
You can build your own to add extra functionality. |
Beta Was this translation helpful? Give feedback.
-
Hello,
I would like to test the max throughput of my NodeJS app. So 100% CPU = how many res/sec?
Beta Was this translation helpful? Give feedback.
All reactions