Your browser doesn't support the features required by impress.js, so you are presented with a simplified version of this presentation.

For the best experience please use the latest Chrome, Safari or Firefox browser.

Stress Testing Realtime Node.js Apps


  1. Project background
  2. Building an application
  3. Configuring a server
  4. Gathering a swarm
  5. Demonstration

Project Background

  1. PBS built a client-side Electoral College map
  2. We built a real-time backend
  3. We researched production readiness

Project Background: PBS's Offline Application

Project Background: Our Realtime Extension

Building the Application

The Client: Modularization

Intro to

"Socket.IO aims to make realtime apps possible in every browser and mobile device, blurring the differences between the different transport mechanisms. It's care-free realtime 100% in JavaScript."

Intro to (cont.)

Sometimes called "the jQuery for realtime applications."

Two components:

The Client: Endpoint Parameterization

// Client source file
var defaultOptions = {
    // These values are expanded at build time.
    host: "",
    port: ""

The Client: Endpoint Parameterization (cont.)

// Build script (Grunt)
NODE_HOST: process.env.NODE_HOST || "",
NODE_PORT: process.env.NODE_PORT || 8000,

// Inject NODE_HOST and NODE_PORT values into files as they are
// copied
processContent: function(content) {
  return content.replace(/\{\{\s*(NODE_HOST|NODE_PORT)\s*\}\}/g,
      function(match, varName) {
          return grunt.config("meta." + varName);

(Mostly) Avoiding fs.*Sync()

Generally a bad practice--it blocks the execution thread.

Acceptable during application initialization.

Advanced usage: Learning the API

Advanced usage: Dynamic connection handler

// One approach
socketServer.sockets.on("connection", function(socket) {
  if (app.isLive()) {
    connectLive.apply(socket, arguments);
  } else {
    offlineConnection.apply(socket, arguments);

Advanced usage: Dynamic connection handler

// Another approach
socketServer.sockets.on("connect", function(socket) {
  socketHandlers.connection.apply(socket, arguments);

// elsewhere...
socketHandlers.connection = connectLive;

Warning! Version 1.0 is on its way!

Configuring a Realtime Server

Security: Threat Model

Security: Precautions

Technical requirements: Node.js

Install to /opt/joyent/node/ (check out the Filesystem Hierarchy Standard for some light reading)

$ sudo chown users:dev /opt/joyent/node

Technical requirements: Locking down dependencies

Data collection tools: htop

apt-get install htop

Data collection tools: SAR

apt-get install sysstat

Gathering a Swarm

Client Simulator: Design

A script that simulates many people using your application simultaneously
Possible solutions
  • Script a real browser to run your application (i.e. Selenium)
  • Load your application in PhantomJS
  • Strip out the network module and run that from Node.js

Client Simulator: Technical Considerations

Aside: Modifying upstream dependencies

I wanted to simulate long-polling connections from Node.js (see how). required a patch to support this.

  1. Before that was merged, we needed to use our fork of the project.
  2. After it was merged, we needed to specify the dependency with a git URL
  3. After a new version was published, we could have simply specified the latest version.

Small-scale Demonstration


"Amazon Web Services offers a complete set of infrastructure and application services that enable you to run virtually everything in the cloud: from enterprise applications and big data projects to social games and mobile apps."

Lucky for us, there's a Free usage tier.

* Amazon refused to pay me for this slide

AWS (cont.)

Bees with Machine Guns

A utility for arming (creating) many bees (micro EC2 instances) to attack (load test) targets (web applications).
Bees with Machine Guns project page

My awful hacked-up fork


"But... why?!"

We need a slightly more generalized approach to run our Node.js client simulator.


What's Next?


Use a spacebar or arrow keys to navigate