diff --git a/benchmarks/http_benchmark.lux b/benchmarks/http_benchmark.lux new file mode 100644 index 0000000..b4126f9 --- /dev/null +++ b/benchmarks/http_benchmark.lux @@ -0,0 +1,37 @@ +// HTTP Server Benchmark +// +// A minimal HTTP server for benchmarking request throughput. +// Run with: lux benchmarks/http_benchmark.lux +// +// Test with: +// wrk -t4 -c100 -d10s http://localhost:8080/ +// OR +// ab -n 10000 -c 100 http://localhost:8080/ +// +// Expected: > 50k req/sec on modern hardware + +fn handleRequest(request: { method: String, path: String, body: String }): { status: Int, body: String } with {Console} = { + // Minimal JSON response for benchmarking + { status: 200, body: "{\"status\":\"ok\"}" } +} + +fn serveLoop(): Unit with {Console, HttpServer} = { + let request = HttpServer.accept() + let response = handleRequest(request) + HttpServer.respond(response.status, response.body) + serveLoop() +} + +fn main(): Unit with {Console, HttpServer} = { + Console.print("HTTP Benchmark Server") + Console.print("Listening on port 8080...") + Console.print("") + Console.print("Test with:") + Console.print(" wrk -t4 -c100 -d10s http://localhost:8080/") + Console.print(" ab -n 10000 -c 100 http://localhost:8080/") + Console.print("") + HttpServer.listen(8080) + serveLoop() +} + +let result = run main() with {} diff --git a/benchmarks/json_benchmark.lux b/benchmarks/json_benchmark.lux new file mode 100644 index 0000000..29303a2 --- /dev/null +++ b/benchmarks/json_benchmark.lux @@ -0,0 +1,81 @@ +// JSON Parsing Benchmark +// +// Benchmarks JSON parsing performance. +// Run with: lux benchmarks/json_benchmark.lux +// +// This benchmark: +// 1. Generates a large JSON string +// 2. Parses it multiple times +// 3. Reports timing + +// Generate a JSON string with n objects +fn generateJsonObject(i: Int): String = { + "{\"id\":" + toString(i) + ",\"name\":\"item" + toString(i) + "\",\"active\":true,\"value\":" + toString(i * 100) + "}" +} + +fn generateJsonArray(n: Int, i: Int, acc: String): String = { + if i >= n then acc + else { + let obj = generateJsonObject(i) + let sep = if i == 0 then "" else "," + generateJsonArray(n, i + 1, acc + sep + obj) + } +} + +fn generateLargeJson(n: Int): String = { + "[" + generateJsonArray(n, 0, "") + "]" +} + +// Simple JSON token counting (simulates parsing) +fn countJsonTokens(json: String, i: Int, count: Int): Int = { + if i >= String.length(json) then count + else { + let char = String.substring(json, i, i + 1) + let newCount = + if char == "{" then count + 1 + else if char == "}" then count + 1 + else if char == "[" then count + 1 + else if char == "]" then count + 1 + else if char == ":" then count + 1 + else if char == "," then count + 1 + else count + countJsonTokens(json, i + 1, newCount) + } +} + +// Run benchmark n times +fn runBenchmark(json: String, n: Int, totalTokens: Int): Int = { + if n <= 0 then totalTokens + else { + let tokens = countJsonTokens(json, 0, 0) + runBenchmark(json, n - 1, totalTokens + tokens) + } +} + +fn main(): Unit with {Console, Time} = { + Console.print("JSON Parsing Benchmark") + Console.print("======================") + Console.print("") + + // Generate large JSON (~100 objects) + Console.print("Generating JSON data...") + let json = generateLargeJson(100) + Console.print(" JSON size: " + toString(String.length(json)) + " bytes") + Console.print("") + + // Benchmark parsing + Console.print("Running benchmark (1000 iterations)...") + let startTime = Time.now() + let totalTokens = runBenchmark(json, 1000, 0) + let endTime = Time.now() + let elapsed = endTime - startTime + + Console.print("") + Console.print("Results:") + Console.print(" Total tokens parsed: " + toString(totalTokens)) + Console.print(" Time: " + toString(elapsed) + " ms") + Console.print(" Iterations per second: " + toString((1000 * 1000) / elapsed)) + Console.print("") +} + +let result = run main() with {}