diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 05b1e5a..a7a544c 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -31,3 +31,40 @@ jobs: - name: Run tests with pytest run: | pytest -v + load-test: + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Set up Python + uses: actions/setup-python@v4 + with: + python-version: "3.10" + + - name: Install Python dependencies + run: | + python -m pip install --upgrade pip + pip install -r requirements.txt + + - name: Initialize database + run: | + python init_db.py + + - name: Install k6 + run: | + sudo gpg -k + sudo gpg --no-default-keyring --keyring /usr/share/keyrings/k6-archive-keyring.gpg --keyserver hkp://keyserver.ubuntu.com:80 --recv-keys C5AD17C747E3415A3642D57D77C6C491D6AC1D69 + echo "deb [signed-by=/usr/share/keyrings/k6-archive-keyring.gpg] https://dl.k6.io/deb stable main" | sudo tee /etc/apt/sources.list.d/k6.list + sudo apt-get update + sudo apt-get install k6 + + - name: Start FastAPI server + run: | + uvicorn app.main:app --host 0.0.0.0 --port 8000 & + sleep 5 # 서버가 시작될 때까지 대기 + + - name: Run k6 load test on slow endpoint + run: | + k6 run k6-test.js -e BASE_URL=http://127.0.0.1:8000 \ No newline at end of file diff --git a/k6-test.js b/k6-test.js new file mode 100644 index 0000000..67206c5 --- /dev/null +++ b/k6-test.js @@ -0,0 +1,22 @@ +import http from "k6/http"; +import { check, sleep } from "k6"; + +export const options = { + stages: [ + { duration: "10s", target: 10 }, + { duration: "20s", target: 10 }, + { duration: "10s", target: 0 }, + ], + thresholds: { + http_req_duration: ["p(95)<500"], + }, +}; + +export default function () { + const res = http.get("http://127.0.0.1:8000/api/posts/v1/slow"); + check(res, { + "is status 200": (r) => r.status === 200, + "response time < 500ms": (r) => r.timings.duration < 500, + }); + sleep(1); +}