Skip to main content Link Menu Expand (external link) Document Search Copy Copied

created at 2023-01-17

실시간 채팅방 backend에 직접 대량의 Rest api test를 진행했었다. 그런데 이 rest api test가 매번 직접 설정해주기 힘들어서 이를 Docker-compose 와 Viper 로 자동화 할 것이다. 아래는 자동화 시키고 실행한 결과를 먼저 보인다. 사용법과 코드는 https://github.com/ghkdqhrbals/multiple-restapi-request-test 에 업로드하였다.

주의점!

프록시 서버가 컨테이너로 실행되고, 호스트한테 프록시 포트가 expose되어있다면, 그대로 docker-compose up을 실행하면 된다. 하지만 내부 포트로 expose되어 있다면, 같은 네트워크로 묶어줘야한다!!

실행 결과

test-multiple-http-request  | Request url: http://127.0.0.1:8080/auth/user
test-multiple-http-request  | The number of HTTP Requests: 10000
test-multiple-http-request  | The number of threads: 100
test-multiple-http-request  | Proceeding! Please wait until getting all the responses
test-multiple-http-request  | Elapsed Time: 30.533003028
test-multiple-http-request  | Response status code:  200 , How many?:  10000

총 10K개의 rest api call 을 진행하였고, 100개의 go-routine으로 진행한다. 결과로 약 30.5초가 소요되었으며, 전부 200 status code를 반환 받은 것을 확인할 수 있다.

흐름도는 아래와 같다.

app.env 설정
--(Viper)--> go build
--> build docker images
--> run docker container(network:host)
--> nginx
--> auth-server

아래는 이미지 생성에 필요한 Dockerfile

# Build stage
FROM golang:1.18.3-alpine3.16 AS builder
WORKDIR /app
COPY . .
RUN go build -o main main.go


# Run stage
FROM alpine:3.16
WORKDIR /app
COPY --from=builder /app/main .
COPY app.env .

CMD ["/app/main"]

아래는 docker-compose.yaml

version: '3'
services:
  multi-test:
    container_name: test-multiple-http-request
    build:
      context: .
      dockerfile: Dockerfile
    ports:
      - "8002:8002"
    command: ["/app/main"]
    network_mode: "host"