godummy: cpu/ram endpoints
This commit is contained in:
+1
-1
@@ -1,3 +1,3 @@
|
|||||||
module github.com/tikinang/godummy
|
module github.com/tikinang/godummy
|
||||||
|
|
||||||
go 1.23.1
|
go 1.26.5
|
||||||
|
|||||||
+43
-1
@@ -1,14 +1,56 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/sha256"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strconv"
|
||||||
|
"sync"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
var BuildTime string
|
||||||
|
|
||||||
|
const ramChunkSize = 100 * 1024 * 1024 // 0.1 GiB
|
||||||
|
|
||||||
|
var (
|
||||||
|
ramMu sync.Mutex
|
||||||
|
ramChunks [][]byte
|
||||||
)
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
w.WriteHeader(http.StatusOK)
|
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)
|
_ = http.ListenAndServe(":8080", nil)
|
||||||
}
|
}
|
||||||
|
|||||||
+2
-4
@@ -1,15 +1,13 @@
|
|||||||
zerops:
|
zerops:
|
||||||
- setup: app
|
- setup: app
|
||||||
build:
|
build:
|
||||||
os: ubuntu
|
base: ubuntu/go@latest
|
||||||
base: go@latest
|
|
||||||
buildCommands:
|
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
|
cache: true
|
||||||
deployFiles:
|
deployFiles:
|
||||||
- app
|
- app
|
||||||
run:
|
run:
|
||||||
os: ubuntu
|
|
||||||
base: ubuntu@latest
|
base: ubuntu@latest
|
||||||
ports:
|
ports:
|
||||||
- port: 8080
|
- port: 8080
|
||||||
|
|||||||
Reference in New Issue
Block a user