-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
171 lines (158 loc) · 4.57 KB
/
server.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const { json } = require("express");
const express = require("express");
const promBundle = require("express-prom-bundle");
const app = express();
const metricsMiddleware = promBundle({ includeMethod: true });
app.use(metricsMiddleware);
app.use(express.json());
const leak_increment_kb = parseInt(
process.env.HEALTHCHECK_LEAK_INCREMENT_KB || 1000
);
let port = process.env.PORT || 8080;
let memory_waste = [];
let total_wasted_kb = 0;
app.get("/", (req, res) => {
console.log(
JSON.stringify({
time: new Date(new Date().toUTCString()),
requestPath: req.path,
requestQuery: req.query,
type: "getBasePath",
message: "Returning hello world",
})
);
res.json({ hello: "World!" });
});
app.get("/random-nr", function (req, res) {
let randomNr = 4; // Random number chosen by fair dice roll
console.log(
JSON.stringify({
time: new Date(new Date().toUTCString()),
requestPath: req.path,
requestQuery: req.query,
type: "getRandomNr",
message: `Returning random nr (${randomNr})`,
})
);
res.json({ randomValue: `${randomNr}` });
});
app.get("/slow-request", (req, res) => {
if (!req.query.request_time) {
res.status(400).send("Bad Request: must send get parameter request_time");
return;
}
console.log(
JSON.stringify({
time: new Date(new Date().toUTCString()),
requestPath: req.path,
requestQuery: req.query,
type: "preSlowRequest",
message: `Entering slow request (${req.query.request_time} seconds)`,
})
);
setTimeout(function () {
console.log(
JSON.stringify({
time: new Date(new Date().toUTCString()),
requestPath: req.path,
requestQuery: req.query,
type: "postSlowRequest",
message: `Slow request done (${req.query.request_time} seconds)`,
})
);
res.json({
waited: req.query.request_time + "s",
});
}, parseFloat(req.query.request_time) * 1000);
});
app.post("/allocate-memory-kb", (req, res) => {
if (!req.body.memory_size_kb) {
res
.status(400)
.send("Bad Request: must send json body with memory_size_kb property");
return;
}
console.log(
JSON.stringify({
time: new Date(new Date().toUTCString()),
requestPath: req.path,
requestQuery: req.query,
type: "preAllocateMemoryKB",
message: `Trying to allocate ${req.body.memory_size_kb} KB`,
})
);
let increment_kb = parseInt(req.body.memory_size_kb);
let increment_byte = increment_kb * 1000;
let increment = "*".repeat(increment_byte);
memory_waste.push(increment);
total_wasted_kb += increment_kb;
// Make sure the data is kept in the working set by accessing it
let lastByte = memory_waste[memory_waste.length - 1].charCodeAt(
increment_byte - 1
);
console.log(
JSON.stringify({
time: new Date(new Date().toUTCString()),
requestPath: req.path,
requestQuery: req.query,
type: "postAllocateMemoryKB",
message: `Allocated ${req.body.memory_size_kb} KB`,
})
);
res.json({
alive: true,
total_wasted_kb: total_wasted_kb,
lastByte: lastByte,
});
});
app.get("/healthcheck", (req, res) => {
if (process.env.HEALTHCHECK_LEAK_MEMORY) {
console.log(
JSON.stringify({
time: new Date(new Date().toUTCString()),
requestPath: req.path,
requestQuery: req.query,
type: "preHealthCheckWithAllocation",
message: `Allocating ${leak_increment_kb} KB`,
})
);
let increment_byte = leak_increment_kb * 1000;
let increment = "*".repeat(increment_byte);
memory_waste.push(increment);
total_wasted_kb += leak_increment_kb;
// Make sure the data is kept in the working set by accessing it
let lastByte = memory_waste[memory_waste.length - 1].charCodeAt(
increment_byte - 1
);
console.log(
JSON.stringify({
time: new Date(new Date().toUTCString()),
requestPath: req.path,
requestQuery: req.query,
type: "postHealthCheckWithAllocation",
message: `Allocated ${leak_increment_kb} KB`,
})
);
res.json({
alive: true,
total_wasted_kb: total_wasted_kb,
lastByte: lastByte,
});
} else {
console.log(
JSON.stringify({
time: new Date(new Date().toUTCString()),
requestPath: req.path,
requestQuery: req.query,
type: "healthCheckStandard",
message: "Healthcheck returning alive=true",
})
);
res.json({
alive: true,
});
}
});
var server = app.listen(port, function () {
console.log(`Example app listening on port ${port}`);
});