first commit
This commit is contained in:
26
.dockerignore
Normal file
26
.dockerignore
Normal file
@@ -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
|
||||||
21
Dockerfile
Normal file
21
Dockerfile
Normal file
@@ -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"]
|
||||||
69
README.md
Normal file
69
README.md
Normal file
@@ -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=<size>` | Stream `<size>` 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 |
|
||||||
6
docker-compose.yml
Normal file
6
docker-compose.yml
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
services:
|
||||||
|
speedtest-server:
|
||||||
|
build: .
|
||||||
|
ports:
|
||||||
|
- "8080:8080"
|
||||||
|
restart: unless-stopped
|
||||||
102
main.go
Normal file
102
main.go
Normal file
@@ -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=<size>\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"))
|
||||||
|
}
|
||||||
BIN
speedtest-server
Executable file
BIN
speedtest-server
Executable file
Binary file not shown.
Reference in New Issue
Block a user