Skip to content

Commit bb74681

Browse files
committed
db proxy
1 parent d432e4c commit bb74681

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

benchmark/db-proxy.js

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/**
2+
* Simple TCP proxy to add artificial latency to MongoDB connections
3+
* This helps make benchmark measurements more stable by simulating network conditions
4+
*/
5+
6+
const net = require('net');
7+
8+
const PROXY_PORT = parseInt(process.env.PROXY_PORT || '27018', 10);
9+
const TARGET_HOST = process.env.TARGET_HOST || 'localhost';
10+
const TARGET_PORT = parseInt(process.env.TARGET_PORT || '27017', 10);
11+
const LATENCY_MS = parseInt(process.env.LATENCY_MS || '10', 10);
12+
13+
const server = net.createServer((clientSocket) => {
14+
const serverSocket = net.createConnection({
15+
host: TARGET_HOST,
16+
port: TARGET_PORT,
17+
});
18+
19+
// Add latency to data flowing from client to MongoDB
20+
clientSocket.on('data', (data) => {
21+
setTimeout(() => {
22+
if (!serverSocket.destroyed) {
23+
serverSocket.write(data);
24+
}
25+
}, LATENCY_MS);
26+
});
27+
28+
// Add latency to data flowing from MongoDB to client
29+
serverSocket.on('data', (data) => {
30+
setTimeout(() => {
31+
if (!clientSocket.destroyed) {
32+
clientSocket.write(data);
33+
}
34+
}, LATENCY_MS);
35+
});
36+
37+
clientSocket.on('error', () => {
38+
serverSocket.destroy();
39+
});
40+
41+
serverSocket.on('error', () => {
42+
clientSocket.destroy();
43+
});
44+
45+
clientSocket.on('close', () => {
46+
serverSocket.destroy();
47+
});
48+
49+
serverSocket.on('close', () => {
50+
clientSocket.destroy();
51+
});
52+
});
53+
54+
server.listen(PROXY_PORT, () => {
55+
console.log(`MongoDB proxy listening on port ${PROXY_PORT}`);
56+
console.log(`Forwarding to ${TARGET_HOST}:${TARGET_PORT} with ${LATENCY_MS}ms latency`);
57+
});
58+
59+
process.on('SIGTERM', () => {
60+
server.close(() => {
61+
process.exit(0);
62+
});
63+
});
64+
65+
process.on('SIGINT', () => {
66+
server.close(() => {
67+
process.exit(0);
68+
});
69+
});

0 commit comments

Comments
 (0)