Last active
August 27, 2025 07:03
-
-
Save up1/f6f648983ca080b24e41f82b3e3ceede to your computer and use it in GitHub Desktop.
Go green tea
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Start with Green tea | |
| $GOEXPERIMENT=greenteagc go run main.go | |
| 2025/08/27 11:01:27 pprof server on :6060 | |
| 2025/08/27 11:01:27 Server running on :8080 with Green Tea GC enabled | |
| /gc/heap/allocs:bytes = {1 2402052896 <nil>} | |
| /gc/heap/frees:bytes = {1 2396205576 <nil>} | |
| /gc/heap/allocs:objects = {1 22943712 <nil>} | |
| /gc/heap/frees:objects = {1 22922079 <nil>} | |
| /gc/heap/live:bytes = {1 5356376 <nil>} | |
| /gc/cycles/total:gc-cycles = {1 539 <nil>} | |
| /gc/pauses:seconds = {3 0 0x14001324390} | |
| # Load test | |
| $wrk -t12 -c400 -d30s http://localhost:8080/items | |
| Running 30s test @ http://localhost:8080/items | |
| 12 threads and 400 connections | |
| Thread Stats Avg Stdev Max +/- Stdev | |
| Latency 9.23ms 12.79ms 327.11ms 92.51% | |
| Req/Sec 4.78k 0.96k 8.06k 87.00% | |
| 1714201 requests in 30.07s, 9.02GB read | |
| Socket errors: connect 0, read 337, write 0, timeout 0 | |
| Requests/sec: 57003.43 | |
| Transfer/sec: 307.04MB |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "encoding/json" | |
| "fmt" | |
| "log" | |
| "math/rand" | |
| "net/http" | |
| "runtime/metrics" | |
| "time" | |
| _ "net/http/pprof" // enables /debug/pprof | |
| ) | |
| type Item struct { | |
| ID int | |
| Name string | |
| Value float64 | |
| } | |
| // handler simulates creating many small objects per request | |
| func handler(w http.ResponseWriter, r *http.Request) { | |
| var items []Item | |
| for i := 0; i < 100; i++ { // allocate lots of small structs | |
| items = append(items, Item{ | |
| ID: i, | |
| Name: randomString(8), | |
| Value: rand.Float64(), | |
| }) | |
| } | |
| w.Header().Set("Content-Type", "application/json") | |
| json.NewEncoder(w).Encode(items) | |
| } | |
| // randomString generates small temporary strings | |
| func randomString(n int) string { | |
| letters := []rune("abcdefghijklmnopqrstuvwxyz") | |
| s := make([]rune, n) | |
| for i := range s { | |
| s[i] = letters[rand.Intn(len(letters))] | |
| } | |
| return string(s) | |
| } | |
| func logGCMetrics() { | |
| ticker := time.NewTicker(5 * time.Second) | |
| defer ticker.Stop() | |
| samples := []metrics.Sample{ | |
| {Name: "/gc/heap/allocs:bytes"}, | |
| {Name: "/gc/heap/frees:bytes"}, | |
| {Name: "/gc/heap/allocs:objects"}, | |
| {Name: "/gc/heap/frees:objects"}, | |
| {Name: "/gc/heap/live:bytes"}, | |
| {Name: "/gc/cycles/total:gc-cycles"}, | |
| {Name: "/gc/pauses:seconds"}, | |
| } | |
| for range ticker.C { | |
| metrics.Read(samples) | |
| for _, s := range samples { | |
| fmt.Printf("%s = %v\n", s.Name, s.Value) | |
| } | |
| fmt.Println("----") | |
| } | |
| } | |
| func main() { | |
| go logGCMetrics() | |
| go func() { | |
| log.Println("pprof server on :6060") | |
| log.Println(http.ListenAndServe("localhost:6060", nil)) | |
| }() | |
| rand.Seed(time.Now().UnixNano()) | |
| http.HandleFunc("/items", handler) | |
| log.Println("Server running on :8080 with Green Tea GC enabled") | |
| log.Fatal(http.ListenAndServe(":8080", nil)) | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment