Load Testing NodeJS on Multiple Cores
Check Out the Git Hub project HERE
There are a few ways to use multiple cores in NodeJS, two of which are Cluster (part of the NodeJS API) and WebWorker Threads.
I will preface this by saying I am no NodeJS expert, this project was simply for learning on my part and there may be a better way to go about this with an NGinx set up, but I wanted to share my findings and maybe you'll find it cool too.
I will preface this by saying I am no NodeJS expert, this project was simply for learning on my part and there may be a better way to go about this with an NGinx set up, but I wanted to share my findings and maybe you'll find it cool too.
The Planning Stages
I started the project out with a simple NodeJS and Express server, which when called with a number in the query, would calculate and return the Fibonacci sequence for that number to simulate high CPU calculations.
My first thought was to use Web Worker Threads to spin up a thread to do work on, and just pass in the data and let it work on a background thread, but this caused issues under high load as too many threads were being spun up (I had no cap at the time) and at the start they were descoping and causing segmentation faults.
I ended up fixing the Seg Fault issue, but even so, they were burning through all of my VMs 2GB of memory and crashing.
I attempted to create a Worker Pool function to control the amount of threads at any one time, but it quickly got quite complicated. The idea was to use a queue and simply drop information off and process when ready, but with web requests and waiting on data, this as well got very complicated.
Then I stumbled across the Cluster section of the NodeJS API and found my solution. With the Cluster API you can spin up multiple instances of your node server on the same port. So I decided to test if this truly would improve my performance.
Load Testing
I installed loadtest in order to deliver high concurrency testing to my application and began to test the application. On one branch of my git project I had the vanilla, single threaded NodeJS server which would generate a Fibonacci sequence, and on my other branch I had my project which generated 4 servers on a single port, which would use up the multiple cores.
This was a MUCH simpler solution to building a multi-threaded system within a single event loop and was extremely easy to set up.
So the load testing began. I called out to my application like so:
loadtest -t 20 -c 32 http://localhost:3030/fib?num=25
I pinged my application using various concurrency values for 20 seconds testing periods. I tested up from 1 concurrent connection up to 1000 connections.
While I don't believe all four cores were actually being used (as it was on a VM and my computer didn't crash) I did see quite an improvement using the multiple core approach (as one would expect).
The Results
The results from the load test are as follows. Load test outputs the maximum response times for 50%, 90%, 95%, 99%, and the maximum response time as well as Requests Per Second, Mean response time, and Total Requests.
Computer Specs: The VM was an Ubuntu 14 VM with 2 GB Ram, 4 processors.
Each request was made with a Fibonacci sequence of 25 for 20 seconds.
Computer Specs: The VM was an Ubuntu 14 VM with 2 GB Ram, 4 processors.
Each request was made with a Fibonacci sequence of 25 for 20 seconds.
NodeJS Single Event Loop
Concurrency | Completed Requests | 50%(ms) | 90%(ms) | 95%(ms) | 99%(ms) | Max (ms) | Req / Sec | Mean Lat (ms) |
---|---|---|---|---|---|---|---|---|
64 | 8891 | 135 | 169 | 180 | 245 | 286 | 429 | 150 |
200 | 8110 | 336 | 1276 | 1358 | 3300 | 7364 | 405 | 480 |
1000 | 8178 | 1235 | 3726 | 7626 | 15726 | 16256 | 409 | 2510 |
NodeJS Multi-Core with Cluster
Concurrency | Completed Requests | 50%(ms) | 90%(ms) | 95%(ms) | 99%(ms) | Max (ms) | Req / Sec | Mean Lat (ms) |
---|---|---|---|---|---|---|---|---|
64 | 15009 | 70 | 155 | 193 | 327 | 560 | 750 | 80 |
200 | 14498 | 252 | 476 | 567 | 756 | 1062 | 596 | 330 |
1000 | 14923 | 1172 | 1954 | 2538 | 3618 | 7641 | 718 | 1350 |
As you can see the results from using the multi-core approach were much better than the single event loop, and using Cluster is super easy to do.
You can fork the GitHub project here: https://github.com/WakeskaterX/NodeThreading
The main fork is (well master too, but) hostedVM which has the standard deployment of nodeJS and express, and the multi-core approach is on the hostedVM_multicore branch.
Feel free to test it as well and let me know how your results are with better machines than my very low powered VM.
Cheers,
WakeskaterX
No comments:
Post a Comment