Skip to content

Commit 9596966

Browse files
committed
build(benchmark): add fasthttp/net-http hello world baselines and update baremetal script
Add standalone hello-world servers for fasthttp (140k req/s) and net/http (74k req/s) to establish raw HTTP-layer baselines independent of the application. Update baremetal.sh to use a pre-built binary instead of rebuilding from source on each run.
1 parent 71847e4 commit 9596966

File tree

3 files changed

+76
-7
lines changed

3 files changed

+76
-7
lines changed

benchmark/baremetal.sh

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)"
3232
RESULTS_DIR="${SCRIPT_DIR}/results"
3333

3434
# ---------- defaults ---------------------------------------------------------
35-
CONNECTIONS=50
35+
CONNECTIONS=100
3636
REQUESTS=100000
3737
DURATION=""
3838
PORT=8080
@@ -79,15 +79,14 @@ check_deps() {
7979
}
8080

8181
# ---------- helpers ----------------------------------------------------------
82-
BIN="/tmp/static-web-baremetal-$$"
82+
BIN="${PROJECT_ROOT}/static-web"
8383
SERVER_PID=""
8484

8585
cleanup() {
8686
if [ -n "$SERVER_PID" ] && kill -0 "$SERVER_PID" 2>/dev/null; then
8787
kill "$SERVER_PID" 2>/dev/null
8888
wait "$SERVER_PID" 2>/dev/null || true
8989
fi
90-
rm -f "$BIN"
9190
}
9291
trap cleanup EXIT
9392

@@ -161,10 +160,13 @@ main() {
161160
echo -e " ${CYAN}OS/Arch: $(uname -s)/$(uname -m)${RESET}"
162161
echo ""
163162

164-
# ---- build static-web ----------------------------------------------------
165-
echo -e "${BOLD}→ Building static-web...${RESET}"
166-
(cd "$PROJECT_ROOT" && go build -ldflags="-s -w" -o "$BIN" ./cmd/static-web)
167-
echo -e " ${GREEN}Built: ${BIN}${RESET}"
163+
# ---- use pre-built static-web binary -------------------------------------
164+
echo -e "${BOLD}→ Using pre-built static-web: ${BIN}${RESET}"
165+
if [ ! -x "$BIN" ]; then
166+
echo -e "${RED}Binary not found or not executable: ${BIN}${RESET}"
167+
echo -e "${RED}Run: go build -ldflags=\"-s -w\" -o static-web ./cmd/static-web${RESET}"
168+
exit 1
169+
fi
168170
echo ""
169171

170172
# Make sure port is free

benchmark/fasthttp-hello/main.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// Bare-minimum fasthttp server: pre-allocated response, no middleware, no allocs.
2+
// Direct comparison against the net/http hello world to isolate HTTP stack overhead.
3+
package main
4+
5+
import (
6+
"log"
7+
"os"
8+
9+
"github.com/valyala/fasthttp"
10+
)
11+
12+
var (
13+
body = []byte("Hello, World!")
14+
contentType = []byte("text/plain")
15+
contentLen = []byte("13")
16+
)
17+
18+
func handler(ctx *fasthttp.RequestCtx) {
19+
ctx.Response.Header.SetBytesV("Content-Type", contentType)
20+
ctx.Response.Header.SetBytesV("Content-Length", contentLen)
21+
ctx.SetStatusCode(200)
22+
ctx.SetBody(body)
23+
}
24+
25+
func main() {
26+
port := ":8080"
27+
if p := os.Getenv("PORT"); p != "" {
28+
port = ":" + p
29+
}
30+
31+
s := &fasthttp.Server{
32+
Handler: handler,
33+
Name: "fasthttp-hello",
34+
}
35+
36+
log.Printf("fasthttp listening on %s", port)
37+
if err := s.ListenAndServe(port); err != nil {
38+
log.Fatal(err)
39+
}
40+
}

benchmark/raw-hello/main.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Bare-minimum Go HTTP server: pre-allocated response, no middleware, no allocs.
2+
// This establishes the net/http ceiling on this machine.
3+
package main
4+
5+
import (
6+
"net/http"
7+
"os"
8+
)
9+
10+
var body = []byte("Hello, World!")
11+
12+
func main() {
13+
port := ":8080"
14+
if p := os.Getenv("PORT"); p != "" {
15+
port = ":" + p
16+
}
17+
18+
mux := http.NewServeMux()
19+
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
20+
w.Header().Set("Content-Type", "text/plain")
21+
w.Header().Set("Content-Length", "13")
22+
w.WriteHeader(200)
23+
w.Write(body)
24+
})
25+
26+
http.ListenAndServe(port, mux)
27+
}

0 commit comments

Comments
 (0)