JavaScript Express Application

infiniSoft Blog > Blog > Solution > JavaScript Express Application

To start let’s create general node.js application and add Express module:

npm init -y && npm i express

Now we’ll add index.js file that implements logic of the dispatcher:

const express = require('express')
const bodyParser = require('body-parser')
const app = express()
const port = 3000

let storage = {}

function stringToHash(string) {
  var hash = 0;

  if (string.length == 0) return hash;

  for (i = 0; i < string.length; i++) {
    char = string.charCodeAt(i);
    hash = ((hash << 5) - hash) + char;
    hash = hash & hash;
  }

  return hash;
}

function nameToHash(name) {
  return stringToHash(name.firstName) ^ stringToHash(name.lastName)
}

app.use(bodyParser.json())
app.use(
  bodyParser.urlencoded({
    extended: true,
  })
)

app.post('/name', (request, response) => {
  const names = request.body
  let resp = {}
  let temp = {}
  names.forEach(name => {
    if(nameToHash(name) in storage) {
      resp[nameToHash(name)] = name
    } else {
      temp[nameToHash(name)] = name
    }
  })
  Object.keys(temp).forEach(key => storage[key] = temp[key])
  response.status(200).send(Object.values(resp))
})

app.delete('/name', (request, response) => {
  request.body.forEach(name => delete storage[nameToHash(name)])
  response.status(200).send("")
})

app.listen(port, () => {
  console.log(`App running on port ${port}.`)
})

Running application as node index.js with further tests in Postman confirms that the logic works similar to what we recently implemented in Java and C#. Node that server runs on port 3000:

Repeat request to see exclusion:

And cleanup:

It’s time to dockerize application – add the following Dockerfile:

FROM node:16

WORKDIR /usr/src/app

COPY package*.json ./

RUN npm install

COPY . .

EXPOSE 3000
CMD [ "node", "index.js" ]

Build image as:

docker build . -t infinisoft/dispatcher-js

As before, take a note that image size is 919MB – so far the largest.

Run application in Docker as docker run -p 3000:3000 -d infinisoft/dispatcher-js and note initial memory consumption: 16.3-13.29MiB (size changes when application starts.)

Next let’s run updated JMeter scenario (attached) to populate and clean up dispatcher cache. As before, we’ll run 100 threads in 100 cycles with 1 second ramp-up period.

On completion in 35 seconds (AMD FX-8320 with 16GB RAM) the Apache JMeter shows:

During test we’ll notice that application uses only single CPU core, 85.73MiB of RAM (which drops to 19.97 after garbage collection.)

Leave a Reply

Your email address will not be published. Required fields are marked *

About This Site

This may be a good place to introduce yourself and your site or include some credits.

Recent Comments

    Archives

    Find Us

    Mailing Address
    1703 Evans Rd 9108
    San Antonio, TX 78258

    Hours
    Monday–Friday: 9:00AM–5:00PM
    Saturday & Sunday: 11:00AM–3:00PM