[TIL] Check Node.js http server performance : May 14, 2020

RE_BROTHER·2020년 5월 15일
0

TIL

목록 보기
14/41
post-thumbnail

10초간 서버에 어느정도 요청을 보낼 수 있는지 테스트할 기회가 생겨 node.js Server Performance Test를 해볼까 한다.
대략적인 생각으로는 http 서버를 구동하고, client가 10초간 서버에 요청을 보내고 1회 요청 당 performance_value 라는 변수에 +1을 해주는 식으로 진행을 해볼까 한다.

Node.js http server

일단 Node.js로 서버를 구축하고 해당 서버에 지속적으로 요청을 날려볼까 한다.

// Date : 2020-05-10
// Time : 23:29
// File : index.js
// Dir : /home/testID/node-test

const http = require('http')
const PORT = 8001
const server = http.createServer ((req, res) => {
	// If you receive the response you want, send it to the header
    res.writeHead(200, {'Content-Type':'text/plain'})
    res.end("RE_BROTHER")
})

server.listen(PORT)

Client가 Server에 request을 보낼 시 "RE_BROTHER" 라는 response를 돌려준다

Node.js http client

// Date : 2020-05-14
// Time : 11:59
// File : client.js
// Dir : /home/testID/node-test

const http = require('http')

// Include information about the server to connect
const = options = {
    host: "127.0.0.1",
    port: 8001,
    path: "/"

// Page call
const req = http.request(options, (res) => {
    let data = ""
    res.on('data', (chunk) => {		// Receiving data from the server
        data += chunk
    })
    
    res.on('end', () => {		// Output on receipt completion
        console.log(data)
    })
})

req.end()

일단 Client가 Server에 1회 요청을 보내고 Server의 response를 출력하는 코드를 작성했다.
이제 10초간 지속적으로 요청을 보내는 function을 추가해보자.

const http = require("http")
let performance = 0

const options = {
        host: "127.0.0.1",
        port: 8001,
        path: "/"
}
if (true) {
    let loop = setInterval(() => {
        const req = http.request(options, (res) => {
            res.on('end', () => {
            })
        })
        req.end()
        performance += 1
        console.log('Response for 10 seconds :', performance)
    }, 0.000001)
    setTimeout(()=>{clearInterval(loop)}, 10001)
}

Client가 Server에서 Response를 받으면 performance에 1을 가산하여 10초간 res를 측정을 진행했다.
htop를 보니 생각보다 CPU 리소스를 많이 쓰지는 않는다.

10초동안 총 11071개의 Response를 받았으며, 단순 계산으로 1초당 약 1107개의 응답을 받았다고 할 수 있겠다.

profile
I hope the All-Rounder Developer & Researcher

0개의 댓글