-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: create load test example (#243)
- Loading branch information
1 parent
75b318e
commit 8d127d7
Showing
8 changed files
with
1,514 additions
and
700 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 |
---|---|---|
|
@@ -50,5 +50,6 @@ const config = { | |
}, | ||
], | ||
}, | ||
ignorePatterns: ['webpack.config.js'], | ||
} | ||
module.exports = config |
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 |
---|---|---|
|
@@ -13,7 +13,7 @@ | |
/out/ | ||
|
||
# production | ||
/build | ||
**/build | ||
|
||
# misc | ||
.DS_Store | ||
|
Large diffs are not rendered by default.
Oops, something went wrong.
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,25 @@ | ||
# Load Tests | ||
|
||
## Prerequisites | ||
|
||
- [`k6`](https://k6.io/) | ||
|
||
## Setting up the Environment | ||
|
||
1. Build the load test files. | ||
|
||
``` | ||
npm run test:load:build | ||
``` | ||
|
||
1. Log in to the target environment with a test account and save the session cookie value. | ||
|
||
## Runing the Load Tests | ||
|
||
### Create Post Load Test | ||
|
||
``` | ||
k6 run tests/load/build/create-post.test.js \ | ||
-e SESSION_COOKIE={{ session cookie }} \ | ||
-e BASE_URL={{ scheme }}://{{ hostname }}:{{ port }} | ||
``` |
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,56 @@ | ||
import { check } from 'k6' | ||
import { createClient, createOptions } from 'k6-trpc' | ||
import { type Options } from 'k6/options' | ||
import superjson from 'superjson' | ||
|
||
import { type AppRouter } from '~/server/modules/_app' | ||
|
||
const PARAMETERS = { | ||
SESSION_COOKIE: __ENV.SESSION_COOKIE, | ||
BASE_URL: __ENV.BASE_URL ?? 'http://localhost:3000', | ||
} as const | ||
|
||
const apiOptions = { | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
} as const | ||
|
||
const client = createClient<AppRouter>( | ||
`${PARAMETERS.BASE_URL}/api/trpc/`, | ||
superjson | ||
) | ||
|
||
export const options: Options = { | ||
vus: 100, | ||
duration: '60s', | ||
} as const | ||
|
||
export const setup = () => { | ||
if (!PARAMETERS.SESSION_COOKIE) { | ||
throw new Error('No session cookie provided.') | ||
} | ||
|
||
// For some reason, k6 will capitalize the first letter of object keys | ||
return { session: PARAMETERS.SESSION_COOKIE } | ||
} | ||
|
||
export default function test({ session }: ReturnType<typeof setup>) { | ||
const response = client.post.add.mutate( | ||
{ | ||
content: `Hello World! | ||
This is a new post!`, | ||
contentHtml: | ||
'{"type":"doc","content":[{"type":"paragraph","content":[{"type":"text","marks":[{"type":"bold"}],"text":"Hello World!"}]},{"type":"paragraph"},{"type":"paragraph","content":[{"type":"text","text":"This is a new post!"}]}]}', | ||
}, | ||
createOptions({ | ||
...apiOptions, | ||
cookies: { 'auth.session-token': session }, | ||
}) | ||
) | ||
|
||
check(response, { | ||
'Create post mutation is successful': () => response.status < 300, | ||
}) | ||
} |
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,43 @@ | ||
const path = require('path') | ||
const { CleanWebpackPlugin } = require('clean-webpack-plugin') | ||
const GlobEntries = require('webpack-glob-entries') | ||
|
||
module.exports = { | ||
mode: 'production', | ||
entry: GlobEntries(path.join(__dirname, '*.test.ts')), | ||
output: { | ||
path: path.join(__dirname, 'build'), | ||
libraryTarget: 'commonjs', | ||
filename: '[name].js', | ||
}, | ||
resolve: { | ||
extensions: ['.ts', '.js'], | ||
}, | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.ts$/, | ||
use: { | ||
loader: 'babel-loader', | ||
options: { | ||
presets: ['@babel/preset-env', '@babel/preset-typescript'], | ||
plugins: [ | ||
'@babel/plugin-transform-class-properties', | ||
'@babel/plugin-transform-object-rest-spread', | ||
], | ||
}, | ||
}, | ||
exclude: /node_modules/, | ||
}, | ||
], | ||
}, | ||
externals: /^(k6|https?\:\/\/)(\/.*)?(?!-trpc)/, | ||
stats: { | ||
colors: true, | ||
}, | ||
plugins: [new CleanWebpackPlugin()], | ||
optimization: { | ||
// Don't minimize, as it's not used in the browser | ||
minimize: false, | ||
}, | ||
} |
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
8d127d7
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
starter-kit – ./
starter-kit-ogp-tooling.vercel.app
ogp-starter-kit.vercel.app
starter-kit-git-main-ogp-tooling.vercel.app