[{"data":1,"prerenderedAt":316},["ShallowReactive",2],{"news-item-\u002Fnews\u002Fchay-load-test-performance-test-voi-artillery":3},{"id":4,"title":5,"body":6,"category":303,"created by":304,"date":305,"description":306,"extension":307,"meta":308,"navigation":309,"path":310,"sections":311,"seo":312,"stem":313,"thumbnail":314,"__hash__":315},"content_en\u002Fnews\u002Fchay-load-test-performance-test-voi-artillery.md","Run load test\u002Fperformance test with Artillery",{"type":7,"value":8,"toc":280},"minimark",[9,14,19,23,31,35,38,42,45,48,52,55,59,63,66,77,83,86,92,96,106,112,115,119,122,128,131,135,141,144,152,156,159,163,166,172,175,181,185,188,194,197,200,204,207,213,216,229,232,235,249,255,259,262,265,269],[10,11,13],"h2",{"id":12},"introduction","Introduction",[15,16,18],"h3",{"id":17},"what-is-performance-testing-and-why-should-you-do-a-load-test","What is performance testing? And why should you do a load test?",[20,21,22],"p",{},"Performance testing is software testing that evaluates how well an application performs under different workloads.",[20,24,25,26,30],{},"This involves measuring various metrics such as response time, bandwidth, and scalability to ensure that the application can handle the expected number of requests from users without degrading performance or crashing. ",[27,28,29],"strong",{},"Performance testing"," can also identify performance issues and bottlenecks, it helps you make necessary changes to optimize performance. The goal of this is to ensure that the application delivers a smooth, fast, and reliable user experience even under heavy load conditions.",[15,32,34],{"id":33},"about-artilleryio","About Artillery.io",[20,36,37],{},"Artillery.io is an open-source performance testing tool that allows you to test the performance and scalability of your web applications, APIs, and microservices. You can simulate real user behavior, generate load from multiple sources, and test your application flow according to the scenario.",[15,39,41],{"id":40},"supported-protocols","Supported protocols",[20,43,44],{},"Artillery.io is written in Node.js and supports HTTP, Websocket, and socket.io protocols.",[20,46,47],{},"Also, you can use Artillery’s plugin to test another stack like HLS (HTTP Live Streaming), Amazon Kinesis, Apache Kafka, etc.",[15,49,51],{"id":50},"script-formatting","Script formatting",[20,53,54],{},"Unlike other popular testing tools, Artillery.io provides a flexible YAML-based syntax for defining your load-testing scenarios. It also supports a JSON format.",[10,56,58],{"id":57},"install-basic-features","Install & Basic features",[15,60,62],{"id":61},"install","Install",[20,64,65],{},"You can globally install Artillery.io via npm or yarn by following this command:",[67,68,73],"pre",{"className":69,"code":71,"language":72},[70],"language-text","#npm\nnpm install -g artillery\n\n","text",[74,75,71],"code",{"__ignoreMap":76},"",[67,78,81],{"className":79,"code":80,"language":72},[70],"#yarn\nyarn global add artillery\n\n",[74,82,80],{"__ignoreMap":76},[20,84,85],{},"or you can also install it as a dev dependency of a Node.js project with:",[67,87,90],{"className":88,"code":89,"language":72},[70],"npm install -D artillery\n",[74,91,89],{"__ignoreMap":76},[15,93,95],{"id":94},"fast-test","Fast test",[20,97,98,99,105],{},"Use the quick subcommand so that you can run a test without writing a test script. For example, to run 10 virtual users (VUs), each of them makes 20 requests to the address ",[100,101,102],"a",{"href":102,"rel":103},"http:\u002F\u002Flocalhost:3000\u002F",[104],"nofollow",":",[67,107,110],{"className":108,"code":109,"language":72},[70],"artillery quick -c 10 -n 20 http:\u002F\u002Flocalhost:3000\u002Fapi\u002Fuser\n",[74,111,109],{"__ignoreMap":76},[20,113,114],{},"The -c parameter specifies the total number of VUs, and the -n indicates the number of requests per VU.",[15,116,118],{"id":117},"run-a-test-script","Run a test script",[20,120,121],{},"The run subcommand runs a test script from the local machine. The basic way to run a test is with:",[67,123,126],{"className":124,"code":125,"language":72},[70],"artillery run script.yaml -o report.json\n",[74,127,125],{"__ignoreMap":76},[20,129,130],{},"The -o parameter is the option to write a JSON report to a file.",[15,132,134],{"id":133},"export-report","Export report",[67,136,139],{"className":137,"code":138,"language":72},[70],"artillery report report.json -o report.html\n",[74,140,138],{"__ignoreMap":76},[20,142,143],{},"The report subcommand allows converting the JSON report generated by the run subcommand into a new HTML report.",[145,146],"img",{"className":147,"alt":76,"src":150,"style":151},[148,149],"block","mx-auto","https:\u002F\u002Fs3-ap-southeast-1.amazonaws.com\u002Fhomepage-media\u002Fwp-content\u002Fuploads\u002F2023\u002F05\u002F09143906\u002Ftest-create-user.png","width: 100%;",[10,153,155],{"id":154},"writing-a-simple-load-test","Writing a simple load test",[20,157,158],{},"In the tutorial below we will use YAML syntax, first we need to create a file name like “script.yml”, then we start by defining the “config” section at the top of a file.",[15,160,162],{"id":161},"config-section","Config Section",[20,164,165],{},"We need to set the base URL for your test script with the target option:",[67,167,170],{"className":168,"code":169,"language":72},[70],"config:\n  target: \"http:\u002F\u002Flocalhost:3000\u002Fapi\"\n",[74,171,169],{"__ignoreMap":76},[20,173,174],{},"Next, we'll start to define the load phase, a load phase defines how Artillery generates new VUs in a specified time period:",[67,176,179],{"className":177,"code":178,"language":72},[70],"config:\n  ...\n  phases:\n    - duration: 60\n      arrivalRate: 100\n",[74,180,178],{"__ignoreMap":76},[15,182,184],{"id":183},"scenarios-section","Scenarios Section",[20,186,187],{},"We can define one or more scenarios. Each scenario definition is an object which requires a flow attribute.",[67,189,192],{"className":190,"code":191,"language":72},[70],"scenarios:\n  - name: \"Get All Users\"\n    flow:\n      - get:\n          url: \"\u002Fuser\"\n          qs:\n            limit: 10\n            offset: 20\n",[74,193,191],{"__ignoreMap":76},[20,195,196],{},"In the ‘flow’ property, we continue to define the HTTP method as an object attribute, then we need to define the path of the API, we also can set a query string or set form body like URL-encoded forms, Multipart forms or JSON.",[20,198,199],{},"Save the scripts and then run your test script.",[10,201,203],{"id":202},"realistic-user-flows","Realistic User Flows",[20,205,206],{},"For example, a user flow could be:",[67,208,211],{"className":209,"code":210,"language":72},[70],"config:\n  target: \"http:\u002F\u002Flocalhost:3000\u002Fapi\"\n  phases:\n    - duration: 120\n      arrivalRate: 10\n      name: \"Preparing\"\n    - duration: 240\n      arrivalRate: 30\n      rampTo: 100\n      name: \"Increasing\"\n    - duration: 600\n      arrivalRate: 100\n      name: \"Sustained Load\"\n  payload:\n    path: \"login.csv\"\n    cast: false\n    fields:\n      - \"user\"\n      - \"pass\"\n  processor: \".\u002Fprocessor.js\"\n\nbefore:\n  flow:\n    - log: \"Get auth token\"\n    - post:\n        url: \"\u002Fauth\u002Flogin\"\n        json:\n          username: \"{{user}}\"\n          password: \"{{pass}}\"\n        capture:\n          - json: $.tokens.accessToken\n            as: token\nscenarios:\n  - name: \"Search Users\"\n    flow:\n      - get:\n          url: \"\u002Fuser\"\n          headers:\n            authorization: \"Bearer {{ token }}\"\n  - name: \"Create New User\"\n    flow:\n      - post:\n          url: \"\u002Fuser\"\n          beforeRequest: \"setJsonBody\"\n          headers:\n            authorization: \"Bearer {{ token }}\"\n",[74,212,210],{"__ignoreMap":76},[20,214,215],{},"In the above script, we have three phases in the configuration:",[217,218,219,223,226],"ul",{},[220,221,222],"li",{},"The first phase creates 10 VUs per second and sends them to the application for 2 minutes.",[220,224,225],{},"In the second phase, the load test will start at 30 users per second and gradually increase to 100 users per second over 4 minutes.",[220,227,228],{},"The final phase simulates a sustained load of 100 users per second for 10 minutes.",[20,230,231],{},"By using multiple phases allows you to accurately simulate realistic traffic scenarios and evaluate your application's ability to handle a sudden barrage of requests.",[20,233,234],{},"In the first flow of scenarios, each of the VUs requests GET | \"Search user\", then in the second flow they'll request POST | \"Create user\".",[20,236,237,238,241,242,245,246,248],{},"To make it easy to generate data. In this way, before executing the request. We use the ",[27,239,240],{},"setJsonBody"," function on the custom hook ",[27,243,244],{},"beforeRequest",", The function ",[27,247,240],{}," is defined in the processor.js file referenced in config.processor.",[67,250,253],{"className":251,"code":252,"language":72},[70],"# processor.js\nconst { faker } = require(\"@faker-js\u002Ffaker\");\n\nfunction setJsonBody(requestParams, context, ee, next) {\n  var body = {\n    username: faker.internet.userName(),\n    password: faker.internet.password(3),\n    email: faker.internet.exampleEmail(),\n    name: faker.name.fullName(),\n    role: faker.datatype.number({ min: 0, max: 1 }),\n  };\n\n  requestParams.json = body;\n  return next();\n}\n\nmodule.exports = {\n  setJsonBody,\n};\n",[74,254,252],{"__ignoreMap":76},[10,256,258],{"id":257},"compare-to-k6-load-testing","Compare to K6 load testing",[20,260,261],{},"Artillery and k6 are both open-source tools for performance testing and have similar features such as extensibility, integration with CI\u002FCD tools, and the ability to set test patterns. However, there are some differences between them. Artillery is written in Node.js while k6 is written in Go. So, Artillery is slower than k6, because it isn't multithreaded and uses more memory. Here are the charts comparing memory usage between k6 with Artillery and other tools.",[20,263,264],{},"In conclusion, Artillery and k6 are both powerful tools. However, k6 has more advantages than Artillery. If you want to write a simple load test, Artillery is a good solution that uses YAML syntax.",[10,266,268],{"id":267},"references","References",[20,270,271,275,276],{},[100,272,273],{"href":273,"rel":274},"https:\u002F\u002Fwww.artillery.io\u002F",[104]," ",[100,277,278],{"href":278,"rel":279},"https:\u002F\u002Fk6.io\u002Fblog\u002Fcomparing-best-open-source-load-testing-tools\u002F",[104],{"title":76,"searchDepth":281,"depth":281,"links":282},2,[283,290,296,300,301,302],{"id":12,"depth":281,"text":13,"children":284},[285,287,288,289],{"id":17,"depth":286,"text":18},3,{"id":33,"depth":286,"text":34},{"id":40,"depth":286,"text":41},{"id":50,"depth":286,"text":51},{"id":57,"depth":281,"text":58,"children":291},[292,293,294,295],{"id":61,"depth":286,"text":62},{"id":94,"depth":286,"text":95},{"id":117,"depth":286,"text":118},{"id":133,"depth":286,"text":134},{"id":154,"depth":281,"text":155,"children":297},[298,299],{"id":161,"depth":286,"text":162},{"id":183,"depth":286,"text":184},{"id":202,"depth":281,"text":203},{"id":257,"depth":281,"text":258},{"id":267,"depth":281,"text":268},"testing","Briswell Vietnam Co Ltd","2023-08-07","Performance testing is software testing that evaluates how well an application performs under different workloads. This involves measuring various metrics such as response time, bandwidth, and scalability to ensure that the application can handle the expected number of requests from users without degrading performance or crashing. Performance testing can also identify performance issues and bottlenecks, it helps you make necessary changes to optimize performance. The goal of this is to ensure that the application delivers a smooth, fast, and reliable user experience even under heavy load conditions.","md",{},true,"\u002Fnews\u002Fchay-load-test-performance-test-voi-artillery",null,{"title":5,"description":306},"news\u002Fchay-load-test-performance-test-voi-artillery","https:\u002F\u002Fs3-ap-southeast-1.amazonaws.com\u002Fhomepage-media\u002Fwp-content\u002Fuploads\u002F2023\u002F05\u002F09143246\u002Fartillery.png","L7b2ITUBfc1iRqkjETIHk1aKrl0XkfwZt61JRvabgec",1782263084502]