Deep-dive into Vegeta - HTTP load testing tool and library
Tags: testing, performance, go, tutorial
Published: 2023-10-31 00:32:15


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kuoxxbe87t3tfk60idri.jpeg)<!-- wp:paragraph -->
<p>As a performance engineer, you know how crucial it is to expand your skillset and become proficient with various tools. In your demanding role, ensuring the optimal performance of applications and systems is paramount. To achieve this, you need a diverse range of tools and techniques in your arsenal.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In this blog article, you're about to embark on a journey to explore a valuable addition to your toolkit: Vegeta, an HTTP load testing tool and library. As a performance engineer, you're likely no stranger to the importance of load testing in evaluating and enhancing system performance. Vegeta is one such tool that will help you accomplish this more effectively.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>By delving into the world of Vegeta, you'll gain insights into its features, capabilities, and practical applications for load testing. This knowledge will empower you to handle a higher volume of users and requests while ensuring your systems maintain peak performance. So, let's dive in and discover how Vegeta can further elevate your performance engineering expertise.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading">Vegeta - HTTP load testing tool and library</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Vegeta is an open-source HTTP load testing tool and library designed to help evaluate and analyze the performance of web services, servers, and applications by simulating a large number of HTTP requests. It was created in the Go programming language and is widely used for performance testing and benchmarking.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Vegeta is particularly popular among developers and performance engineers because of its simplicity, ease of use, and the ability to conduct performance tests efficiently. It's worth noting that while Vegeta is a powerful tool for HTTP load testing, users should always ensure they have appropriate permissions and are following ethical and legal guidelines when testing web services and applications.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading">Vegeta Features</h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>CLI and Go-based tool</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Easy to learn and pluggable in automation workflow</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Customizable HTML reporting</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Simple distributed load testing using pdsh</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Easy to install, update, and manage</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Avoids Coordinated Omission</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:image {"align":"center","id":17668,"sizeSlug":"full","linkDestination":"media"} -->
<figure class="wp-block-image aligncenter size-full"><a href="https://qainsights.com/wp-content/uploads/2023/10/image-4.png"><img src="https://qainsights.com/wp-content/uploads/2023/10/image-4.png" alt="It's Over 9000!" class="wp-image-17668"/></a></figure>
<!-- /wp:image -->

<!-- wp:heading -->
<h2 class="wp-block-heading">Install Vegeta</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In macOS, you can install Vegeta using brew.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>brew update &amp;&amp; brew install vegeta</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>For Windows, make sure you have installed Go. To install vegeta, use</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>go get -u github.com/tsenart/vegeta</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>To validate vegeta:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>vegeta -version</code></pre>
<!-- /wp:code -->

<!-- wp:heading -->
<h2 class="wp-block-heading">Simple Load Test</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>To begin a simple load test in vegeta, issue the below command.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>echo "GET http://localhost:8080" | vegeta attack -duration=10s | tee results.bin | vegeta report</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>The above command will attack the localhost for 10s, store the results in <code>results.bin</code> and creates a <code>report</code> in the current directory.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","id":17659,"sizeSlug":"large","linkDestination":"media"} -->
<figure class="wp-block-image aligncenter size-large"><a href="https://qainsights.com/wp-content/uploads/2023/10/image-3.png"><img src="https://qainsights.com/wp-content/uploads/2023/10/image-3-1024x156.png" alt="Deep-dive into Vegeta - HTTP load testing tool and library" class="wp-image-17659"/></a></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>The results will be stored in a <code>bin</code> format. To create a HTML report, use the below command:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>cat results.bin | vegeta plot &gt; plot.html</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>To plot histogram in the terminal, issue the below command:</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>cat results.bin | vegeta report -type="hist&#91;0,100ms,200ms,300ms]"</code></pre>
<!-- /wp:code -->

<!-- wp:heading -->
<h2 class="wp-block-heading">Vegeta defaults</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>By default vegeta uses the following properties:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><!-- wp:list-item -->
<li>CPUs - number of CPUs you have</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Number of connections - 10000</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Rate - 50 requests per second</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>DNS TTL - 0s</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Duration of the test - infinity</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>HTTP2 - if the server supports</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Maximum size of response body - no limit</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Maximum number of workers - 18446744073709551615</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Output file - stdout</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Redirects - 10</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Targets - stdin</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Timeout - 30s</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Workers - 10</li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>There are defaults for plot and report as well. </p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading">Important arguments to remember</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>When running a performance test, it is paramount to configure the right values so that the test will work as per workload model. The following are the important command and arguments to remember in vegeta.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><code>attack</code> - this command generates load and executes the HTTP instructions. It is important to configure the <code>duration</code> of the test in <code>seconds</code> and <code>rate</code> - number of requests per time unit i.e. throughput. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If the HTTP verb is POST, then you need to add appropriate <code>body</code>, <code>headers</code>, <code>tls</code>, <code>redirects</code>, <code>resolvers</code> and more.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>When it comes to <code>report</code>, ensure you mention histogram <code>buckets</code>, <code>output</code> file, and <code>type</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>For the <code>plot</code>, configure the <code>threshold</code>, <code>output</code>, and <code>title</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:heading -->
<h2 class="wp-block-heading">More examples</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>If you are new to vegeta, below are the handy examples which will be useful.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Attacking with headers</h3>
<!-- /wp:heading -->

<!-- wp:code -->
<pre class="wp-block-code"><code>echo "GET http://localhost:8080
X-Track: vegeta-test" | \
vegeta attack -duration=10s | \
tee results.bin | \
vegeta report</code></pre>
<!-- /wp:code -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Attacking with multiple endpoints</h3>
<!-- /wp:heading -->

<!-- wp:code -->
<pre class="wp-block-code"><code>echo "GET http://localhost:8080
X-Track: vegeta-test"

GET http://localhost:8080/leaderboard" | \
vegeta attack -duration=10s -name emojivoto | \
tee results.bin | \
vegeta report</code></pre>
<!-- /wp:code -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Attacking with POST request</h3>
<!-- /wp:heading -->

<!-- wp:code -->
<pre class="wp-block-code"><code>echo "POST http://localhost:8080" | vegeta attack -duration=10s | tee results.bin | vegeta report</code></pre>
<!-- /wp:code -->

<!-- wp:heading -->
<h2 class="wp-block-heading">Attacking with rate</h2>
<!-- /wp:heading -->

<!-- wp:code -->
<pre class="wp-block-code"><code>echo "GET http://localhost:8080" | vegeta attack -duration=10s -rate=100/s | tee results.bin | vegeta report</code></pre>
<!-- /wp:code -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Attacking with payload</h3>
<!-- /wp:heading -->

<!-- wp:code -->
<pre class="wp-block-code"><code>echo "POST http://localhost:8080
@data.json" | vegeta attack -duration=10s | \
tee results.bin | \
vegeta report</code></pre>
<!-- /wp:code -->

<!-- wp:heading {"level":3} -->
<h3 class="wp-block-heading">Attacking with comments</h3>
<!-- /wp:heading -->

<!-- wp:code -->
<pre class="wp-block-code"><code>echo "POST http://localhost:8080
# sending with payload
@data.json" | vegeta attack -duration=10s | \
tee results.bin | \
vegeta report</code></pre>
<!-- /wp:code -->

<!-- wp:heading -->
<h2 class="wp-block-heading">Distributed load testing in Vegeta</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Distributed testing is the core feature for any performance testing tool. It helps in distributing the load to scale the workload. Vegeta's approach is kind of unique while comparing with other tools like JMeter, k6, or Locust. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Vegeta leverages <code>pdsh</code> (Parallel Distributed Shell) is an open-source, high-performance parallel remote shell utility that allows system administrators and users to execute shell commands or scripts on multiple remote hosts simultaneously. It is particularly useful for managing and automating tasks on multiple servers or clusters in parallel.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><code>pdsh</code> is widely used in environments where parallel or distributed system administration and automation are essential. It simplifies the management of large clusters of servers and allows administrators to streamline tasks that would otherwise be time-consuming when performed on each host individually.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>To demo the distributed load testing feature in vegeta, let us spin up multiple virtual machines using <code>multipass</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Multipass is a lightweight, cross-platform virtual machine (VM) management tool developed by Canonical, the company behind Ubuntu Linux. It provides a simple and convenient way to create, manage, and run virtual machines on various operating systems, including Windows, macOS, and Linux. Multipass is designed to be user-friendly, making it easy for developers, sysadmins, and others to work with virtual machines for development, testing, and various computing tasks.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Multipass is commonly used for various purposes, including software development, testing, running isolated development environments, and experimenting with different Linux distributions. It simplifies the process of working with virtual machines, especially for those who prefer a command-line interface and want a straightforward way to create and manage VMs.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>You can check https://multipass.run/install to get started. First, let us create two virtual machines (vm) for this demo. The below command will create two vms <code>worker1</code> and <code>worker2</code> with 2 CPU and 4 GB memory each with Ubuntu 23.04.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>multipass launch --name worker1 23.04 -c 2 -m 4gb </code></pre>
<!-- /wp:code -->

<!-- wp:code -->
<pre class="wp-block-code"><code>multipass launch --name worker2 23.04 -c 2 -m 4gb</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Once it is created, you can list out all the vms using the below command.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>multipass list</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>The next step is to login into the <code>worker1</code> and issue the below commands.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>sudo apt update
sudo apt install golang -y</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>To install vegeta, grab the right download url from https://github.com/tsenart/vegeta/releases/tag/v12.11.1 and download  using the below command.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>curl -L -o vegeta_12.11.1_linux_arm64.tar.gz https://github.com/tsenart/vegeta/releases/download/v12.11.1/vegeta_12.11.1_linux_arm64.tar.gz

tar -xf vegeta_12.11.1_linux_arm64.tar.gz

sudo mv vegeta /usr/local/bin/</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Now repeat the above steps for <code>worker2</code>. Once the vegeta is installed, return to your local to install <code>pdsh</code> using the below command.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>brew install pdsh</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>The next step is to grab the IP address of <code>worker1</code> and <code>worker2</code> using the below command.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>multipass list</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>To SSH into the VMs, you need to copy your public key into them using <code>ssh-copy</code>.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>ssh-copy-id -i ~/.ssh/id_rsa.pub ubuntu@192.168.205.13
ssh-copy-id -i ~/.ssh/id_rsa.pub ubuntu@192.168.205.14</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Now set this environment variable in your local.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>PDSH_RCMD_TYPE=ssh </code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>Trigger the attack</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>pdsh -l ubuntu -b -w '192.168.205.13,192.168.205.14' 'echo "GET http://example.com" | vegeta attack -rate=2 -duration=5s &gt; result.bin'</code></pre>
<!-- /wp:code -->

<!-- wp:list {"ordered":true} -->
<ol><!-- wp:list-item -->
<li><code>pdsh</code>: This is the command used to run a command on multiple remote servers in parallel.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><code>-l ubuntu</code>: This option specifies the username to use when connecting to the remote servers. In this case, it's set to "ubuntu."</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><code>-b</code>: This option tells <code>pdsh</code> to execute the specified command in the background, meaning it will not wait for the command to finish on each remote server before moving on to the next one.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><code>-w '192.168.205.13,192.168.205.14'</code>: This option specifies the list of remote servers to target. In this case, it's set to two IP addresses: 192.168.205.13 and 192.168.205.14.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><code>'echo "GET http://example.com" | vegeta attack -rate=2 -duration=5s &gt; result.bin'</code>: This is the actual command that will be executed on each of the specified remote servers. Let's break it down further:<!-- wp:list -->
<ul><!-- wp:list-item -->
<li><code>echo "GET http://example.com"</code>: This part of the command sends an HTTP GET request to "<a href="http://example.com/">http://example.com</a>." It's used to generate HTTP traffic for the load test.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><code>|</code>: This pipe symbol is used to send the output of the preceding command as input to the following command.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><code>vegeta attack -rate=2 -duration=5s &gt; result.bin</code>: This part of the command utilizes the <code>vegeta</code> load testing tool to perform a load test. Here's what each part does:<!-- wp:list -->
<ul><!-- wp:list-item -->
<li><code>vegeta attack</code>: This is the command to start a load test attack with <code>vegeta</code>.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><code>-rate=2</code>: It specifies the request rate of 2 requests per second.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><code>-duration=5s</code>: The load test will run for 5 seconds.</li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li><code>&gt; result.bin</code>: The output of the load test will be saved to a file called "result.bin." The <code>&gt;</code> symbol is used to redirect the output to the specified file.</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list --></li>
<!-- /wp:list-item --></ul>
<!-- /wp:list --></li>
<!-- /wp:list-item --></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Once the attack is done, it is important to collect the results from each machine using the below command.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>for machine in 192.168.205.13 192.168.205.14; do
    scp ubuntu@$machine:~/result.bin $machine.bin &amp;
done</code></pre>
<!-- /wp:code -->

<!-- wp:paragraph -->
<p>After the results have been collected, below command helps you to plot the combined HTML report.</p>
<!-- /wp:paragraph -->

<!-- wp:code -->
<pre class="wp-block-code"><code>vegeta plot 192.168.205.13.bin 192.168.205.14.bin &gt; combined_report.html</code></pre>
<!-- /wp:code -->

<!-- wp:code -->
<pre class="wp-block-code"><code># for CLI output
vegeta report *.bin</code></pre>
<!-- /wp:code -->

<!-- wp:heading -->
<h2 class="wp-block-heading">Integrations</h2>
<!-- /wp:heading -->

<!-- wp:list -->
<ul><!-- wp:list-item -->
<li>For real time results plotting, vegeta gels with <code>jplot</code> and <code>jaggr</code></li>
<!-- /wp:list-item -->

<!-- wp:list-item -->
<li>Integrates with Prometheus and Grafana</li>
<!-- /wp:list-item --></ul>
<!-- /wp:list -->

<!-- wp:heading -->
<h2 class="wp-block-heading">Conclusion</h2>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>In conclusion, as a performance engineer, it's crucial for you to expand your toolkit with the right tools to succeed in your demanding role. You know that ensuring optimal performance for your applications and systems is an ongoing challenge, but with the right set of tools and techniques, you can tackle this challenge effectively.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Throughout this article, we've taken you on a journey to explore a valuable addition to your toolkit: Vegeta, an HTTP load testing tool and library. You understand that load testing is a fundamental part of evaluating and enhancing system performance, and Vegeta offers a robust solution to help you achieve your goals.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>By delving into the world of Vegeta, we've provided you with insights into its features, capabilities, and practical applications for load testing. Equipped with this knowledge, you'll be better prepared to handle a higher volume of users and requests, all while ensuring your systems maintain peak performance.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>We've covered the basics of installing Vegeta on different platforms and shown you how to perform simple load tests. We've also delved into more advanced topics like distributed load testing using pdsh. You're now aware of the importance of correctly configuring the attack, report, and plot settings to meet your specific testing needs.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Furthermore, we've demonstrated how Vegeta integrates with other tools like jplot and jaggr for real-time results plotting, as well as its seamless integration with Prometheus and Grafana for more comprehensive performance monitoring.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>In your role as a performance engineer, you know that having the right tools can make all the difference. By adding Vegeta to your arsenal, you'll be better equipped to tackle the challenges of performance testing and ensure that the systems you manage continue to perform at their best. So why wait? Dive into the world of Vegeta and elevate your performance engineering expertise today. Your applications and systems will thank you for it.</p>
<!-- /wp:paragraph -->