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.
34
main.go
34
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 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)
|
||||
}
|
||||
|
||||
// Create a channel to capture the time to first byte
|
||||
ttfbChan := make(chan int64)
|
||||
errorChan := make(chan error)
|
||||
statusCodeChan := make(chan int)
|
||||
|
||||
// Record the start time
|
||||
// Record the start time.
|
||||
start := time.Now()
|
||||
|
||||
go func() {
|
||||
// Perform the request
|
||||
// Perform the request.
|
||||
resp, err := client.Do(req)
|
||||
if err != nil {
|
||||
errorChan <- err
|
||||
return
|
||||
return 0, 0, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
|
||||
// Measure the time taken to receive the first byte
|
||||
// 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 ttfb, resp.StatusCode, nil
|
||||
}
|
||||
|
||||
func main() {
|
||||
|
|
Loading…
Reference in New Issue