feat(go): step 3 — multiple URLs and -n sampling with min/avg/max
Add probe.Summarize to aggregate N Results into per-phase min/avg/max stats. Wire up -n/--count flag in main to repeat each URL N times and display an aligned three-column table. Single-sample output (n=1) is unchanged. URLs are grouped with a blank line between them. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
66
go/main.go
66
go/main.go
@@ -5,6 +5,7 @@ import (
|
||||
"fmt"
|
||||
"os"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"latprobe/internal/probe"
|
||||
)
|
||||
@@ -39,22 +40,34 @@ func main() {
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
// --json and -n are parsed but not yet functional (wired in Steps 3–4).
|
||||
// --json parsed but not yet functional (wired in Step 4).
|
||||
_ = jsonOut
|
||||
_ = count
|
||||
|
||||
failed := false
|
||||
for i, url := range urls {
|
||||
if i > 0 {
|
||||
fmt.Println()
|
||||
}
|
||||
r := probe.Measure(url)
|
||||
if r.Err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error %s: %v\n", r.URL, r.Err)
|
||||
failed = true
|
||||
|
||||
results := make([]probe.Result, 0, *count)
|
||||
for j := 0; j < *count; j++ {
|
||||
r := probe.Measure(url)
|
||||
if r.Err != nil {
|
||||
fmt.Fprintf(os.Stderr, "error %s (sample %d/%d): %v\n", url, j+1, *count, r.Err)
|
||||
failed = true
|
||||
break
|
||||
}
|
||||
results = append(results, r)
|
||||
}
|
||||
if len(results) == 0 {
|
||||
continue
|
||||
}
|
||||
printResult(r)
|
||||
|
||||
if *count == 1 {
|
||||
printResult(results[0])
|
||||
} else {
|
||||
printAggregate(probe.Summarize(results))
|
||||
}
|
||||
}
|
||||
|
||||
if failed {
|
||||
@@ -62,6 +75,8 @@ func main() {
|
||||
}
|
||||
}
|
||||
|
||||
// ── single-sample output ──────────────────────────────────────────────────────
|
||||
|
||||
func printResult(r probe.Result) {
|
||||
fmt.Printf("%s (%d)\n", r.URL, r.StatusCode)
|
||||
|
||||
@@ -75,17 +90,42 @@ func printResult(r probe.Result) {
|
||||
{"Server (TTFB) ", r.TTFB},
|
||||
{"Transfer ", r.Transfer},
|
||||
}
|
||||
|
||||
for _, ph := range phases {
|
||||
if ph.p.Present {
|
||||
fmt.Printf(" %s : %8.2f ms\n", ph.label, msec(ph.p))
|
||||
fmt.Printf(" %s : %8.2f ms\n", ph.label, ms(ph.p.Duration))
|
||||
}
|
||||
}
|
||||
|
||||
fmt.Println(" " + strings.Repeat("─", 29))
|
||||
fmt.Printf(" %s : %8.2f ms\n", "Total ", msec(r.Total))
|
||||
fmt.Printf(" %s : %8.2f ms\n", "Total ", ms(r.Total.Duration))
|
||||
}
|
||||
|
||||
func msec(p probe.Phase) float64 {
|
||||
return float64(p.Duration.Microseconds()) / 1000
|
||||
// ── multi-sample output ───────────────────────────────────────────────────────
|
||||
|
||||
func printAggregate(a probe.Aggregate) {
|
||||
fmt.Printf("%s (%d, %d samples)\n", a.URL, a.StatusCode, a.Count)
|
||||
fmt.Printf(" %-14s %9s %9s %9s\n", "", "min", "avg", "max")
|
||||
|
||||
phases := []struct {
|
||||
label string
|
||||
p probe.PhaseStats
|
||||
}{
|
||||
{"DNS lookup ", a.DNS},
|
||||
{"TCP connect ", a.Connect},
|
||||
{"TLS handshake ", a.TLS},
|
||||
{"Server (TTFB) ", a.TTFB},
|
||||
{"Transfer ", a.Transfer},
|
||||
}
|
||||
for _, ph := range phases {
|
||||
if ph.p.Present {
|
||||
fmt.Printf(" %s : %6.2f ms %6.2f ms %6.2f ms\n",
|
||||
ph.label, ms(ph.p.Min), ms(ph.p.Avg), ms(ph.p.Max))
|
||||
}
|
||||
}
|
||||
fmt.Println(" " + strings.Repeat("─", 49))
|
||||
fmt.Printf(" %s : %6.2f ms %6.2f ms %6.2f ms\n",
|
||||
"Total ", ms(a.Total.Min), ms(a.Total.Avg), ms(a.Total.Max))
|
||||
}
|
||||
|
||||
func ms(d time.Duration) float64 {
|
||||
return float64(d.Microseconds()) / 1000
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user