Skip to content

Commit

Permalink
[BE#332] 소셜 기능 k6 부하 테스트 코드 작성 (#341)
Browse files Browse the repository at this point in the history
* test: 소셜 기능 k6 부하 테스트 코드 작성

- 소셜 기능 부하 테스트 코드 작성
- faker를 이용해서 학습 기록 부하 테스트 데이터 현실적인 데이터로 변경
- 소셜 기능 부하 테스트 데이터 현실적인 데이터 faker로 생성

* fix: 폴링 api와 조회 api 통일해서 테스트
  • Loading branch information
victolee0 authored Dec 6, 2023
1 parent 08b5793 commit 528d4d5
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 9 deletions.
17 changes: 17 additions & 0 deletions BE/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions BE/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"winston": "^3.11.0"
},
"devDependencies": {
"@faker-js/faker": "^8.3.1",
"@nestjs/cli": "^10.0.0",
"@nestjs/schematics": "^10.0.0",
"@nestjs/testing": "^10.0.0",
Expand Down
39 changes: 39 additions & 0 deletions BE/test/load-performance/loadtest-social.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { SharedArray } from 'k6/data';
import http from 'k6/http';
import { check, sleep, group } from 'k6';
import { setInterval, setTimeout, clearInterval } from 'k6/experimental/timers';
import moment from 'https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js';
import faker from 'https://cdnjs.cloudflare.com/ajax/libs/Faker/3.1.0/faker.min.js';

const tokens = new SharedArray('possible tokens', function () {
const mock_users = JSON.parse(open('./loadtest-mock.json'));
return mock_users.map(({ access_token }) => access_token);
});

export let options = {
stages: [
{ duration: '30s', target: 50 },
{ duration: '20s', target: 0 },
],
thresholds: {
http_req_duration: ['avg<200', 'p(95)<500', 'max<1000'],
http_reqs: ['rate>100'],
},
};

export default function () {
const token = tokens[__VU % tokens.length];
const url = 'http://localhost:3000/mates';
const params = {
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json',
},
};

const date = moment(faker.date.between('2023-11-23', '2023-11-30'));
check(http.get(`${url}?date=${date}`, params), {
'친구 조회 응답 시간이 1초 이내': (r) => r.timings.duration < 1000,
});
sleep(3);
}
30 changes: 21 additions & 9 deletions BE/test/load-performance/loadtest-study.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { SharedArray } from 'k6/data';
import http from 'k6/http';
import { check, sleep, group } from 'k6';
import moment from 'https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js';
import faker from 'https://cdnjs.cloudflare.com/ajax/libs/Faker/3.1.0/faker.min.js';

const tokens = new SharedArray('possible tokens', function () {
const mock_users = JSON.parse(open('./loadtest-mock.json'));
Expand All @@ -9,9 +11,8 @@ const tokens = new SharedArray('possible tokens', function () {

export let options = {
stages: [
{ duration: '30s', target: 10 }, // 사용자 수를 1분 동안 500까지 증가

{ duration: '16s', target: 0 }, // 사용자 수를 10초 동안 0으로 감소
{ duration: '30s', target: 50 },
{ duration: '10s', target: 0 },
],
thresholds: {
http_req_duration: ['avg<200', 'p(95)<500', 'max<1000'],
Expand All @@ -25,13 +26,24 @@ export default function () {
const params = {
headers: {
Authorization: `Bearer ${token}`,
'Content-Type': 'application/json', // JSON Content-Type 헤더 추가
'Content-Type': 'application/json',
},
};
group('start 응답 시간이 1초 이내', function () {
const start_date = moment(faker.date.between('2023-11-23', '2023-11-30'));

const start_created_at = faker.date.between(
start_date.format('YYYY-MM-DD'),
start_date.add(1, 'd').subtract(1, 's').format('YYYY-MM-DD'),
);
const learning_time = faker.random.number({
min: 60 * 30,
max: 60 * 60 * 5,
});

const start_body = JSON.stringify({
date: '2023-11-23',
created_at: '2023-11-23T23:01:02+09:00',
date: start_date,
created_at: start_created_at,
type: 'start',
learning_time: 0,
});
Expand All @@ -40,10 +52,10 @@ export default function () {
});
sleep(4);
const finish_body = JSON.stringify({
date: '2023-11-23',
created_at: '2023-11-23T23:01:02+09:00',
date: start_date,
created_at: moment(start_created_at).clone().add(learning_time, 's'),
type: 'finish',
learning_time: 2222,
learning_time: learning_time,
});
check(http.post(url, finish_body, params), {
'finish 응답 시간이 1초 이내': (r) => r.timings.duration < 1000,
Expand Down

0 comments on commit 528d4d5

Please sign in to comment.