개발일지

개발일지 81일차

index.ys 2023. 6. 24. 03:09

로컬환경에서 nginx 설정하기

  • 로컬환경에서 모의 부하테스트 하기 위해 ngxin로 프록시 서버를 구축했다

nginx 설정파일

  • 8001번 포트로 접속되는 요청들을 3000 , 3001 ,3002번 3개의 포트로 분할해서 처리한다
  • mywas라는 upstream모듈을 만들고
  • proxy_pass 설정으로  http요청시 mywas모듈로 요청을 포트포워딩함
  • 포트포워딩된 요청은 3000,3001,3002 포트에서 분할되어 실행됨
http {
    include       mime.types;
    default_type  application/octet-stream;

    upstream mywas {     
        server localhost:3000;
        server localhost:3001;
        server localhost:3002;
     
    }
    #log_format  main  '$remote_addr - $remote_user [$time_local] "$request" '
    #                  '$status $body_bytes_sent "$http_referer" '
    #                  '"$http_user_agent" "$http_x_forwarded_for"';

    #access_log  logs/access.log  main;

    sendfile        on;
    #tcp_nopush     on;

    #keepalive_timeout  0;
    keepalive_timeout  65;

    #gzip  on;

    server {
        listen       8001;
        server_name  localhost;

        #charset koi8-r;

        #access_log  logs/host.access.log  main;

        location / {
            proxy_pass http://mywas;
            proxy_set_header Host $host;
            proxy_set_header X-Real-IP $remote_addr;
        }
        
  }

트래픽 분산 확인

  • 간단한 node.js 코드를 작성후 3개의 서버를 실행함
const express = require('express');
const morgan = require('morgan');

const app = express();
const port = 3000; //3001, 3002

//app.use(morgan('combined'));

app.get('/', (req, res) => {
    const { method, url, headers } = req;
    const remoteAddress = req.socket.remoteAddress;
    const remotePort = req.socket.remotePort;
    const ip = req.ip;
    console.log(ip)
    console.log(`Request received on ${url} (${method}) from ${remoteAddress}:${remotePort}`);

    res.writeHead(200, { 'Content-Type': 'text/plain' });
    res.end('Hello, World!');
});

app.listen(port, () => {
    console.log(`Server running at http://localhost:${port}`);
});

3개의 서버 실행

  • 3000~3002번 포트를 동시에 실행하고 요청이 분산되어 들어오는지 확인

8001번 포트로 get요청

1번째 요청

2번째 요청

3번째 요청

각각 다른 포트에서 다른 프로세스를 할당 받아 실행중인 것을 확인 할 수 있음.

8001번 포트로 부하테스트

  • artrillery설치
> npm i -D artillery
> npm start
  • artrillery 간단한 부하 실행 8001번 포트로 100명의 사용자가50번의 요청을 보냄 => 총 5000번의 요청
> npx artillery quick --count 100 -n 50 http://localhost:8001

테스트 결과

//모든 가상사용자 요청을 처리하는데 걸린 시간 => 16초
All VUs finished. Total time: 16 seconds

--------------------------------Summary report @ 03:33:10(+0900)--------------------------------
// http 상태코드 200으로 받은 응답수 => 5000번
http.codes.200: ................................... 5000        
// 다운로드된 총 바이트 수 65,000바이트
http.downloaded_bytes: ............................ 65000  
// 초당 평균 요청수 => 1초에 316개의 요청 처리
http.request_rate: ................................ 316/sec     
// 전체 요청수 => 5000번
http.requests: .................................... 5000        
// 응답 시간 통계
http.response_time:
// 가장빠른 응답 => 0초
  min: ............................................ 0
// 가장 느린응답 => 0.353초
  max: ............................................ 353
// 응답시간 평균값 => 0.247초
  median: ......................................... 149.9       
// 95%응답이 완료되기까지 걸린시간 0.247초
  p95: ............................................ 247.2       
// 99%응답이 완료되기까지 걸린 시간 0.278초
  p99: ............................................ 278.7
http.responses: ................................... 5000
//완료된 가상 사용자 수 => 100명의 가상 사용자
vusers.completed: ................................. 100
//생성된 가상 사용자 => 100명
vusers.created: ................................... 100
vusers.created_by_name.0: ......................... 100
//실패한 가상 사용자 => 0명
vusers.failed: .................................... 0
vusers.session_length:
//가장 짧은 세션 길이는 3.546초입니다.
  min: ............................................ 3546.3
//가장 긴 세션 길이는 9.669초입니다.
  max: ............................................ 9669
//세션 길이의 중간값은 8186.6초입니다.
  median: ......................................... 8186.6
//95%의 세션이 완료되기까지 걸린 시간은 9.416초입니다.
  p95: ............................................ 9416.8
//99%의 세션이 완료되기까지 걸린 시간은 9.416초입니다.
  p99: ............................................ 9416.8