-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from brenoma/feature/stress-test
Stress testing
- Loading branch information
Showing
5 changed files
with
132 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
import { AppService } from './app.service'; | ||
|
||
@Controller() | ||
export class AppController { | ||
constructor(private readonly appService: AppService) {} | ||
|
||
@Get() | ||
getHello(): string { | ||
return this.appService.getHello(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
|
||
@Injectable() | ||
export class AppService { | ||
getHello(): string { | ||
return 'Hello World!'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { check } from 'k6'; | ||
import http from 'k6/http'; | ||
import { Rate } from 'k6/metrics'; | ||
import faker from 'https://unpkg.com/[email protected]/dist/faker.js'; | ||
|
||
const loginFailedRate = new Rate('failed login request'); | ||
const registerFailedRate = new Rate('failed register request'); | ||
const emailDomain = Math.random().toString(36).substring(2, 15) + '.lol'; | ||
|
||
export function requestLogin(baseUrl) { | ||
const payload = JSON.stringify({ | ||
username: '[email protected]', | ||
password: '123', | ||
}); | ||
const params = { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}; | ||
const res = http.post(`${baseUrl}/users/login`, payload, params); | ||
|
||
const result = check(res, { | ||
'Login successfully': res => res.status === 201, | ||
'JSON response': res => /json/.test(res.headers['Content-Type']), | ||
}); | ||
loginFailedRate.add(!result); | ||
} | ||
|
||
export function requestRegister(baseUrl) { | ||
const payload = JSON.stringify({ | ||
name: faker.name.findName(), | ||
email: `stress_${__VU}+${__ITER}@${emailDomain}`, | ||
password: faker.internet.password(), | ||
role: 'admin' | ||
}); | ||
const params = { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}; | ||
const res = http.post(`${baseUrl}/users/create`, payload, params); | ||
|
||
const result = check(res, { | ||
'Register successfully': res => res.status === 201, | ||
'Authorization header': /Bearer\s+.*/.test(res.headers.Authorization), | ||
'JSON response': res => /json/.test(res.headers['Content-Type']), | ||
}); | ||
registerFailedRate.add(!result); | ||
} | ||
|
||
export function requestFind(baseUrl) { | ||
const payload = JSON.stringify({ | ||
email: '[email protected]', | ||
}); | ||
const params = { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}; | ||
const res = http.post(`${baseUrl}/users/find`, payload, params); | ||
|
||
check(res, { | ||
'Get user session': res => res.status === 201, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import http from 'k6/http'; | ||
import { sleep, check, group } from 'k6'; | ||
|
||
import { requestLogin, requestRegister, requestFind } from './auth.js'; | ||
|
||
const baseUrl = 'http://localhost:3000'; | ||
|
||
export const options = { | ||
vus: 30, | ||
duration: '30s', | ||
discardResponseBodies: true, | ||
thresholds: { | ||
checks: ['rate>0.9'], | ||
'failed login request': ['rate < 0.1'], | ||
'failed register request': ['rate < 0.1'], | ||
}, | ||
}; | ||
|
||
// export function setup() { | ||
// const res = http.post(`${baseUrl}/users/login`, { | ||
// username: '[email protected]', | ||
// password: '123', | ||
// }); | ||
// const [, token] = res.body.token | ||
|
||
// return { token }; | ||
// } | ||
|
||
export default function (data) { | ||
const res = http.get(baseUrl); | ||
|
||
check(res, { | ||
'status is OK': res => res.status === 200, | ||
}); | ||
group('Authorization', () => { | ||
requestLogin(baseUrl); | ||
// requestRegister(baseUrl); | ||
requestFind(baseUrl); | ||
}); | ||
sleep(1); | ||
} |