Fixup
This commit is contained in:
parent
56693b76bc
commit
26522adf6e
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
44
main.go
44
main.go
|
@ -30,42 +30,28 @@ func MeasureTTFB(url string, cookie string) (int64, int, error) {
|
||||||
|
|
||||||
// If a cookie is provided, set it in the request header.
|
// If a cookie is provided, set it in the request header.
|
||||||
if cookie != "" {
|
if cookie != "" {
|
||||||
|
// Clean up and format the cookie properly
|
||||||
|
cookie = strings.TrimSpace(cookie)
|
||||||
|
if !strings.Contains(cookie, "=") {
|
||||||
|
return 0, 0, fmt.Errorf("invalid cookie format")
|
||||||
|
}
|
||||||
req.Header.Set("Cookie", cookie)
|
req.Header.Set("Cookie", cookie)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a channel to capture the time to first byte
|
// Record the start time.
|
||||||
ttfbChan := make(chan int64)
|
|
||||||
errorChan := make(chan error)
|
|
||||||
statusCodeChan := make(chan int)
|
|
||||||
|
|
||||||
// Record the start time
|
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
|
|
||||||
go func() {
|
// Perform the request.
|
||||||
// Perform the request
|
resp, err := client.Do(req)
|
||||||
resp, err := client.Do(req)
|
if err != nil {
|
||||||
if err != nil {
|
|
||||||
errorChan <- err
|
|
||||||
return
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
// Measure the time taken to receive the first byte
|
|
||||||
ttfb := time.Since(start).Milliseconds()
|
|
||||||
ttfbChan <- ttfb
|
|
||||||
statusCodeChan <- resp.StatusCode
|
|
||||||
|
|
||||||
// Read the body to ensure we handle the response properly
|
|
||||||
_, _ = ioutil.ReadAll(resp.Body)
|
|
||||||
}()
|
|
||||||
|
|
||||||
select {
|
|
||||||
case ttfb := <-ttfbChan:
|
|
||||||
statusCode := <-statusCodeChan
|
|
||||||
return ttfb, statusCode, nil
|
|
||||||
case err := <-errorChan:
|
|
||||||
return 0, 0, err
|
return 0, 0, err
|
||||||
}
|
}
|
||||||
|
defer resp.Body.Close()
|
||||||
|
|
||||||
|
// Measure the time taken to receive the first byte.
|
||||||
|
ttfb := time.Since(start).Milliseconds()
|
||||||
|
|
||||||
|
return ttfb, resp.StatusCode, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
Loading…
Reference in New Issue