added basic request count logger

This commit is contained in:
2025-10-18 13:10:20 +02:00
parent 07ca1322bd
commit f4496c7fda
2 changed files with 39 additions and 8 deletions

View File

@@ -6,12 +6,12 @@ use tokio::time::sleep;
#[tokio::main]
async fn main() {
let target = "http://127.0.0.1:6008/"; // adjust your port
let expected_ip = "1.2.3.4";
let start_rps = 10_000;
let max_rps = 100_000;
let step = 1000;
let duration_per_step = Duration::from_secs(2);
let target = "http://ip.louiscreates.com/";
let expected_ip = "95.112.1.65";
let start_rps = 10;
let max_rps = 300;
let step = 10;
let duration_per_step = Duration::from_secs(1);
println!("# Benchmarking {target}");
println!("rate(req/s)\tavg_latency(ms)\terrors");

View File

@@ -4,10 +4,36 @@
#include <unistd.h>
#include <string.h>
#include <stdio.h>
#include <stdatomic.h>
#include <time.h>
#include <pthread.h>
#define PORT 6008
#define BACKLOG 16
#define RECV_BUF 1024
#define RECV_BUF 256
atomic_ullong request_count = 0;
void *logger_thread(void *arg) {
(void)arg;
while (1) {
sleep(24 * 60 * 60); // 1 day
unsigned long long count = atomic_exchange(&request_count, 0);
FILE *f = fopen("requests.log", "a");
if (!f) continue;
time_t now = time(NULL);
struct tm *tm = localtime(&now);
char ts[64];
strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S", tm);
fprintf(f, "%s | %llu requests served\n", ts, count);
fclose(f);
}
return NULL;
}
int main() {
int server_fd = socket(AF_INET, SOCK_STREAM, 0);
@@ -24,6 +50,11 @@ int main() {
printf("listening port %d\nbacklog %d\nrecv buffer %d\n", PORT, BACKLOG, RECV_BUF);
//add logger thread
pthread_t logger;
pthread_create(&logger, NULL, logger_thread, NULL);
pthread_detach(logger);
char buf[RECV_BUF];
const char *hdr = "X-Forwarded-For:";
const size_t hdrlen = strlen(hdr);
@@ -34,7 +65,7 @@ int main() {
ssize_t r = recv(client, buf, sizeof(buf) -1, 0);
if (r <= 0) { close(client); continue; }
buf[r] = 0;
atomic_fetch_add(&request_count, 1);
//find header
char *ip = NULL;
for (ssize_t i = 0; i < r - (ssize_t)hdrlen; ++i) {