Fixup
This commit is contained in:
parent
daf1f92de2
commit
56693b76bc
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.
63
main.go
63
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] <URL>")
|
||||
fmt.Println("Usage: ttfb-go [-c cookieFile] <URL>")
|
||||
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)
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue