probe.Measure performs an HTTP GET and records total elapsed time from request start to body fully read. main.go prints URL, status code, and total for each positional URL argument, exiting 1 on any failure. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
61 lines
1.3 KiB
Go
61 lines
1.3 KiB
Go
package main
|
||
|
||
import (
|
||
"flag"
|
||
"fmt"
|
||
"os"
|
||
"time"
|
||
|
||
"latprobe/internal/probe"
|
||
)
|
||
|
||
const usageText = `latprobe — measure per-phase HTTP request latency
|
||
|
||
Usage:
|
||
latprobe [flags] <url> [url ...]
|
||
|
||
Flags:
|
||
-n, --count int Number of requests per URL (default 1)
|
||
--json Output results as JSON instead of text
|
||
-h, --help Show this help
|
||
|
||
Examples:
|
||
latprobe https://example.com
|
||
latprobe -n 5 https://example.com https://www.google.com
|
||
latprobe --json https://example.com | jq .
|
||
`
|
||
|
||
func main() {
|
||
count := flag.Int("count", 1, "number of requests per URL")
|
||
flag.IntVar(count, "n", 1, "number of requests per URL (shorthand)")
|
||
jsonOut := flag.Bool("json", false, "output results as JSON")
|
||
|
||
flag.Usage = func() { fmt.Fprint(os.Stderr, usageText) }
|
||
flag.Parse()
|
||
|
||
urls := flag.Args()
|
||
if len(urls) == 0 {
|
||
fmt.Fprint(os.Stderr, usageText)
|
||
os.Exit(1)
|
||
}
|
||
|
||
// --json and -n are parsed but not yet functional (wired in Steps 3–4).
|
||
_ = jsonOut
|
||
_ = count
|
||
|
||
failed := false
|
||
for _, url := range urls {
|
||
r := probe.Measure(url)
|
||
if r.Err != nil {
|
||
fmt.Fprintf(os.Stderr, "error %s: %v\n", url, r.Err)
|
||
failed = true
|
||
continue
|
||
}
|
||
fmt.Printf("%-45s %d %v\n", r.URL, r.StatusCode, r.Total.Duration.Round(time.Millisecond))
|
||
}
|
||
|
||
if failed {
|
||
os.Exit(1)
|
||
}
|
||
}
|