HTTP latency tester in Deno
Introduction
There is always a need to compare benchmark/compare performance with some other system. It need not be limited to some other type of system. There could be a need to compare performance with an old release, a different type of implementation, a revamp of application, new libraries, etc.
There are numerous open-source benchmarking tools available in the market for different types of performance benchmarking. The tools range from simple (command-line based) to very sophisticated (web interface, graphs, etc.).
Sometimes the need is to simply compare the latency between two services (observed latency for service 1 & service 2). A simple tool without elaborate setup/scripts/configuration would be most useful in such cases.
In this article, we’ll see how to develop a very simple latency comparator tool in Deno. With a few lines of code, a simple & functioning latency comparator can be developed.
Latency comparator
The latency comparator always compares the latency between two SUTs. It allocates workers & then start hitting the services. It continuously measures the latency & reports mean (rolling) latency for both the services.
Inputs
The latency comparator takes the following inputs:
- name1=URL1: The name of the first SUT along with the URL
- name2=URL2: The name of the second SUT along with the URL
- -m: The HTTP method (default GET)
- -c: The number of concurrent connections per SUT (default 10)
- -j: The JSON body to send (must be stringified)
- -f: The path of the file to upload
- -d: A semi-colon separated string of key-value pairs that would be sent as multipart/form-data
The SUT name shouldn’t exceed eight characters.
Outputs
The latency comparator always produces output on the console. A rolling mean of latency for both the SUTs is produced. The readings are colored with green (lower latency) and red (higher latency).
> deno run -q --allow-read --allow-net --allow-hrtime --unstable app.ts deno=http://localhost:3000 node=http://localhost:4000 -j '{"name": "Mayank C"}' -c 50
Code
The complete code (just 290 lines of code) is present in the following GitHub repository:
Examples
Let’s go over some examples. We’ll see file upload, JSON data, and a simple hello world.
Let’s compare Deno & Node.js’s latency for file uploads (sample.pdf). Deno is running on port 3000, while Node.js is running on port 4000.
Here is the default case (10 concurrent connections):
Here is the same case, but with 3 concurrent connections.
Now, let’s compare Deno & Node.js’s latency for JSON data.
Here is the case with 4 concurrent connections.
Here is the case with 50 concurrent connections (Note that 50 is to each SUT, in total 100):
Finally, let’s compare Deno’s old HTTP server implementation with new HTTP server (hyper based). The old HTTP server is running on port 3000, while the new HTTP server is running on port 4000.
This story is a part of the exclusive medium publication on Deno: Deno World.