{"id":1086,"date":"2021-01-27T15:46:54","date_gmt":"2021-01-27T12:46:54","guid":{"rendered":"https:\/\/www.etemkeskin.com\/?p=1086"},"modified":"2021-01-27T16:19:58","modified_gmt":"2021-01-27T13:19:58","slug":"load-testing-web-applications-and-web-services-with-locust","status":"publish","type":"post","link":"https:\/\/www.etemkeskin.com\/index.php\/2021\/01\/27\/load-testing-web-applications-and-web-services-with-locust\/","title":{"rendered":"Load Testing Web Applications and Web Services with LOCUST"},"content":{"rendered":"\n<p>In this post, I will explain <strong>LOCUST<\/strong>, a very useful python library. When we have website or web service, we want the best user experience. For this, we want our website and services to have low response times and not to crash.<\/p>\n\n\n\n<p><strong>Locust<\/strong> is a tool with a user interface that we can quickly and easily test our websites or services performance. With Locust, we can test if our site crashes by making requests to many users at the same time. In addition, we can see how the improvements we made in our application (such as the cache mechanism) are compared to the previous ones by testing Locust.<\/p>\n\n\n\n<h4>Installation<\/h4>\n\n\n\n<p>In order to use Locust, we first need to install it on our computer. For this, we install it using <strong>pip<\/strong> package manager by running the following codes.<\/p>\n\n\n\n<p>After installing library with <code>pip3 install locust<\/code>, we can check the version by running <code>locust -v<\/code>.<\/p>\n\n\n\n<h4>Usage<\/h4>\n\n\n\n<p>We create our file named <strong>locust_test.py<\/strong> where we will write our tests.<\/p>\n\n\n\n<p>We create our test case by writing the following codes in this test file.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;showPanel&quot;:false,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">import time\nfrom locust import HttpUser, task, between\n\nclass QuickstartUser(HttpUser):\n    wait_time = between(1, 2.5)\n\n    @task\n    def my_first_task(self):\n        self.client.get(&quot;&quot;)\n\n    @task(2)\n    def my_second_task(self):\n        self.client.get(&quot;index.php\/about\/&quot;)<\/pre><\/div>\n\n\n\n<p>In our test, we will measure the performance of our website by making GET requests to the homepage and about page.<\/p>\n\n\n\n<p><strong>wait_time:<\/strong> Random time between 1 and 2.5 seconds is generated and the HTTP request is made according to this time.<\/p>\n\n\n\n<p><strong>@task<\/strong> : When our test script is run, the tasks defined under the task decorator are performed. task decorator takes an optional parameter. <br>This parameter defines the weight of the tasks. In our example, <strong>my_first_task<\/strong> requests one request while <strong>my_second_task<\/strong> requests twice.<\/p>\n\n\n\n<p>Since we created our script on the desktop, we first open the command terminal to run this file. Then we run the following commands by typing (or file_path \/ locust_test.py):<\/p>\n\n\n\n<p><code>locust -f locust_test.py<\/code> <\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1006\" height=\"76\" src=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2021\/01\/image-11.png\" alt=\"\" class=\"wp-image-1064\"\/><\/figure>\n\n\n\n<p>When our script runs without any errors, we can open the Locust interface by typing <strong>http: \/\/ localhost: 8089\/<\/strong> into the web browser.  <\/p>\n\n\n\n<p>Then, a form appears on the screen. In this form, we define how many users will be simulated in the test, in how many seconds we want to add users(<strong>spawn rate<\/strong>) and which domain to connect to. When we press the <strong>Start swarming<\/strong> button after filling the form, our test starts.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" width=\"387\" height=\"439\" src=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2021\/01\/image-14.png\" alt=\"\" class=\"wp-image-1075\"\/><\/figure><\/div>\n\n\n\n<p>When the test starts, the screen in the picture below will appear. By looking at the table, we can see response times, the total number of requests, the number of requests per second (RPS (request per second)), failures. We can open the chart screen by pressing the Charts tab.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" width=\"1020\" height=\"376\" src=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2021\/01\/image-12.png\" alt=\"\" class=\"wp-image-1065\"\/><\/figure>\n\n\n\n<p>To get the test report, we open the Download Report page and click the download report button to obtain the report.<\/p>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter size-large\"><img loading=\"lazy\" width=\"708\" height=\"957\" src=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2021\/01\/image-13.png\" alt=\"\" class=\"wp-image-1066\"\/><\/figure><\/div>\n\n\n\n<p>While writing Locust Tests, we can make <strong>POST<\/strong> request.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;showPanel&quot;:false,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">@task\ndef my_load_test(self):\n        self.client.post(&quot;\/login&quot;, json={&quot;username&quot;:&quot;foo&quot;, &quot;password&quot;:&quot;bar&quot;})<\/pre><\/div>\n\n\n\n<p>We can also define our tests as loop.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;python&quot;,&quot;mime&quot;:&quot;text\/x-python&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:false,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;showPanel&quot;:false,&quot;language&quot;:&quot;Python&quot;,&quot;modeName&quot;:&quot;python&quot;}\">@task()\n    def view_items(self):\n        for item_id in range(10):\n            self.client.get(f&quot;\/item?id={item_id}&quot;, name=&quot;\/item&quot;)\n            time.sleep(1)<\/pre><\/div>\n\n\n\n<p>Locust also supports using multiple machines for testing. For more information, you can improve your tests using locust&#8217;s official document. Good luck.<\/p>\n\n\n\n<h5>Sources<\/h5>\n\n\n\n<ol><li>https:\/\/docs.locust.io\/en\/stable\/index.html<\/li><\/ol>\n","protected":false},"excerpt":{"rendered":"<p>In this post, I will explain LOCUST, a very useful python library. When we have website or web service, we want the best user experience. For this, we want our website and services to have low response times and not to crash. Locust is a tool with a user interface that we can quickly and [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1,4],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Load Testing Web Applications and Web Services with LOCUST - blog website<\/title>\n<meta name=\"description\" content=\"In this post, I will explain LOCUST, a very useful python library. When we have website or web service, we want the best user experience. For this, we want our website and services to have low response times and not to crash. Locust is a tool with a user interface that we can quickly and easily test our websites or services performance. With Locust, we can test if our site crashes by making requests to many users at the same time. In addition, we can see how the improvements we made in our application (such as the cache mechanism) are compared to the previous ones by testing Locust.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.etemkeskin.com\/index.php\/2021\/01\/27\/load-testing-web-applications-and-web-services-with-locust\/\" \/>\n<meta property=\"og:locale\" content=\"tr_TR\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Load Testing Web Applications and Web Services with LOCUST - blog website\" \/>\n<meta property=\"og:description\" content=\"In this post, I will explain LOCUST, a very useful python library. When we have website or web service, we want the best user experience. For this, we want our website and services to have low response times and not to crash. Locust is a tool with a user interface that we can quickly and easily test our websites or services performance. With Locust, we can test if our site crashes by making requests to many users at the same time. In addition, we can see how the improvements we made in our application (such as the cache mechanism) are compared to the previous ones by testing Locust.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.etemkeskin.com\/index.php\/2021\/01\/27\/load-testing-web-applications-and-web-services-with-locust\/\" \/>\n<meta property=\"og:site_name\" content=\"blog website\" \/>\n<meta property=\"article:published_time\" content=\"2021-01-27T12:46:54+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-01-27T13:19:58+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2021\/01\/image-11.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Tahmini okuma s\u00fcresi\" \/>\n\t<meta name=\"twitter:data1\" content=\"3 dakika\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.etemkeskin.com\/#website\",\"url\":\"https:\/\/www.etemkeskin.com\/\",\"name\":\"blog website\",\"description\":\"Etem KESK\\u0130N\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/www.etemkeskin.com\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"tr\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/01\/27\/load-testing-web-applications-and-web-services-with-locust\/#primaryimage\",\"inLanguage\":\"tr\",\"url\":\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2021\/01\/image-11.png\",\"contentUrl\":\"https:\/\/www.etemkeskin.com\/wp-content\/uploads\/2021\/01\/image-11.png\",\"width\":1006,\"height\":76},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/01\/27\/load-testing-web-applications-and-web-services-with-locust\/#webpage\",\"url\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/01\/27\/load-testing-web-applications-and-web-services-with-locust\/\",\"name\":\"Load Testing Web Applications and Web Services with LOCUST - blog website\",\"isPartOf\":{\"@id\":\"https:\/\/www.etemkeskin.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/01\/27\/load-testing-web-applications-and-web-services-with-locust\/#primaryimage\"},\"datePublished\":\"2021-01-27T12:46:54+00:00\",\"dateModified\":\"2021-01-27T13:19:58+00:00\",\"author\":{\"@id\":\"https:\/\/www.etemkeskin.com\/#\/schema\/person\/dcbc30282861ce578b96a79ce8789629\"},\"description\":\"In this post, I will explain LOCUST, a very useful python library. When we have website or web service, we want the best user experience. For this, we want our website and services to have low response times and not to crash. Locust is a tool with a user interface that we can quickly and easily test our websites or services performance. With Locust, we can test if our site crashes by making requests to many users at the same time. In addition, we can see how the improvements we made in our application (such as the cache mechanism) are compared to the previous ones by testing Locust.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/01\/27\/load-testing-web-applications-and-web-services-with-locust\/#breadcrumb\"},\"inLanguage\":\"tr\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.etemkeskin.com\/index.php\/2021\/01\/27\/load-testing-web-applications-and-web-services-with-locust\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.etemkeskin.com\/index.php\/2021\/01\/27\/load-testing-web-applications-and-web-services-with-locust\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Ana sayfa\",\"item\":\"https:\/\/www.etemkeskin.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Load Testing Web Applications and Web Services with LOCUST\"}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.etemkeskin.com\/#\/schema\/person\/dcbc30282861ce578b96a79ce8789629\",\"name\":\"etemkeskin\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/www.etemkeskin.com\/#personlogo\",\"inLanguage\":\"tr\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/6af0148b790691ed24ae245fb3dc773b?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/6af0148b790691ed24ae245fb3dc773b?s=96&d=mm&r=g\",\"caption\":\"etemkeskin\"},\"url\":\"https:\/\/www.etemkeskin.com\/index.php\/author\/etemkeskinyahoo-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/1086"}],"collection":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/comments?post=1086"}],"version-history":[{"count":28,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/1086\/revisions"}],"predecessor-version":[{"id":1117,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/posts\/1086\/revisions\/1117"}],"wp:attachment":[{"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/media?parent=1086"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/categories?post=1086"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.etemkeskin.com\/index.php\/wp-json\/wp\/v2\/tags?post=1086"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}