-
Notifications
You must be signed in to change notification settings - Fork 0
Performance testing - Initial Examples #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
010a77a
Add initial examples
bubafinder 97c26dd
Fix image path
bubafinder 31c0f93
Update title
bubafinder 77acac0
Update antipattern 1 description
bubafinder c682003
Add antipattern examples and update references in documentation
bubafinder 5a316cc
Update page name
bubafinder 6149c54
Add Glossary
bubafinder 6a481cb
Add Load testing types to the Glossary
bubafinder be2e193
Merge branch 'master' into examples/performance-testing
bubafinder File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or 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,124 @@ | ||
| # Performance Testing - Antipattern Examples | ||
|
|
||
| ## Antipattern 1: Ignoring Think Time | ||
|
|
||
| Excluding think time between user actions can result in unrealistic performance | ||
| metrics for certain types of tests, such as average, stress, and soak tests. | ||
| However, think time is less critical for tests like breakpoint and spike tests, | ||
| as other parameters can control these scenarios effectively. Incorporating | ||
| think time is crucial when simulating real user behavior based on user | ||
| scenarios. In the provided example, user actions are executed without any delay, | ||
| which does not accurately reflect real-world conditions for this type of test. | ||
|
|
||
| ```javascript | ||
| export default function () { | ||
| http.get('http://example.com/api/resource1'); | ||
| http.get('http://example.com/api/resource2'); | ||
| http.get('http://example.com/api/resource3'); | ||
| } | ||
| ``` | ||
|
|
||
| ### Solution | ||
|
|
||
| Introduce think time between user actions to simulate real user behavior. This example adds a random delay between 1 to 5 seconds between each request. The bigger the range, the more realistic the simulation. | ||
|
|
||
| ```javascript | ||
| import { randomIntBetween } from 'https://jslib.k6.io/k6-utils/1.4.0/index.js'; | ||
| import { sleep } from 'k6'; | ||
|
|
||
| export default function () { | ||
| http.get('http://example.com/api/resource1'); | ||
| sleep(randomIntBetween(1, 5)); | ||
| http.get('http://example.com/api/resource2'); | ||
| sleep(randomIntBetween(1, 5)); | ||
| http.get('http://example.com/api/resource3'); | ||
| } | ||
| ``` | ||
|
|
||
| ## Antipattern 2: Lack of Data Variation | ||
|
|
||
| Using static, hardcoded data for requests can cause caching mechanisms to produce artificially high performance metrics. In this example, the same username is used for every request, which may not represent real-world scenarios. | ||
|
|
||
| ```javascript | ||
| export default function () { | ||
| const payload = JSON.stringify({ | ||
| username: 'username', // Static username used in every request | ||
| password: 'password', | ||
| }); | ||
|
|
||
| http.post('http://example.com/api/login', payload); | ||
| } | ||
| ``` | ||
|
|
||
| ### Solution | ||
|
|
||
| Use dynamic data or randomization to simulate different user scenarios. This example generates a random username for each request. | ||
|
|
||
| ```javascript | ||
| import exec from 'k6/execution'; | ||
|
|
||
| export default function () { | ||
| const payload = JSON.stringify({ | ||
| username: `username${exec.vu.idInTest}`, // Unique identifier for each virtual user, we will use it to be sure every username is unique | ||
| password: 'password', | ||
| }); | ||
|
|
||
| http.post('http://example.com/api/login', payload); | ||
| } | ||
| ``` | ||
|
|
||
| ## Antipattern 3: Not Scaling Virtual Users | ||
|
|
||
| Running performance tests with unrealistic numbers of virtual users or ramping up too quickly can lead to inaccurate results. In this example, the test starts with 1000 VUs immediately. | ||
|
|
||
| ```javascript | ||
| export const options = { | ||
| vus: 1000, | ||
| duration: '1m', | ||
| }; | ||
|
|
||
| export default function () { | ||
| http.get('http://example.com/api/resource'); | ||
| } | ||
| ``` | ||
|
|
||
| ### Solution | ||
|
|
||
| Executors control how k6 schedules VUs and iterations. The executor that you choose depends on the goals of your test and the type of traffic you want to model. For example, the `ramping-vus` executor gradually increases the number of VUs over a specified duration, allowing for more realistic load testing for specific test types. | ||
|
|
||
| ```javascript | ||
| export const options = { | ||
| discardResponseBodies: true, | ||
| scenarios: { | ||
| contacts: { | ||
| executor: 'ramping-vus', | ||
| startVUs: 0, | ||
| stages: [ | ||
| { duration: '20s', target: 10 }, | ||
| { duration: '10s', target: 0 }, | ||
| ], | ||
| gracefulRampDown: '0s', | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| export default function () { | ||
| http.get('http://example.com/api/resource'); | ||
| // Injecting sleep | ||
| // Sleep time is 500ms. Total iteration time is sleep + time to finish request. | ||
| sleep(0.5); | ||
| } | ||
| ``` | ||
|
|
||
| Based upon our test scenario inputs and results: | ||
|
|
||
| - The configuration defines 2 stages for a total test duration of 30 seconds; | ||
| - Stage 1 ramps up VUs linearly from the startVUs of 0 to the target of 10 over a 20 second duration; | ||
| - From the 10 VUs at the end of stage 1, stage 2 then ramps down VUs linearly to the target of 0 over a 10 second duration; | ||
| - Each iteration of the default function is expected to be roughly 515ms, or ~2/s; | ||
| - As the number of VUs changes, the iteration rate directly correlates; each addition of a VU increases the rate by about 2 iters/s, whereas each subtraction of a VU reduces by about 2 iters/s; | ||
| - The example performed ~300 iterations over the course of the test. | ||
|
|
||
| #### Chart representation of the test execution | ||
|
|
||
|  | ||
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I would like to see this in the glossary. Just a few words on what each type means.