Nodejs is very fast, at least faster than java and c#

Nodejs is very fast, at least faster than java and c#

2016/03/28 Programming 0

Nodejs is very fast, at least faster than java and c#, here is my test case:

  1. JMeter send rest api request from Tai Wai to Mong Kok, below is the trace route

    /Users/peter>traceroute 210.5.164.14
    traceroute to 210.5.164.14 (210.5.164.14), 64 hops max, 52 byte packets
    1 192.168.10.1 (192.168.10.1) 2.463 ms 3.169 ms 0.995 ms
    2 058176102001.ctinets.com (58.176.102.1) 1.937 ms 2.291 ms 3.107 ms
    3 10.239.62.109 (10.239.62.109) 3.146 ms 3.196 ms 3.071 ms
    4 014199252053.ctinets.com (14.199.252.53) 7.214 ms 5.729 ms 3.998 ms
    5 014136128118.ctinets.com (14.136.128.118) 3.191 ms 1.983 ms 1.980 ms
    6 newtt4-10g.hkix.net (123.255.91.243) 3.834 ms 3.466 ms 4.204 ms
    7 115.160.187.115 (115.160.187.115) 4.708 ms 6.820 ms 5.003 ms
    8 175.45.53.210 (175.45.53.210) 20.109 ms 7.609 ms 4.958 ms
    9 210.5.164.14 (210.5.164.14) 5.558 ms 6.597 ms 7.165 ms[/code

  2. Jmeter settings are 8 threads, 100 request per thread
  3. API get the username and password from post, read the db and check the password is correct or not, return a json string
  4. I use sequelize as ORM, no tuning. Mysql also no tuning

Here is the result, most of the response below 20ms, 50% of the response below 10%, remember one thing i didn’t tune my network, code, database and OS !!!

nodejs test 1nodejs test 3nodejs test 1

Here is the server side code:

 

var db = require('../db')

exports.test = function(req, res) {
	res.json({
		message : "First API!"
	});
};

exports.register = function(req, res) {
	var parameters = [ 'email', 'age', 'gender', 'password' ];
	for ( var p in parameters) {
		if (req.body[parameters[p]] == null) {
			return res.send('Error, parameter ' + parameters[p] + ' is missing');
		}
	}
	db.User.findAndCountAll({
		where: {
			email: {
				$like: req.body.email.toLowerCase()
			}
		}
	}).then(function(result) {
		if (result.count==0){
			console.log(result.rows);
			var user = db.User.build({
				email : req.body.email.toLowerCase(),
				age : req.body.age,
				gender : req.body.gender,
				password : req.body.password
			});
	
			user.save().then(function() {
				res.json({
					message : "Register API",
					status : "ok"
				});
			}).catch(function(error){
				res.json({
					message : "Error : "+error,
					status : "error"
				});
			});
		}else{
			res.json({
				message : "User already exist",
				status : "error"
			});
		}
	});
};

exports.login = function(req, res) {
	var parameters = [ 'email', 'password' ];
	for ( var p in parameters) {
		if (req.body[parameters[p]] == null) {
			return res.send('Error, parameter ' + parameters[p] + ' is missing');
		}
	}
	db.User.findAndCountAll({
		where: {
			$and: [
				{
					email: {
						$like: req.body.email.toLowerCase()
					},
					password: {
						$like: req.body.password
					}
				}
			]
		}
	}).then(function(result) {
		if (result.count==1){
			res.json({
				message : "Login successfully",
				status : "ok"
			});
		}else{
			res.json({
				message : "Login failed",
				status : "error"
			});
		}
	});
};

exports.getChatMessage = function(req, res) {
	var parameters = [ 'number', 'highestChatNo' ];
	for ( var p in parameters) {
		if (req.body[parameters[p]] == null) {
			return res.send('Error, parameter ' + parameters[p] + ' is missing');
		}
	}
	db.Message.findAll({
		where: {
			$and: [
				{
					room: {
						$like: req.body.room
					},
					id: {
						ge: req.body.highestChatNo
					}
				}
			]
		}
	}).then(function(result) {
		res.json(result);
	});
};

exports.getHotRooms = function(req, res) {
	db.Stock.findAll({ limit: 8 }).then(function(result) {
		res.json(result);
	});
};