Skip to content

Commit 421a2ac

Browse files
chore: update script example from hybrid performance guide (#1653)
* chore: update script example from hybrid performance guide * Update docs/sources/next/using-k6-browser/recommended-practices/hybrid-approach-to-performance.md * Update hybrid-approach-to-performance.md * Update hybrid-approach-to-performance.md * Update hybrid-approach-to-performance.md
1 parent d23a440 commit 421a2ac

File tree

1 file changed

+24
-15
lines changed

1 file changed

+24
-15
lines changed

docs/sources/next/using-k6-browser/recommended-practices/hybrid-approach-to-performance.md

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,13 @@ The code below shows an example of combining a browser and HTTP test in a single
1818

1919
{{< code >}}
2020

21-
<!-- eslint-disable no-undef -->
22-
2321
```javascript
2422
import http from 'k6/http';
2523
import { check } from 'k6';
2624
import { browser } from 'k6/browser';
25+
import { randomIntBetween } from 'https://jslib.k6.io/k6-utils/1.2.0/index.js';
2726

28-
const BASE_URL = __ENV.BASE_URL;
27+
const BASE_URL = __ENV.BASE_URL || 'https://quickpizza.grafana.com';
2928

3029
export const options = {
3130
scenarios: {
@@ -72,7 +71,7 @@ export function getPizza() {
7271
const res = http.post(`${BASE_URL}/api/pizza`, JSON.stringify(restrictions), {
7372
headers: {
7473
'Content-Type': 'application/json',
75-
'X-User-ID': customers[Math.floor(Math.random() * customers.length)],
74+
'X-User-ID': randomIntBetween(1, 30000),
7675
},
7776
});
7877

@@ -87,9 +86,9 @@ export async function checkFrontend() {
8786
try {
8887
await page.goto(BASE_URL);
8988

90-
const header = await page.locator("h1").textContent();
89+
const header = await page.locator('h1').textContent();
9190
check(header, {
92-
header: h => h == "Looking to break out of your pizza routine?",
91+
header: (h) => h == 'Looking to break out of your pizza routine?',
9392
});
9493

9594
await Promise.all([
@@ -98,27 +97,37 @@ export async function checkFrontend() {
9897
]);
9998
await page.screenshot({ path: `screenshots/${__ITER}.png` });
10099

101-
const recommendation = await page.locator("div#recommendations").textContent();
100+
const recommendation = await page.locator('div#recommendations').textContent();
102101
check(recommendation, {
103-
recommendation: (r) => r != "",
102+
recommendation: (r) => r != '',
104103
});
105104
} finally {
106105
await page.close();
107106
}
108107
}
109108
```
110109

111-
<!-- eslint-enable no-undef -->
112-
113110
{{< /code >}}
114111

112+
If you save that script to a local file named `test.js`, you can run it with:
113+
114+
```bash
115+
k6 run test.js
116+
```
117+
118+
The script also includes a common best practice by defining a `BASE_URL` variable, and using the [environment variable](https://grafana.com/docs/k6/<K6_VERSION>/using-k6/environment-variables/) value `__ENV.BASE_URL` if it exists. That's useful if you want to use the same script for multiple environments, such as staging and production, and you could pass that value to your script with the command:
119+
120+
```bash
121+
k6 run -e BASE_URL=https://quickpizza.grafana.com test.js
122+
```
123+
115124
## Browser and failure injection test
116125

117-
You can also run a browser test together with a failure injection test by using the [xk6-disruptor](https://github.com/grafana/xk6-disruptor) extension. This approach lets you find issues in your front end if any services it depends on are suddenly injected with failures, such as delays or server errors.
126+
You can also run a browser test together with a failure injection test by using the [xk6-disruptor](https://github.com/grafana/xk6-disruptor) extension. This approach lets you find issues in your frontend if any services it depends on are suddenly injected with failures, such as delays or server errors.
118127

119-
The following code shows an example of how to introduce faults to a Kubernetes service. At the same time, the `browser` scenario runs to ensure the frontend application is free of any unexpected errors that may not have been handled properly.
128+
The following code shows an example of how you could use the xk6-disruptor extension to introduce faults to a Kubernetes service. At the same time, the `browser` scenario runs to ensure the frontend application is free of any unexpected errors that may not have been handled properly.
120129

121-
To find out more information about injecting faults to your service, check out the [Get started with xk6-disruptor guide](https://grafana.com/docs/k6/<K6_VERSION>/testing-guides/injecting-faults-with-xk6-disruptor/xk6-disruptor/).
130+
To find out more information about injecting faults to your service, check out the [Get started with xk6-disruptor guide](https://grafana.com/docs/k6/<K6_VERSION>/testing-guides/injecting-faults-with-xk6-disruptor/first-steps/).
122131

123132
{{< code >}}
124133

@@ -181,7 +190,7 @@ export async function checkFrontend() {
181190
await page.goto(BASE_URL);
182191
const header = await page.locator('h1').textContent();
183192
check(header, {
184-
header: h => h == 'Looking to break out of your pizza routine?',
193+
header: (h) => h == 'Looking to break out of your pizza routine?',
185194
});
186195

187196
await Promise.all([
@@ -192,7 +201,7 @@ export async function checkFrontend() {
192201

193202
const recommendation = await page.locator('div#recommendations').textContent();
194203
check(recommendation, {
195-
recommendation: r => r != '',
204+
recommendation: (r) => r != '',
196205
});
197206
} finally {
198207
await page.close();

0 commit comments

Comments
 (0)