diff --git a/dist/ttfb-go b/dist/ttfb-go index 2d51280..79abe67 100755 Binary files a/dist/ttfb-go and b/dist/ttfb-go differ diff --git a/dist/ttfb-go_darwin_amd64 b/dist/ttfb-go_darwin_amd64 index aa85c26..e79dc61 100755 Binary files a/dist/ttfb-go_darwin_amd64 and b/dist/ttfb-go_darwin_amd64 differ diff --git a/dist/ttfb-go_darwin_arm64 b/dist/ttfb-go_darwin_arm64 index a73b91c..ac4ba57 100755 Binary files a/dist/ttfb-go_darwin_arm64 and b/dist/ttfb-go_darwin_arm64 differ diff --git a/dist/ttfb-go_linux_arm b/dist/ttfb-go_linux_arm index 71293f8..cac03c5 100755 Binary files a/dist/ttfb-go_linux_arm and b/dist/ttfb-go_linux_arm differ diff --git a/dist/ttfb-go_linux_arm64 b/dist/ttfb-go_linux_arm64 index ed0288d..b262fc3 100755 Binary files a/dist/ttfb-go_linux_arm64 and b/dist/ttfb-go_linux_arm64 differ diff --git a/dist/ttfb-go_linux_arm64_static b/dist/ttfb-go_linux_arm64_static index 5df24e9..1c6c92f 100755 Binary files a/dist/ttfb-go_linux_arm64_static and b/dist/ttfb-go_linux_arm64_static differ diff --git a/dist/ttfb-go_linux_arm_static b/dist/ttfb-go_linux_arm_static index 21fcfda..365530c 100755 Binary files a/dist/ttfb-go_linux_arm_static and b/dist/ttfb-go_linux_arm_static differ diff --git a/dist/ttfb-go_static b/dist/ttfb-go_static index 3c7a9a5..21a51af 100755 Binary files a/dist/ttfb-go_static and b/dist/ttfb-go_static differ diff --git a/main.go b/main.go index 9db048b..31e1294 100644 --- a/main.go +++ b/main.go @@ -12,7 +12,7 @@ import ( ) // MeasureTTFB measures the Time to First Byte for the given URL. -func MeasureTTFB(url string, cookie string, verbose bool) (int64, int, error) { +func MeasureTTFB(url string, cookie string) (int64, int, error) { // Create a custom HTTP transport to allow measuring the TTFB. transport := &http.Transport{ TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, @@ -31,44 +31,51 @@ func MeasureTTFB(url string, cookie string, verbose bool) (int64, int, error) { // If a cookie is provided, set it in the request header. if cookie != "" { req.Header.Set("Cookie", cookie) - if verbose { - fmt.Println("Using cookie:", cookie) - } } - if verbose { - fmt.Println("Sending request to:", url) - } + // 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() - // Perform the request. - resp, err := client.Do(req) - if err != nil { + go func() { + // Perform the request + resp, err := client.Do(req) + 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 } - defer resp.Body.Close() - - // Measure the time taken to receive the first byte. - ttfb := time.Since(start).Milliseconds() - - if verbose { - fmt.Println("Received response status:", resp.Status) - } - - return ttfb, resp.StatusCode, nil } func main() { // Define and parse command-line flags. - verbose := flag.Bool("v", false, "Enable verbose output") cookieFile := flag.String("c", "", "Path to file containing the authentication cookie") flag.Parse() // Check if the URL is provided as an argument. if flag.NArg() != 1 { - fmt.Println("Usage: ttfb-go [-v] [-c cookieFile] ") + fmt.Println("Usage: ttfb-go [-c cookieFile] ") os.Exit(1) } @@ -86,17 +93,13 @@ func main() { } // Measure the TTFB. - ttfb, statusCode, err := MeasureTTFB(url, cookie, *verbose) + ttfb, statusCode, err := MeasureTTFB(url, cookie) if err != nil { fmt.Printf("Error measuring TTFB: %v\n", err) os.Exit(1) } - // Print the TTFB and HTTP status code. - if *verbose { - fmt.Printf("Time to First Byte for %s: %d ms, HTTP Status: %d\n", url, ttfb, statusCode) - } else { - fmt.Printf("%d,%d\n", ttfb, statusCode) - } + // Print the HTTP status code and TTFB. + fmt.Printf("%d,%d\n", statusCode, ttfb) }