|
1 | | -import http from 'k6/http' |
| 1 | +import http from "k6/http"; |
2 | 2 |
|
3 | | -const directTrafficRate = 0.1 |
4 | | -const customEventRate = 0.3 |
5 | | -const errorRate = 0.0 |
6 | | -const bounceRate = 0.5 |
7 | | -const exitRate = 0.1 |
8 | | -const maxEventsPerSession = 30 |
| 3 | +const directTrafficRate = 0.1; |
| 4 | +const customEventRate = 0.3; |
| 5 | +const errorRate = 0.0; |
| 6 | +const bounceRate = 0.5; |
| 7 | +const exitRate = 0.1; |
| 8 | +const maxEventsPerSession = 30; |
9 | 9 |
|
10 | 10 | export const options = { |
11 | 11 | thresholds: { |
12 | 12 | // Thresholds so tags appear in CLI report. |
13 | | - 'http_reqs{event_type:pageview}': ['count >= 0'], |
14 | | - 'http_reqs{event_type:custom}': ['count >= 0'] |
| 13 | + "http_reqs{event_type:pageview}": ["count >= 0"], |
| 14 | + "http_reqs{event_type:custom}": ["count >= 0"], |
15 | 15 | }, |
16 | 16 | discardResponseBodies: true, |
17 | 17 | scenarios: { |
18 | 18 | sharedIterationsPageViewEvents: { |
19 | | - executor: 'shared-iterations', |
| 19 | + executor: "shared-iterations", |
20 | 20 | vus: 4096, |
21 | | - iterations: 2 ** 17 |
22 | | - } |
23 | | - } |
24 | | -} |
| 21 | + iterations: 2 ** 17, |
| 22 | + }, |
| 23 | + }, |
| 24 | +}; |
25 | 25 |
|
26 | 26 | const origins = [ |
27 | | - 'mywebsite.localhost', |
28 | | - 'foo.mywebsite.localhost' |
29 | | -] |
| 27 | + "mywebsite.localhost", |
| 28 | + "foo.mywebsite.localhost", |
| 29 | +]; |
30 | 30 |
|
31 | 31 | export default function () { |
32 | 32 | const origin = [ |
33 | | - randomItem(['http', 'https']), |
34 | | - '://', |
35 | | - randomItem(origins) |
36 | | - ].join('') |
| 33 | + randomItem(["http", "https"]), |
| 34 | + "://", |
| 35 | + randomItem(origins), |
| 36 | + ].join(""); |
37 | 37 |
|
38 | | - const ipAddr = randomIP() |
| 38 | + const ipAddr = randomIP(); |
39 | 39 |
|
40 | | - const visitorState = { origin, ipAddr } |
| 40 | + const visitorState = { origin, ipAddr }; |
41 | 41 |
|
42 | 42 | // Entry pageview. |
43 | | - const response = pageView(visitorState) |
| 43 | + const response = pageView(visitorState); |
44 | 44 | if (response.status !== 200) { |
45 | | - console.error('entry pageview', response.status_text, response.error) |
46 | | - return |
| 45 | + console.error("entry pageview", response.status_text, response.error); |
| 46 | + return; |
47 | 47 | } |
48 | 48 |
|
49 | 49 | // Custom events. |
50 | 50 | while (Math.random() < customEventRate) { |
51 | | - const response = customEvent(visitorState) |
| 51 | + const response = customEvent(visitorState); |
52 | 52 | if (response.status !== 200) { |
53 | | - console.error('custom event', response.status_text, response.error) |
54 | | - return |
| 53 | + console.error("custom event", response.status_text, response.error); |
| 54 | + return; |
55 | 55 | } |
56 | 56 | } |
57 | 57 |
|
58 | 58 | // Bounce. |
59 | 59 | if (Math.random() < bounceRate) { |
60 | | - return |
| 60 | + return; |
61 | 61 | } |
62 | 62 |
|
63 | | - let events = 0 |
| 63 | + let events = 0; |
64 | 64 | while (events < maxEventsPerSession) { |
65 | 65 | // Pageview. |
66 | | - const response = pageView(visitorState) |
| 66 | + const response = pageView(visitorState); |
67 | 67 | if (response.status !== 200) { |
68 | | - console.error('pageview', response.status_text, response.error) |
69 | | - return |
| 68 | + console.error("pageview", response.status_text, response.error); |
| 69 | + return; |
70 | 70 | } |
71 | | - events++ |
| 71 | + events++; |
72 | 72 |
|
73 | 73 | // Custom events. |
74 | 74 | while (Math.random() < customEventRate) { |
75 | | - const response = customEvent(visitorState) |
| 75 | + const response = customEvent(visitorState); |
76 | 76 | if (response.status !== 200) { |
77 | | - console.error('custom event', response.status_text, response.error) |
78 | | - return |
| 77 | + console.error("custom event", response.status_text, response.error); |
| 78 | + return; |
79 | 79 | } |
80 | | - events++ |
| 80 | + events++; |
81 | 81 | } |
82 | 82 |
|
83 | 83 | // Exit rate. |
84 | 84 | if (Math.random() < exitRate) { |
85 | | - return |
| 85 | + return; |
86 | 86 | } |
87 | 87 | } |
88 | 88 | } |
89 | 89 |
|
90 | | -function pageView (visitorState) { |
91 | | - const { origin, ipAddr, referrer } = visitorState |
| 90 | +function pageView(visitorState) { |
| 91 | + const { origin, ipAddr, referrer } = visitorState; |
92 | 92 | const url = [ |
93 | 93 | origin, |
94 | | - randomItem(['', 'foo', 'bar', 'qux', 'foo']) |
95 | | - ].join('/') |
| 94 | + randomItem(["", "foo", "bar", "qux", "foo"]), |
| 95 | + ].join("/"); |
96 | 96 |
|
97 | 97 | const headers = { |
98 | 98 | Origin: origin, |
99 | | - 'X-Prisme-Referrer': url, |
100 | | - 'X-Prisme-Document-Referrer': referrer ?? origin, |
101 | | - 'X-Forwarded-For': ipAddr |
102 | | - } |
| 99 | + "X-Prisme-Referrer": url, |
| 100 | + "X-Prisme-Document-Referrer": referrer ?? origin, |
| 101 | + "X-Forwarded-For": ipAddr, |
| 102 | + }; |
103 | 103 |
|
104 | 104 | if (!referrer) { |
105 | 105 | if (Math.random() < directTrafficRate) { |
106 | | - delete headers['X-Prisme-Document-Referrer'] |
| 106 | + delete headers["X-Prisme-Document-Referrer"]; |
107 | 107 | } else { |
108 | | - headers['X-Prisme-Document-Referrer'] = randomItem([ |
109 | | - 'https://google.com', |
110 | | - 'https://duckduckgo.com', |
111 | | - 'https://qwant.com', |
112 | | - 'https://github.com' |
113 | | - ]) |
| 108 | + headers["X-Prisme-Document-Referrer"] = randomItem([ |
| 109 | + "https://google.com", |
| 110 | + "https://duckduckgo.com", |
| 111 | + "https://qwant.com", |
| 112 | + "https://github.com", |
| 113 | + ]); |
114 | 114 | } |
115 | 115 | } |
116 | 116 |
|
117 | 117 | if (Math.random() < errorRate) { |
118 | 118 | // Invalid origin. |
119 | | - headers.Origin = 'an invalid origin' |
| 119 | + headers.Origin = "an invalid origin"; |
120 | 120 | } |
121 | 121 |
|
122 | 122 | const response = http.post( |
123 | | - 'http://prisme.localhost/api/v1/events/pageviews', |
| 123 | + "http://prisme.localhost/api/v1/events/pageviews", |
124 | 124 | null, |
125 | | - { headers, tags: { event_type: 'pageview' } } |
126 | | - ) |
| 125 | + { headers, tags: { event_type: "pageview" } }, |
| 126 | + ); |
127 | 127 |
|
128 | | - visitorState.referrer = url |
| 128 | + visitorState.referrer = url; |
129 | 129 |
|
130 | | - return response |
| 130 | + return response; |
131 | 131 | } |
132 | 132 |
|
133 | | -function customEvent (visitorState) { |
134 | | - const { origin, ipAddr, referrer } = visitorState |
| 133 | +function customEvent(visitorState) { |
| 134 | + const { origin, ipAddr, referrer } = visitorState; |
135 | 135 | const headers = { |
136 | 136 | Origin: origin, |
137 | | - 'Content-Type': 'application/json', |
138 | | - 'X-Prisme-Referrer': referrer ?? origin, |
139 | | - 'X-Forwarded-For': ipAddr |
140 | | - } |
| 137 | + "Content-Type": "application/json", |
| 138 | + "X-Prisme-Referrer": referrer ?? origin, |
| 139 | + "X-Forwarded-For": ipAddr, |
| 140 | + }; |
141 | 141 |
|
142 | 142 | if (Math.random() < errorRate) { |
143 | 143 | // Invalid origin. |
144 | | - headers.Origin = 'an invalid origin' |
| 144 | + headers.Origin = "an invalid origin"; |
145 | 145 | } |
146 | 146 |
|
147 | | - const eventName = randomItem(['click', 'empty', 'big', 'download']) |
148 | | - let body = {} |
| 147 | + const eventName = randomItem(["click", "empty", "big", "download"]); |
| 148 | + let body = {}; |
149 | 149 | switch (eventName) { |
150 | | - case 'click': |
151 | | - body = { x: Math.round(Math.random() * 100), y: Math.round(Math.random() * 100) } |
152 | | - break |
| 150 | + case "click": |
| 151 | + body = { |
| 152 | + x: Math.round(Math.random() * 100), |
| 153 | + y: Math.round(Math.random() * 100), |
| 154 | + }; |
| 155 | + break; |
153 | 156 |
|
154 | | - case 'empty': |
155 | | - break |
| 157 | + case "empty": |
| 158 | + break; |
156 | 159 |
|
157 | | - case 'big': |
| 160 | + case "big": |
158 | 161 | for (let i = 0; i < 32; i++) { |
159 | | - body[i] = i |
| 162 | + body[i] = i; |
160 | 163 | } |
161 | | - break |
| 164 | + break; |
162 | 165 |
|
163 | | - case 'download': |
164 | | - body.file = randomItem(['file.pdf', 'summary.pdf', 'company.pdf']) |
165 | | - break |
| 166 | + case "download": |
| 167 | + body.file = randomItem(["file.pdf", "summary.pdf", "company.pdf"]); |
| 168 | + break; |
166 | 169 |
|
167 | 170 | default: |
168 | | - throw new Error('unknown event name: ' + eventName) |
| 171 | + throw new Error("unknown event name: " + eventName); |
169 | 172 | } |
170 | 173 |
|
171 | 174 | const response = http.post( |
172 | | - 'http://prisme.localhost/api/v1/events/custom/' + eventName, |
| 175 | + "http://prisme.localhost/api/v1/events/custom/" + eventName, |
173 | 176 | JSON.stringify(body), |
174 | | - { headers, tags: { event_type: 'custom' } } |
175 | | - ) |
| 177 | + { headers, tags: { event_type: "custom" } }, |
| 178 | + ); |
176 | 179 |
|
177 | | - return response |
| 180 | + return response; |
178 | 181 | } |
179 | 182 |
|
180 | | -function randomItem (items) { |
181 | | - const index = Math.floor(Math.random() * items.length) |
182 | | - return items[index] |
| 183 | +function randomItem(items) { |
| 184 | + const index = Math.floor(Math.random() * items.length); |
| 185 | + return items[index]; |
183 | 186 | } |
184 | 187 |
|
185 | | -function randomIP () { |
186 | | - const addr = [] |
| 188 | +function randomIP() { |
| 189 | + const addr = []; |
187 | 190 | for (let i = 0; i < 4; i++) { |
188 | | - addr.push(Math.floor(Math.random() * 255)) |
| 191 | + addr.push(Math.floor(Math.random() * 255)); |
189 | 192 | } |
190 | 193 |
|
191 | | - return addr.map((b) => b.toString()).join('.') |
| 194 | + return addr.map((b) => b.toString()).join("."); |
192 | 195 | } |
0 commit comments