commit 3f8082abcdde91fff3ef406c5dba950960e972b3 Author: Vyacheslav K Date: Sun Feb 22 23:47:58 2026 +0300 first commit diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 0000000..c2e58e9 --- /dev/null +++ b/.dockerignore @@ -0,0 +1,26 @@ +# Binaries +speedtest-server +*.exe + +# Go +*.o +*.a +vendor/ + +# Git +.git/ +.gitignore + +# IDE +.idea/ +.vscode/ +*.swp +*.swo + +# OS +.DS_Store + +# Keep only what's needed +Dockerfile* +docker-compose* +*.md diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..3d6de14 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,21 @@ +# Build stage +FROM golang:1.25-alpine AS builder + +WORKDIR /app +COPY go.mod go.sum* ./ +RUN go mod download 2>/dev/null || true + +COPY *.go ./ +RUN CGO_ENABLED=0 GOOS=linux go build -ldflags="-w -s" -o speedtest-server . + +# Runtime stage - distroless for minimal attack surface +FROM gcr.io/distroless/static-debian12:latest-amd64 + +WORKDIR / +COPY --from=builder /app/speedtest-server /speedtest-server + +EXPOSE 8080 + +USER nonroot:nonroot + +ENTRYPOINT ["/speedtest-server"] diff --git a/README.md b/README.md new file mode 100644 index 0000000..f5378c5 --- /dev/null +++ b/README.md @@ -0,0 +1,69 @@ +# Speedtest Server + +A lightweight HTTP server written in Go for measuring network bandwidth. Provides download and upload endpoints that clients can use to benchmark their connection speed. + +## How It Works + +- **Download test** — the server streams a requested number of bytes of random data to the client. The random pool (4 MB) is generated once at startup and cycled to fulfill any size request without disk I/O. +- **Upload test** — the client POSTs arbitrary data; the server reads and discards it, then responds with `200 OK`. + +## Endpoints + +| Method | Path | Description | +|--------|------|-------------| +| `GET` | `/download?bytes=` | Stream `` bytes of random data | +| `POST` | `/upload` | Consume uploaded data and acknowledge | + +### Examples + +```bash +# Download 100 MB and measure throughput +curl -o /dev/null -w "%{speed_download} bytes/s\n" \ + "http://localhost:8080/download?bytes=104857600" + +# Upload 50 MB and measure throughput +curl -X POST -o /dev/null -w "%{speed_upload} bytes/s\n" \ + --data-binary @/dev/urandom \ + --limit-rate 50M \ + "http://localhost:8080/upload" +``` + +## Running + +### Locally (requires Go 1.21+) + +```bash +go run main.go +``` + +The server starts on **port 8080**. + +### Docker + +```bash +docker build -t speedtest-server . +docker run -p 8080:8080 speedtest-server +``` + +### Docker Compose + +```bash +docker compose up -d +``` + +The service restarts automatically unless explicitly stopped. + +## Docker Image + +The image uses a two-stage build: + +1. **Builder** — `golang:1.25-alpine` compiles a statically linked binary with debug info stripped (`-ldflags="-w -s"`). +2. **Runtime** — `gcr.io/distroless/static-debian12` — a minimal, shell-less base image. The binary runs as `nonroot:nonroot`. + +## Configuration + +| Parameter | Default | Description | +|-----------|---------|-------------| +| Port | `8080` | Hardcoded in `main.go` | +| Random pool size | `4 MB` | Pre-generated at startup | +| Upload read buffer | `32 KB` | Per-request buffer size | diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..aa5e6d3 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,6 @@ +services: + speedtest-server: + build: . + ports: + - "8080:8080" + restart: unless-stopped diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..d1b43dd --- /dev/null +++ b/go.mod @@ -0,0 +1,3 @@ +module rinuuri/speedtest + +go 1.25.0 diff --git a/main.go b/main.go new file mode 100644 index 0000000..8194c4a --- /dev/null +++ b/main.go @@ -0,0 +1,102 @@ +package main + +import ( + "fmt" + "io" + "math/rand" + "net/http" + "strconv" + "time" +) + +const ( + randomPoolSize = 4 * 1024 * 1024 // 4MB pool of random data, read-only after init + uploadBufSize = 32 * 1024 // per-request local buffer +) + +var randomPool []byte + +func init() { + rng := rand.New(rand.NewSource(time.Now().UnixNano())) + randomPool = make([]byte, randomPoolSize) + rng.Read(randomPool) +} + +func main() { + http.HandleFunc("/download", downloadHandler) + http.HandleFunc("/upload", uploadHandler) + + port := ":8080" + fmt.Printf("Speedtest server starting on http://localhost%s\n", port) + fmt.Printf("Download endpoint: GET /download?bytes=\n") + fmt.Printf("Upload endpoint: POST /upload\n") + + if err := http.ListenAndServe(port, nil); err != nil { + fmt.Printf("Server error: %v\n", err) + } +} + +func downloadHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodGet { + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + return + } + + bytesParam := r.URL.Query().Get("bytes") + if bytesParam == "" { + http.Error(w, "Missing 'bytes' parameter", http.StatusBadRequest) + return + } + + numBytes, err := strconv.ParseInt(bytesParam, 10, 64) + if err != nil || numBytes <= 0 { + http.Error(w, "Invalid 'bytes' parameter", http.StatusBadRequest) + return + } + + w.Header().Set("Content-Type", "application/octet-stream") + w.Header().Set("Content-Length", strconv.FormatInt(numBytes, 10)) + + poolLen := int64(len(randomPool)) + offset := int64(0) + remaining := numBytes + + for remaining > 0 { + start := offset % poolLen + end := start + remaining + if end > poolLen { + end = poolLen + } + n, writeErr := w.Write(randomPool[start:end]) + if writeErr != nil { + return + } + written := int64(n) + remaining -= written + offset += written + } +} + +func uploadHandler(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + http.Error(w, "Method not allowed", http.StatusMethodNotAllowed) + return + } + + defer r.Body.Close() + + buf := make([]byte, uploadBufSize) + for { + _, err := r.Body.Read(buf) + if err == io.EOF { + break + } + if err != nil { + http.Error(w, "Error reading body", http.StatusInternalServerError) + return + } + } + + w.WriteHeader(http.StatusOK) + w.Write([]byte("Upload complete")) +} diff --git a/speedtest-server b/speedtest-server new file mode 100755 index 0000000..5d5f351 Binary files /dev/null and b/speedtest-server differ