What do you think about serverless? Is it the best thing ever - or just another terrible idea? Regardless if you love or hate the term or technology, I think universal access to it is critical. And since Rust is fast, safe, and simply awesome I’ll give that a try!

Node, wasm & Rust

As a high-level recap: NodeJS is a runtime for JavaScript applications based on Google’s V8 engine. It’s highly optimized and provides a way to create JavaScript-based (incl. derivatives like TypeScript or CoffeeScript) server applications. This works just like in the browser and interprets text and compiles it at runtime (JIT).

A major drawback of this approach always has been the need to interpret the actual code to run it. As a consequence running a JavaScript application (in the browser or server) is comparatively slow and large - something that could easily be solved by shipping binary executables instead. One approach to that has now been refined by web assembly, which was built from asm.js.

Rust on the other hand is a completely different beast. It compiles to native code using the popular LLVM toolchain, which also provides flexible compilation targets (e.g. CPU architectures) by using an intermediate representation (LLVM IR) which is then further turned into native code of the desired architecture. I thought that this is a really neat idea and provides a great tool for building new languages. Other than Rust a few others (like Swift) use this approach as well!

Be Fearless with Serverless

Fearless concurrency has been a motto of the Rust community for a while now, maybe we can expand that to going serverless? To recap: Serverless is just a name for a plattform that only runs a function (instead of entire applications like a regular PaaS offering), which is a nice way to save on server/operating/framework/etc. maintenance and scaling! While there is someone maintaining servers for you, for simple use cases this is a great way of getting up and running quickly.

For this blog post I am going to use Azure Functions (part of Microsoft’s cloud offering), which will be based on the Azure WebJobs SDK once its runtime 2.0 is out of preview. The current version unfortunately is not open source and only runs NodeJS version 5.x, which does not support web assembly.

Let’s look into how well that works then! 😁

Sharing a Pi(e)

As a first step we need a “hard” problem, something that takes a while to compute. For this, we are going to approximate Pi by using the Monte Carlo method! It “computes” Pi like that:

Generally, I would say these two are very much the same. The code extern { fn random() -> f64 } declares a function random() that is passed in from somewhere else - i.e. the NodeJS runtime. We can bind any function we want to that and in this case it’s Math.random().

Function setup

Azure functions can be deployed in several ways, but in order to add other files (like a wasm binary), Github is quite convenient and using the webhook re-deploy it’s a tiny CD system. For the setup to work each repository needs a defined structure, that you can find in the docs

Check out the GitHub repository for the project as another example. For this experiment, I am going to use a HTTP triggered function!

Getting the WASM

As explained in this post, the first thing to do is installing the wasm32 toolchain using rustup. With that handy, a single command will build everything:

cargo +nightly build --target wasm32-unknown-unknown --release

This produces some binary output in the target/wasm32-unknown-unknown directory that then can be processed further using wasm-bindgen:

By running this command, two additional files are generated that take care of the loading and export/import of all declared functions in JavaScript. Since this is an ongoing effort improve the process, look at the wasm-bindgen repository for more information!

Unfortunately I could not use the rand crate at this time since the 0.5 preview version wouldn’t build on my machine and the 0.4 version did not work with wasm (it did build though).

Local testing

Having a .wasm file ready and the includes set up in the .js files, we can now move on to testing. The test code is simply running 10 million iterations (a random choice really) of the simulation to approximate Pi:

Using time to track the elapsed time we can roughly estimate the speed of each of the implementations. Let’s see …

… no. Contrary to my expectations the local tests showed that the wasm implementation is considerably slower (0.51 vs 0.30 seconds, a more than 60% increase) than its JavaScript relative. This definitely needs more investigation! (Node version was 9.4.0)

To reproduce this experiment, you can use the local.js files in each function’s folder.

Into the clouds

First, the appropriate version of node is required. This can be done by enabling preview/beta features in the application settings.

Then an environment variable needs to be set to the appropriate version of node and after some trial and error, this was the highest version I could get:

This should do the trick! Let’s deploy and see how well it does compared to the local counterparts.

Loading it up

For these RESTful endpoints to use appropriate testing, I am going to use Visual Studio Team Services’ load testing tool. It utilizes Azure resources to issue specified requests to a provided endpoint and adds some nice stats and charts to it. This service is also free and I posted about it a while back.

Let’s do two tests, one for each URL and check the response times. After a minute of constant load of 25 concurrent ‘users’, here’s what I got:

Same result 😢! Yet Functions are able to reproduce my local results, which means that it works well in the cloud and that the issue could be with the code itself or due to the early stage of wasm.

That’s all folks

While I am honestly quite shocked by the local performance of wasm with NodeJS, I am even more happy that it works well and it makes running small Rust projects in the cloud easier. Hopefully the performance will improve in the future (I am not sure what’s missing at this point, let me know on twitter if you know!), and I will continue to run tests and various experiments on Azure services to get Rust closer to the cloud 😁.