diff --git a/.prettierrc b/.prettierrc index 3ea7979..519a853 100644 --- a/.prettierrc +++ b/.prettierrc @@ -2,7 +2,7 @@ "printWidth": 80, "tabWidth": 1, "useTabs": true, - "semi": true, + "semi": false, "singleQuote": true, "trailingComma": "none", "bracketSpacing": true, diff --git a/src/controller/my.crocodiles.ts b/src/controller/my.crocodiles.ts index 637b2a3..75a5004 100644 --- a/src/controller/my.crocodiles.ts +++ b/src/controller/my.crocodiles.ts @@ -1,41 +1,41 @@ -import * as request from '../utils/request'; -import { K6Interceptor } from '../utils/request'; -import { sleep } from 'k6'; -import { Crocodile } from '../types/crocodile.type'; -import fake from 'faker'; +import * as request from '../utils/request' +import { K6Interceptor } from '../utils/request' +import { sleep } from 'k6' +import { Crocodile } from '../types/crocodile.type' +import fake from 'faker' -const CROCODILES_ENDPOINT = 'my/crocodiles/'; +const CROCODILES_ENDPOINT = 'my/crocodiles/' export function createCrocodiles( token: string, crocodile: Crocodile, interceptor?: K6Interceptor ): Crocodile { - const headers = request.headers(token); + const headers = request.headers(token) const response = request.post( CROCODILES_ENDPOINT, crocodile, headers, interceptor - ); + ) - sleep(0.3); - return response; + sleep(0.3) + return response } export function getCrocodiles( token: string, interceptor?: K6Interceptor ): Crocodile[] { - const headers = request.headers(token); + const headers = request.headers(token) const response = request.get( CROCODILES_ENDPOINT, headers, interceptor - ); + ) - sleep(0.3); - return response; + sleep(0.3) + return response } export function prepareCrocodiles(token: string, count: number = 100): void { @@ -44,10 +44,10 @@ export function prepareCrocodiles(token: string, count: number = 100): void { name: fake.name.firstName(), sex: 'M', date_of_birth: '2020-10-15' - }; + } - const headers = request.headers(token); - request.post(CROCODILES_ENDPOINT, crocodile, headers); - sleep(0.3); + const headers = request.headers(token) + request.post(CROCODILES_ENDPOINT, crocodile, headers) + sleep(0.3) } } diff --git a/src/controller/user.auth.ts b/src/controller/user.auth.ts index 61be307..b15b64c 100644 --- a/src/controller/user.auth.ts +++ b/src/controller/user.auth.ts @@ -1,30 +1,30 @@ -import * as request from '../utils/request'; -import { K6Interceptor } from '../utils/request'; -import { sleep } from 'k6'; -import { AuthRequest, AuthResponse } from '../types/user.type'; +import * as request from '../utils/request' +import { K6Interceptor } from '../utils/request' +import { sleep } from 'k6' +import { AuthRequest, AuthResponse } from '../types/user.type' -const AUTH_ENDPOINT = 'auth/token/login/'; +const AUTH_ENDPOINT = 'auth/token/login/' export function login( authUser: AuthRequest, interceptor?: K6Interceptor ): void { - const headers = request.headers(); - request.post(AUTH_ENDPOINT, authUser, headers, interceptor); - sleep(0.3); + const headers = request.headers() + request.post(AUTH_ENDPOINT, authUser, headers, interceptor) + sleep(0.3) } export function getAuth({ username, password }: { - username: string; - password: string; + username: string + password: string }): string { - const headers = request.headers(); + const headers = request.headers() return request.post( AUTH_ENDPOINT, { username, password }, headers - ).access; + ).access } diff --git a/src/controller/user.register.ts b/src/controller/user.register.ts index 0ae2dc1..72f50ab 100644 --- a/src/controller/user.register.ts +++ b/src/controller/user.register.ts @@ -1,12 +1,12 @@ -import { sleep } from 'k6'; -import * as request from '../utils/request'; -import { K6Interceptor } from '../utils/request'; -import { User } from '../types/user.type'; +import { sleep } from 'k6' +import * as request from '../utils/request' +import { K6Interceptor } from '../utils/request' +import { User } from '../types/user.type' -const REGISTER_ENDPOINT = 'user/register/'; +const REGISTER_ENDPOINT = 'user/register/' export function register(user: User, interceptor?: K6Interceptor): void { - const headers = request.headers(); - request.post(REGISTER_ENDPOINT, user, headers, interceptor); - sleep(0.3); + const headers = request.headers() + request.post(REGISTER_ENDPOINT, user, headers, interceptor) + sleep(0.3) } diff --git a/src/test/crocodiles.create.ts b/src/test/crocodiles.create.ts index 00d7f6a..1fbe60e 100644 --- a/src/test/crocodiles.create.ts +++ b/src/test/crocodiles.create.ts @@ -1,11 +1,11 @@ -import * as auth from '../controller/user.auth'; -import * as user from '../controller/user.register'; -import { Options } from 'k6/options'; -import * as fake from 'faker'; -import * as rate from '../utils/metrics'; -import { createCrocodiles } from '../controller/my.crocodiles'; -import { Crocodile } from '../types/crocodile.type'; -import { User } from '../types/user.type'; +import * as auth from '../controller/user.auth' +import * as user from '../controller/user.register' +import { Options } from 'k6/options' +import * as fake from 'faker' +import * as rate from '../utils/metrics' +import { createCrocodiles } from '../controller/my.crocodiles' +import { Crocodile } from '../types/crocodile.type' +import { User } from '../types/user.type' export const options: Partial = { vus: 30, @@ -14,7 +14,7 @@ export const options: Partial = { p80_within_1sec: ['rate >= 0.8'], errors_rate: ['rate <= 0.1'] } -}; +} export function setup(): string { const userPayload: User = { @@ -23,10 +23,10 @@ export function setup(): string { last_name: fake.name.lastName(), email: fake.internet.email(), password: fake.internet.password() - }; + } - user.register(userPayload); - return auth.getAuth(userPayload); + user.register(userPayload) + return auth.getAuth(userPayload) } export default function (_token: string): void { @@ -34,10 +34,10 @@ export default function (_token: string): void { name: fake.name.firstName(), sex: 'M', date_of_birth: '2020-10-15' - }; + } createCrocodiles(_token, crocodile, response => { - rate.p80_within_1sec(response); - rate.errors(response, 201); - }); + rate.p80_within_1sec(response) + rate.errors(response, 201) + }) } diff --git a/src/test/crocodiles.search.ts b/src/test/crocodiles.search.ts index af61e19..b642114 100644 --- a/src/test/crocodiles.search.ts +++ b/src/test/crocodiles.search.ts @@ -1,10 +1,10 @@ -import { Options } from 'k6/options'; -import { User } from '../types/user.type'; -import * as fake from 'faker'; -import * as user from '../controller/user.register'; -import * as auth from '../controller/user.auth'; -import { getCrocodiles, prepareCrocodiles } from '../controller/my.crocodiles'; -import * as rate from '../utils/metrics'; +import { Options } from 'k6/options' +import { User } from '../types/user.type' +import * as fake from 'faker' +import * as user from '../controller/user.register' +import * as auth from '../controller/user.auth' +import { getCrocodiles, prepareCrocodiles } from '../controller/my.crocodiles' +import * as rate from '../utils/metrics' export const options: Partial = { scenarios: { @@ -41,7 +41,7 @@ export const options: Partial = { ] }, discardResponseBodies: false -}; +} export function setup(): string { const userPayload: User = { @@ -50,17 +50,17 @@ export function setup(): string { last_name: fake.name.lastName(), email: fake.internet.email(), password: fake.internet.password() - }; + } - user.register(userPayload); - const token = auth.getAuth(userPayload); - prepareCrocodiles(token, 50); - return token; + user.register(userPayload) + const token = auth.getAuth(userPayload) + prepareCrocodiles(token, 50) + return token } export function searchCrocodiles(_token: string): void { getCrocodiles(_token, response => { - rate.p80_within_1sec(response); - rate.errors(response, 200); - }); + rate.p80_within_1sec(response) + rate.errors(response, 200) + }) } diff --git a/src/types/crocodile.type.ts b/src/types/crocodile.type.ts index 6ac1235..1f1bc9f 100644 --- a/src/types/crocodile.type.ts +++ b/src/types/crocodile.type.ts @@ -1,7 +1,7 @@ export interface Crocodile { - id?: number; - name: string; - sex: string; - date_of_birth: string; - age?: number; + id?: number + name: string + sex: string + date_of_birth: string + age?: number } diff --git a/src/types/user.type.ts b/src/types/user.type.ts index 46b3891..7455c91 100644 --- a/src/types/user.type.ts +++ b/src/types/user.type.ts @@ -1,17 +1,17 @@ export interface User { - username: string; - first_name: string; - last_name: string; - email: string; - password: string; + username: string + first_name: string + last_name: string + email: string + password: string } export interface AuthRequest { - username: string; - password: string; + username: string + password: string } export interface AuthResponse { - refresh: string; - access: string; + refresh: string + access: string } diff --git a/src/utils/metrics.ts b/src/utils/metrics.ts index 49c6cfb..9a04036 100644 --- a/src/utils/metrics.ts +++ b/src/utils/metrics.ts @@ -1,13 +1,13 @@ -import { Rate } from 'k6/metrics'; -import { RefinedResponse } from 'k6/http'; +import { Rate } from 'k6/metrics' +import { RefinedResponse } from 'k6/http' -const P80 = new Rate('p80_within_1sec'); -const ERRORS_RATE = new Rate('errors_rate'); +const P80 = new Rate('p80_within_1sec') +const ERRORS_RATE = new Rate('errors_rate') export function p80_within_1sec(response: RefinedResponse): void { - P80.add(response.timings.duration < 1000); + P80.add(response.timings.duration < 1000) } export function errors(response: RefinedResponse, status: number): void { - ERRORS_RATE.add(response.status !== status); + ERRORS_RATE.add(response.status !== status) } diff --git a/src/utils/report.js b/src/utils/report.js index 92341f5..b372b1e 100644 --- a/src/utils/report.js +++ b/src/utils/report.js @@ -1,17 +1,17 @@ -const reporter = require('k6-html-reporter'); -const fs = require('fs'); +const reporter = require('k6-html-reporter') +const fs = require('fs') -const REPORT_FOLDER = './dist/reports'; -const SOURCE_FOLDER = './dist/source'; +const REPORT_FOLDER = './dist/reports' +const SOURCE_FOLDER = './dist/source' fs.readdirSync(SOURCE_FOLDER).forEach(file => { - const name = file.replace('.json', ''); + const name = file.replace('.json', '') const options = { jsonFile: `${SOURCE_FOLDER}/${file}`, output: REPORT_FOLDER - }; + } - reporter.generateSummaryReport(options); + reporter.generateSummaryReport(options) - fs.renameSync(`${REPORT_FOLDER}/report.html`, `${REPORT_FOLDER}/${name}.html`); -}); + fs.renameSync(`${REPORT_FOLDER}/report.html`, `${REPORT_FOLDER}/${name}.html`) +}) diff --git a/src/utils/request.ts b/src/utils/request.ts index f10d81c..7bbb52e 100644 --- a/src/utils/request.ts +++ b/src/utils/request.ts @@ -1,8 +1,8 @@ -import http, { RefinedResponse } from 'k6/http'; +import http, { RefinedResponse } from 'k6/http' -const BASE_URL = 'https://test-api.k6.io'; +const BASE_URL = 'https://test-api.k6.io' -export type K6Interceptor = (res: RefinedResponse<'text'>) => void; +export type K6Interceptor = (res: RefinedResponse<'text'>) => void export function headers(authToken?: string): any { return { @@ -10,7 +10,7 @@ export function headers(authToken?: string): any { 'Content-Type': 'application/json', ...(authToken ? { Authorization: `Bearer ${authToken}` } : {}) } - }; + } } export function post( @@ -19,14 +19,14 @@ export function post( headers: any, interceptor?: K6Interceptor ): T { - const url = `${BASE_URL}/${_url}`; + const url = `${BASE_URL}/${_url}` const response: RefinedResponse<'text'> = http.post( url, JSON.stringify(body), headers - ); + ) - return responseAs(response, url, interceptor); + return responseAs(response, url, interceptor) } export function get( @@ -34,10 +34,10 @@ export function get( headers: any, interceptor?: K6Interceptor ): T { - const url = `${BASE_URL}/${_url}`; - const response: RefinedResponse<'text'> = http.get(url, headers); + const url = `${BASE_URL}/${_url}` + const response: RefinedResponse<'text'> = http.get(url, headers) - return responseAs(response, url, interceptor); + return responseAs(response, url, interceptor) } export function responseAs( @@ -45,15 +45,15 @@ export function responseAs( url: string, interceptor?: K6Interceptor ): T { - if (res.error) console.log(`UNKNOWN ERROR ON REQUEST : ${res.error}`); + if (res.error) console.log(`UNKNOWN ERROR ON REQUEST : ${res.error}`) if (res.error_code) { // @ts-ignore - const statusText = res.status_text; - console.log(`REQUEST : ${url}\nFAILED: ${statusText}: ${res.body}`); + const statusText = res.status_text + console.log(`REQUEST : ${url}\nFAILED: ${statusText}: ${res.body}`) } - if (interceptor) interceptor(res); + if (interceptor) interceptor(res) - return JSON.parse(res.body); + return JSON.parse(res.body) }