Skip to content

Instantly share code, notes, and snippets.

@caltuntas
Last active March 4, 2024 20:26
Show Gist options
  • Save caltuntas/c9aa264834ba4d9b05c51c999901cf6f to your computer and use it in GitHub Desktop.
Save caltuntas/c9aa264834ba4d9b05c51c999901cf6f to your computer and use it in GitHub Desktop.
HTTP 1.1 with keep-alive vs. without keep-alive
#!/bin/sh
PROTOCOL="http"
SERVER="localhost"
PORT=5000
URL="$PROTOCOL://$SERVER:$PORT"
/usr/bin/time curl --silent -H "Connection: close" "$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" > /dev/null
#!/bin/sh
PROTOCOL="http"
SERVER="localhost"
PORT=5000
URL="$PROTOCOL://$SERVER:$PORT"
/usr/bin/time curl --silent "$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" \
"$URL" > /dev/null
import http.server
import socketserver
import sys
class HttpServer(http.server.SimpleHTTPRequestHandler):
protocol_version = "HTTP/1.1"
def handle(self):
self.close_connection = False
while not self.close_connection:
self.handle_one_request()
def run(port):
with socketserver.TCPServer(("", port), HttpServer) as httpd:
print("HTTP 1.1 Persistent Connections Server running at port", port)
httpd.serve_forever()
if __name__ == "__main__":
port = int(sys.argv[1]) if len(sys.argv) > 1 else 5000
run(port)
const http = require('http');
const options = {
hostname: 'localhost',
port: 5000,
path: '/',
method: 'GET'
};
const count = 500;
let requestsCompleted = 0;
function sendRequest() {
return new Promise((resolve, reject) => {
const req = http.request(options, (res) => {
res.on('data', () => {});
res.on('end', () => {
requestsCompleted++;
console.log(`Request ${requestsCompleted} completed.`);
resolve();
});
});
req.on('error', (error) => {
console.error('Request error:', error);
reject(error);
});
req.end();
});
}
async function sendSequentialRequests() {
for (let i = 0; i < count; i++) {
try {
await sendRequest();
} catch (error) {
console.error('Failed to send request:', error);
}
}
console.log('All requests completed.');
}
sendSequentialRequests();
const http = require('http');
const options = {
hostname: 'localhost',
port: 5000,
path: '/',
method: 'GET',
agent: new http.Agent({ keepAlive: true })
};
const count = 500;
let requestsCompleted = 0;
function sendRequest() {
return new Promise((resolve, reject) => {
const req = http.request(options, (res) => {
res.on('data', () => {});
res.on('end', () => {
requestsCompleted++;
console.log(`Request ${requestsCompleted} completed.`);
resolve();
});
});
req.on('error', (error) => {
console.error('Request error:', error);
reject(error);
});
req.end();
});
}
async function sendSequentialRequests() {
for (let i = 0; i < count; i++) {
try {
await sendRequest();
} catch (error) {
console.error('Failed to send request:', error);
}
}
console.log('All requests completed.');
}
sendSequentialRequests();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment