From 4dd66d50bf910586425d971d0d3899a691708f38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Pavl=C3=AD=C4=8Dek?= Date: Thu, 13 Aug 2026 11:15:46 +0200 Subject: [PATCH] godummy: cpu/ram endpoints --- godummy/go.mod | 2 +- godummy/main.go | 44 +++++++++++++++++++++++++++++++++++++++++++- godummy/zerops.yml | 6 ++---- 3 files changed, 46 insertions(+), 6 deletions(-) diff --git a/godummy/go.mod b/godummy/go.mod index 24e17dc..4e8599c 100644 --- a/godummy/go.mod +++ b/godummy/go.mod @@ -1,3 +1,3 @@ module github.com/tikinang/godummy -go 1.23.1 +go 1.26.5 diff --git a/godummy/main.go b/godummy/main.go index 09a2ddc..650e466 100644 --- a/godummy/main.go +++ b/godummy/main.go @@ -1,14 +1,56 @@ package main import ( + "crypto/sha256" "fmt" "net/http" + "strconv" + "sync" + "time" +) + +var BuildTime string + +const ramChunkSize = 100 * 1024 * 1024 // 0.1 GiB + +var ( + ramMu sync.Mutex + ramChunks [][]byte ) func main() { http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) { w.WriteHeader(http.StatusOK) - _, _ = fmt.Fprintln(w, "OK") + _, _ = fmt.Fprintln(w, BuildTime) + }) + http.HandleFunc("/ram", func(w http.ResponseWriter, r *http.Request) { + chunk := make([]byte, ramChunkSize) + for i := range chunk { + chunk[i] = 1 + } + ramMu.Lock() + ramChunks = append(ramChunks, chunk) + total := len(ramChunks) + ramMu.Unlock() + + w.WriteHeader(http.StatusOK) + _, _ = fmt.Fprintf(w, "allocated 0.1 GiB, total held: %.1f GiB (%d chunks)\n", float64(total)*0.1, total) + }) + http.HandleFunc("/cpu", func(w http.ResponseWriter, r *http.Request) { + iterations := 1_000_000 + if n, err := strconv.Atoi(r.URL.Query().Get("n")); err == nil && n > 0 { + iterations = n + } + + start := time.Now() + sum := sha256.Sum256([]byte("zerops")) + for i := 0; i < iterations; i++ { + sum = sha256.Sum256(sum[:]) + } + elapsed := time.Since(start) + + w.WriteHeader(http.StatusOK) + _, _ = fmt.Fprintf(w, "computed %x in %s (%d iterations)\n", sum, elapsed, iterations) }) _ = http.ListenAndServe(":8080", nil) } diff --git a/godummy/zerops.yml b/godummy/zerops.yml index 14ad743..20d8506 100644 --- a/godummy/zerops.yml +++ b/godummy/zerops.yml @@ -1,15 +1,13 @@ zerops: - setup: app build: - os: ubuntu - base: go@latest + base: ubuntu/go@latest buildCommands: - - go build -o app main.go + - go build -ldflags "-X main.BuildTime=$(date -u +%Y-%m-%dT%H:%M:%SZ)" -o app main.go cache: true deployFiles: - app run: - os: ubuntu base: ubuntu@latest ports: - port: 8080