feat(go): step 1 — total wall-clock latency per URL
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>
This commit is contained in:
@@ -1,10 +1,13 @@
|
||||
// Package probe measures per-phase HTTP request latency.
|
||||
package probe
|
||||
|
||||
import "time"
|
||||
import (
|
||||
"io"
|
||||
"net/http"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Phase holds the measured duration of a single request phase.
|
||||
// A zero Duration means the phase did not occur (e.g. TLS on plain HTTP).
|
||||
type Phase struct {
|
||||
Duration time.Duration
|
||||
// Present is false when the phase was skipped (e.g. no TLS for http://).
|
||||
@@ -27,8 +30,27 @@ type Result struct {
|
||||
Err error
|
||||
}
|
||||
|
||||
// Measure performs an HTTP GET request to url and returns a populated Result.
|
||||
// It is a placeholder until Step 1 wires in the actual implementation.
|
||||
// Measure performs an HTTP GET to url and returns a Result.
|
||||
// In Step 1 only the Total phase is populated.
|
||||
func Measure(url string) Result {
|
||||
return Result{URL: url}
|
||||
r := Result{URL: url}
|
||||
|
||||
start := time.Now()
|
||||
|
||||
resp, err := http.Get(url) //nolint:noctx
|
||||
if err != nil {
|
||||
r.Err = err
|
||||
r.Total = Phase{Duration: time.Since(start), Present: true}
|
||||
return r
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
_, err = io.Copy(io.Discard, resp.Body)
|
||||
|
||||
r.Total = Phase{Duration: time.Since(start), Present: true}
|
||||
r.StatusCode = resp.StatusCode
|
||||
if err != nil {
|
||||
r.Err = err
|
||||
}
|
||||
return r
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user