{"id":28697,"date":"2024-04-08T15:55:27","date_gmt":"2024-04-08T14:55:27","guid":{"rendered":"https:\/\/nelkodev.com\/blog\/optimiza-el-rendimiento-pruebas-de-estres-en-node-js-con-artillery-io\/"},"modified":"2024-06-03T18:43:52","modified_gmt":"2024-06-03T17:43:52","slug":"optimiza-el-rendimiento-pruebas-de-estres-en-node-js-con-artillery-io","status":"publish","type":"post","link":"https:\/\/nelkodev.com\/en\/blog\/optimize-performance-stress-testing-in-node-js-with-artillery-io\/","title":{"rendered":"Optimize Performance: Stress Testing in Node.js with Artillery.io"},"content":{"rendered":"<p>In modern application development, ensuring your application adequately handles large volumes of traffic is crucial. As your apps grow in functionality and users, it&#039;s essential to validate how they perform under pressure before any major release. For Node.js developers, a powerful and versatile tool that assists in this task is Artillery.io. Through this article we will explore how to implement load and stress tests in Node.js applications using Artillery.io, teaching step by step how to perform these essential tests to ensure the scalability and stability of your application.<\/p>\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_80 counter-hierarchy ez-toc-counter ez-toc-custom ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #ffffff;color:#ffffff\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #ffffff;color:#ffffff\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/nelkodev.com\/en\/blog\/optimize-performance-stress-testing-in-node-js-with-artillery-io\/#%C2%BFQue_es_Artilleryio\" >What is Artillery.io?<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/nelkodev.com\/en\/blog\/optimize-performance-stress-testing-in-node-js-with-artillery-io\/#Configuracion_Inicial_de_Artilleryio_en_un_Proyecto_de_Nodejs\" >Initial Setup of Artillery.io in a Node.js Project<\/a><ul class='ez-toc-list-level-3' ><li class='ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/nelkodev.com\/en\/blog\/optimize-performance-stress-testing-in-node-js-with-artillery-io\/#Instalacion_de_Artillery\" >Artillery Installation<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/nelkodev.com\/en\/blog\/optimize-performance-stress-testing-in-node-js-with-artillery-io\/#Creacion_de_un_Script_de_Prueba\" >Creating a Test Script<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/nelkodev.com\/en\/blog\/optimize-performance-stress-testing-in-node-js-with-artillery-io\/#Ejecucion_de_Pruebas_y_Analisis_de_Resultados\" >Execution of Tests and Analysis of Results<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/nelkodev.com\/en\/blog\/optimize-performance-stress-testing-in-node-js-with-artillery-io\/#Estrategias_Avanzadas_de_Pruebas_con_Artillery\" >Advanced Artillery Testing Strategies<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/nelkodev.com\/en\/blog\/optimize-performance-stress-testing-in-node-js-with-artillery-io\/#Conclusion\" >Conclusion<\/a><\/li><\/ul><\/nav><\/div>\n<h2><span class=\"ez-toc-section\" id=\"%C2%BFQue_es_Artilleryio\"><\/span>What is Artillery.io?<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Artillery.io is a modern load and performance testing tool, used to test APIs, microservices, and web applications. It works by generating simulated traffic to the server to mimic the behavior of multiple users simultaneously accessing the application, allowing developers to identify bottlenecks and performance issues before they affect real users.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Configuracion_Inicial_de_Artilleryio_en_un_Proyecto_de_Nodejs\"><\/span>Initial Setup of Artillery.io in a Node.js Project<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Before starting testing, we need to configure Artillery in our local development environment. Below, I&#039;ll guide you through the initial steps:<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Instalacion_de_Artillery\"><\/span>Artillery Installation<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>To install Artillery, you simply need to have Node.js and npm installed. Run the following command in your terminal:<\/p>\n<pre><code class=\"&quot;language-bash&quot;\">npm install -g artillery<\/code><\/pre>\n<p>This command will install Artillery globally on your system, allowing you to access the Artillery CLI from anywhere on your terminal.<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Creacion_de_un_Script_de_Prueba\"><\/span>Creating a Test Script<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Artillery uses YAML or JSON scripts to define load test parameters. Next, we will create a file called <code>test-script.yml<\/code> which will contain the initial configuration:<\/p>\n<pre><code class=\"&quot;language-yaml&quot;\">config: target: &#039;http:\/\/localhost:3000&#039; phases: - duration: 60 arrivalRate: 20 scenarios: - flow: - get: url: &quot;\/api\/resource&quot;<\/code><\/pre>\n<p>In this basic script, we configure Artillery to target <code>http:\/\/localhost:3000<\/code>, which will be our local Node.js server. We define a test phase of 60 seconds with an arrival rate of 20 virtual users per second. Next, we specify a simple flow of users who will make GET requests to <code>\/api\/resource<\/code>.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Ejecucion_de_Pruebas_y_Analisis_de_Resultados\"><\/span>Execution of Tests and Analysis of Results<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>With our script ready, we run the test with the following command:<\/p>\n<pre><code class=\"&quot;language-bash&quot;\">artillery run test-script.yml<\/code><\/pre>\n<p>Upon execution, Artillery will provide a report on server performance that includes metrics such as request rate, average latency, and HTTP response codes. This data is crucial for evaluating how the application behaves under load.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Estrategias_Avanzadas_de_Pruebas_con_Artillery\"><\/span>Advanced Artillery Testing Strategies<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>To get the most out of load testing, you can explore advanced Artillery features:<\/p>\n<ul>\n<li><strong>Environment Variables<\/strong>: Use variables to customize test environments without changing code.<\/li>\n<li><strong>Authentication<\/strong>: Add tests that require authentication by simulating authentic users.<\/li>\n<li><strong>Data Capture and Use<\/strong>: Capture data from a response and use it in subsequent requests to simulate more realistic user flows.<\/li>\n<\/ul>\n<pre><code class=\"&quot;language-yaml&quot;\">config: target: &#039;http:\/\/localhost:3000&#039; phases: - duration: 120 arrivalRate: 50 rampTo: 100 name: &quot;Ramp up load&quot; variables: authToken: &#039;YOUR_AUTH_TOKEN&#039; scenarios: - beforeRequest: &quot;setHeaders&quot; flow: - post: url: &quot;\/api\/login&quot; json: username: &quot;testuser&quot; password: &quot;password&quot; capture: json: &quot;$.token&quot; as: &quot;authToken&quot; ...<\/code><\/pre>\n<p>This example shows how to handle authentication and load ramping.<\/p>\n<h2><span class=\"ez-toc-section\" id=\"Conclusion\"><\/span>Conclusion<span class=\"ez-toc-section-end\"><\/span><\/h2>\n<p>Load and stress testing is essential for any application that aspires to perform well under pressure. Artillery.io offers a robust and flexible tool for Node.js that facilitates these tests, helping developers prepare their applications for the real world. With the strategies discussed and proper implementation, you can significantly improve the resilience and scalability of your web services.<\/p>\n<p>For more details on the optimization and development of robust applications, I invite you to visit <a href=\"https:\/\/nelkodev.com\/en\/\">my blog NelkoDev<\/a> o <a href=\"https:\/\/nelkodev.com\/en\/contact\/\">contact me<\/a> if you have questions or need assistance with your development projects.<\/p>","protected":false},"excerpt":{"rendered":"<p>In modern application development, ensuring your application can adequately handle high volumes of traffic is crucial. As your applications grow in functionality and users, it is essential to validate how they behave under pressure before any major release. For Node.js developers, a powerful and versatile tool that assists in this is Node.js.<\/p>","protected":false},"author":1,"featured_media":28698,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[420,2207,1890],"tags":[1888,205,90,1889,1193,2208,884,1008,202],"class_list":["post-28697","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","category-nodejs","category-pruebas-de-rendimiento","tag-artillery-io","tag-blog","tag-con","tag-estres","tag-node-js","tag-nodejs","tag-optimiza","tag-pruebas","tag-rendimiento"],"_links":{"self":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/28697","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/comments?post=28697"}],"version-history":[{"count":0,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/posts\/28697\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media\/28698"}],"wp:attachment":[{"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/media?parent=28697"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/categories?post=28697"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nelkodev.com\/en\/wp-json\/wp\/v2\/tags?post=28697"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}