This commit is contained in:
Colin 2024-05-21 12:19:29 -04:00
parent daf1f92de2
commit 56693b76bc
9 changed files with 33 additions and 30 deletions

BIN
dist/ttfb-go vendored

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
dist/ttfb-go_linux_arm vendored

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

BIN
dist/ttfb-go_static vendored

Binary file not shown.

63
main.go
View File

@ -12,7 +12,7 @@ import (
) )
// MeasureTTFB measures the Time to First Byte for the given URL. // 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. // Create a custom HTTP transport to allow measuring the TTFB.
transport := &http.Transport{ transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, 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 a cookie is provided, set it in the request header.
if cookie != "" { if cookie != "" {
req.Header.Set("Cookie", cookie) req.Header.Set("Cookie", cookie)
if verbose {
fmt.Println("Using cookie:", cookie)
}
} }
if verbose { // Create a channel to capture the time to first byte
fmt.Println("Sending request to:", url) ttfbChan := make(chan int64)
} errorChan := make(chan error)
statusCodeChan := make(chan int)
// Record the start time. // Record the start time
start := time.Now() start := time.Now()
// Perform the request. go func() {
resp, err := client.Do(req) // Perform the request
if err != nil { 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 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() { func main() {
// Define and parse command-line flags. // 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") cookieFile := flag.String("c", "", "Path to file containing the authentication cookie")
flag.Parse() flag.Parse()
// Check if the URL is provided as an argument. // Check if the URL is provided as an argument.
if flag.NArg() != 1 { if flag.NArg() != 1 {
fmt.Println("Usage: ttfb-go [-v] [-c cookieFile] <URL>") fmt.Println("Usage: ttfb-go [-c cookieFile] <URL>")
os.Exit(1) os.Exit(1)
} }
@ -86,17 +93,13 @@ func main() {
} }
// Measure the TTFB. // Measure the TTFB.
ttfb, statusCode, err := MeasureTTFB(url, cookie, *verbose) ttfb, statusCode, err := MeasureTTFB(url, cookie)
if err != nil { if err != nil {
fmt.Printf("Error measuring TTFB: %v\n", err) fmt.Printf("Error measuring TTFB: %v\n", err)
os.Exit(1) os.Exit(1)
} }
// Print the TTFB and HTTP status code. // Print the HTTP status code and TTFB.
if *verbose { fmt.Printf("%d,%d\n", statusCode, ttfb)
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)
}
} }