-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexample.js
106 lines (92 loc) · 3.13 KB
/
example.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { randomSeed } from 'k6';
import http from 'k6/http';
import runWorkload from './shared/runworkload.js';
import think from './shared/think.js';
import getWithResources from './httpext/getwithresources.js';
import { group, checkAll } from './shared/k6-ext.js';
export let options = {
vus: 1,
iterations: 8
};
const hostName = 'http://demo.mercury-ecommerce.com/';
export default function () {
// Seed so that multiple runs use the same probability (and thus path)
randomSeed(__VU * 1000 + __ITER); // Unique seed per virtual user and iteration
// Create a workload that can be executed by the Pensum runner
runWorkload({
initial: 'home',
abandon: 'abandon',
states: [
// Home
{
name: 'home',
targets: [{
target: 'lister',
probability: 75
},
{
target: 'abandon',
probability: 25
}],
action: (thinkTime) => visitHomePage(thinkTime)
},
// Lister
{
name: 'lister',
targets: [{
target: 'home',
probability: 15
},
{
target: 'abandon',
probability: 85
}],
// For example only: always visit the prepare/bar product lister page.
// In a real setup the lister pages should be dynamically selected, e.g. based on the sitemap and a random distribution
action: (thinkTime) => visitListerPage(thinkTime, 'prepare/bar')
},
// Abandon, ie. leave website
{
name: 'abandon',
targets: [],
action: () => {}
}]
});
}
function visitHomePage (thinkTime) {
group('Home', function () {
// Load the webpage including all resources defined in <script> and <link> HTML tags
getWithResources(hostName, hostName);
// Additionally perform important async JS calls, e.g. get cart
let req = [{
'method': 'get',
'url': `${hostName}/mercury/checkout/cart`
}];
const res = http.batch(req);
checkAll(res, {
'is status 200': (x) => x.status === 200
});
});
// Simulate user think time, ie. wait some time before continuing
think(thinkTime.avg, thinkTime.std, thinkTime.min, thinkTime.max);
}
function visitListerPage (thinkTime, categoryUrl) {
group('Lister', function () {
getWithResources(hostName, `${hostName}/${categoryUrl}`);
// Additionally perform important async JS calls, e.g. get cart & product comparison
let req = [{
'method': 'get',
'url': `${hostName}/mercury/checkout/cart`
},
{
'method': 'get',
'url': `${hostName}/mercury/productcomparison`
}];
const res = http.batch(req);
checkAll(res, {
'is status 200': (x) => x.status === 200
});
});
// Simulate user think time, ie. wait some time before continuing
think(thinkTime.avg, thinkTime.std, thinkTime.min, thinkTime.max);
}