-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathflask-mysql-benchmarks.js
More file actions
130 lines (121 loc) · 4.5 KB
/
flask-mysql-benchmarks.js
File metadata and controls
130 lines (121 loc) · 4.5 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import http from 'k6/http';
import { check, sleep, fail } from 'k6';
import exec from 'k6/execution';
import { Trend } from 'k6/metrics';
const BASE_URL_8086 = 'http://localhost:8086';
const BASE_URL_8087 = 'http://localhost:8087';
export const options = {
vus: 1, // Number of virtual users
thresholds: {
test_40mb_payload: [{
threshold: "avg<15", // This is a higher threshold due to the data being processed
abortOnFail: true,
delayAbortEval: '10s',
}],
test_multiple_queries: [{
threshold: "avg<6",
abortOnFail: true,
delayAbortEval: '10s',
}],
test_multiple_queries_with_big_body: [{
threshold: "avg<6",
abortOnFail: true,
delayAbortEval: '10s',
}],
test_create_with_big_body: [{
threshold: "avg<6",
abortOnFail: true,
delayAbortEval: '10s',
}],
test_normal_route: [{
threshold: "avg<6",
abortOnFail: true,
delayAbortEval: '10s',
}],
test_id_route: [{
threshold: "avg<6",
abortOnFail: true,
delayAbortEval: '10s',
}],
test_open_file: [{
threshold: "avg<6",
abortOnFail: true,
delayAbortEval: '10s',
}],
test_execute_shell: [{
threshold: "avg<10",
abortOnFail: true,
delayAbortEval: '10s',
}],
},
};
const default_headers = {
"User-Agent":
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36",
};
const default_payload = {
dog_name: "Pops",
other_dogs: Array(2000).fill("Lorem Ipsum"),
other_dogs2: Array.from({length: 5000}, () => Math.floor(Math.random() * 99999999)),
text_message: "Lorem ipsum dolor sit amet".repeat(3000)
};
function generateLargeJson(sizeInMB) {
const sizeInBytes = sizeInMB * 1024; // Convert MB to Kilobytes
let long_text = "b".repeat(sizeInBytes)
return {
dog_name: "test",
long_texts: new Array(1024).fill(long_text)
}
}
function measureRequest(url, method = 'GET', payload, status_code=200, headers=default_headers) {
let res;
if (method === 'POST') {
res = http.post(url, payload, {
headers: headers
}
);
} else {
res = http.get(url, {
headers: headers
});
}
check(res, {
'status is correct': (r) => r.status === status_code,
});
return res.timings.duration; // Return the duration of the request
}
function route_test(trend, amount, route, method="GET", data=default_payload, status=200) {
for (let i = 0; i < amount; i++) {
let time_with_fw = measureRequest(BASE_URL_8086 + route, method, data, status)
let time_without_fw = measureRequest(BASE_URL_8087 + route, method, data, status)
trend.add(time_with_fw - time_without_fw)
}
}
export function handleSummary(data) {
for (const [metricName, metricValue] of Object.entries(data.metrics)) {
if(!metricName.startsWith('test_') || metricValue.values.avg == 0) {
continue
}
let values = metricValue.values
console.log(`🚅 ${metricName}: ΔAverage is ${values.avg.toFixed(2)}ms | ΔMedian is ${values.med.toFixed(2)}ms.`);
}
return {stdout: ""};
}
let test_40mb_payload = new Trend('test_40mb_payload')
let test_multiple_queries = new Trend("test_multiple_queries")
let test_multiple_queries_with_big_body = new Trend("test_multiple_queries_with_big_body")
let test_create_with_big_body = new Trend("test_create_with_big_body")
let test_normal_route = new Trend("test_normal_route")
let test_id_route = new Trend("test_id_route")
let test_open_file = new Trend("test_open_file")
let test_execute_shell = new Trend("test_execute_shell")
export default function () {
route_test(test_40mb_payload, 30, "/create", "POST", generateLargeJson(40)) // 40 Megabytes
route_test(test_multiple_queries, 50, "/multiple_queries", "POST", {dog_name: "W"})
route_test(test_multiple_queries_with_big_body, 50, "/multiple_queries", "POST")
route_test(test_create_with_big_body, 500, "/create", "POST")
route_test(test_normal_route, 500, "/")
route_test(test_id_route, 500, "/dogpage/1")
route_test(test_open_file, 500, "/open_file", 'POST', { filepath: '.env.example' })
route_test(test_execute_shell, 500, "/shell", "POST", { command: 'xyzwh'})
}