Skip to content

Commit

Permalink
update k6 benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
negrel committed May 23, 2024
1 parent a8bdd00 commit 21363ef
Showing 1 changed file with 153 additions and 63 deletions.
216 changes: 153 additions & 63 deletions tests/k6/events/main.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,176 @@
import http from 'k6/http';
import { Counter } from 'k6/metrics';
import { sleep } from 'k6';

const directTrafficRate = 0.1;
const customEventRate = 0.3;
const errorRate = 0.1;
const bounceRate = 0.5;
const exitRate = 0.1;

export const options = {
thresholds: {
// Thresholds so tags appear in CLI report.
'http_reqs{event_type:pageview}': ['count >= 0'],
'http_reqs{event_type:custom}': ['count >= 0'],
},
discardResponseBodies: true,
scenarios: {
sharedIterationsPageViewEvents: {
executor: "shared-iterations",
vus: 4096,
iterations: 2 ** 19,
exec: "pageView",
iterations: 2 ** 17,
},
sharedIterationsCustomEvents: {
executor: "shared-iterations",
vus: 4096,
iterations: 2 ** 19,
exec: "customEvent",
}
}
}

const origins = [
"mywebsite.localhost",
"foo.mywebsite.localhost",
"someoneelsewebsite.com"
]

export function pageView() {
export default function () {
const origin = [
randomItem(["http", "https"]),
"://",
randomItem(origins),
randomItem(["http", "https"]),
"://",
randomItem(origins),
].join('')
const docReferrer = randomItem([
undefined,
"https://google.com",
"https://duckduckgo.com",
"https://qwant.com",
"https://github.com",
origin,
])

const res = http.post('http://prisme.localhost/api/v1/events/pageviews', null, {
headers: {
"Origin": origin,
"X-Prisme-Referrer": [
origin,
randomItem(["/", "/foo", "/bar", "qux", "/foo/"])
].join(''),
"X-Forwarded-For": randomIP(),
"X-Prisme-Document-Referrer": docReferrer

const ipAddr = randomIP()

// Entry pageview.
const response = pageView({entryPageView: true, origin, ipAddr})
if (response.status !== 200) {
// console.error("entry pageview", response.status_text, response.error)
return
}

// Custom events.
while (Math.random() < customEventRate) {
const response = customEvent({origin, ipAddr})
if (response.status !== 200) {
// console.error("custom event", response.status_text, response.error)
return
}
})
}

// Bounces.
if (Math.random() < bounceRate) {
return
}

let events = 0
while (events < 30) {
// Pageview.
const response = pageView({origin, ipAddr})
if (response.status !== 200) {
// console.error("pageview", response.status_text, response.error)
return
}
events++

// Custom events.
while (Math.random() < customEventRate) {
const response = customEvent({origin, ipAddr})
if (response.status !== 200) {
// console.error("custom event", response.status_text, response.error)
return
}
events++
}

// Exit rate.
if (Math.random() < exitRate) {
return
}
}
}

export function customEvent() {
const origin = [
randomItem(["http", "https"]),
"://",
randomItem(origins),
].join('')
const docReferrer = randomItem([
undefined,
"https://google.com",
"https://duckduckgo.com",
"https://qwant.com",
"https://github.com",
...origins,
])

const res = http.post(`http://prisme.localhost/api/v1/events/custom/${"foo"}`, JSON.stringify({x: 1024, y: 4096}), {
headers: {
"Content-Type": "application/json",
"Origin": origin,
"X-Prisme-Referrer": [
origin,
randomItem(["/", "/foo", "/bar", "qux", "/foo/"])
].join(''),
"X-Forwarded-For": randomIP(),
"X-Prisme-Document-Referrer": docReferrer
function pageView({ entryPageView = false, origin, ipAddr }) {
const headers = {
"Origin": origin,
"X-Prisme-Referrer": [
origin,
randomItem(["/", "/foo", "/bar", "qux", "/foo/"])
].join(''),
// "X-Prisme-Document-Referrer": origin,
"X-Forwarded-For": ipAddr,
}

if (entryPageView) {
if (Math.random() < directTrafficRate) {
delete headers["X-Prisme-Document-Referrer"]
} else {
headers["X-Prisme-Document-Referrer"] = randomItem([
"https://google.com",
"https://duckduckgo.com",
"https://qwant.com",
"https://github.com",
])
}
})
}

if (Math.random() < errorRate) {
// Invalid origin.
headers["Origin"] = "undefined"
}

const response = http.post(
'http://prisme.localhost/api/v1/events/pageviews',
null,
{ headers, tags: { event_type: "pageview" } }
)

return response
}

function customEvent({origin, ipAddr}) {
const headers = {
"Origin": origin,
"Content-Type": "application/json",
"X-Prisme-Referrer": [
origin,
randomItem(["/", "/foo", "/bar", "qux", "/foo/"])
].join(''),
"X-Forwarded-For": ipAddr,
}

if (Math.random() < errorRate) {
// Invalid origin.
headers["Origin"] = "undefined"
}

const eventName = randomItem(["click", "empty", "big", "download"])
let body = {}
switch (eventName) {
case "click":
body = {x: Math.round(Math.random() * 100), y: Math.round(Math.random() * 100) }
break;

case "empty":
break;

case "big":
for (let i = 0; i < 32; i++) {
body[i] = i
}
break;

case "download":
body["file"] = randomItem(["file.pdf", "summary.pdf", "company.pdf"])
break;

default:
throw new Error("unknown event name: " + eventName)
}

const response = http.post(
'http://prisme.localhost/api/v1/events/custom/' + eventName,
JSON.stringify(body),
{ headers, tags: { event_type: "custom" } }
)

return response
}

function randomItem(items) {
Expand All @@ -88,11 +179,10 @@ function randomItem(items) {
}

function randomIP() {
const addr = [10, 10]
// for (let i = 0; i < 2; i++) {
addr.push(Math.floor(Math.random() * 16))
addr.push(Math.floor(Math.random() * 255))
// }
const addr = []
for (let i = 0; i < 4; i++) {
addr.push(Math.floor(Math.random() * 255))
}

return addr.map((b) => b.toString()).join('.')
}

0 comments on commit 21363ef

Please sign in to comment.