Add http retcode

This commit is contained in:
Colin 2024-05-21 12:07:56 -04:00
parent 09bc16857f
commit ba2c05324a
9 changed files with 8 additions and 8 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.

16
main.go
View File

@ -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, error) {
func MeasureTTFB(url string, cookie string, verbose bool) (int64, int, error) {
// Create a custom HTTP transport to allow measuring the TTFB.
transport := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
@ -25,7 +25,7 @@ func MeasureTTFB(url string, cookie string, verbose bool) (int64, error) {
// Create a new request.
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return 0, err
return 0, 0, err
}
// If a cookie is provided, set it in the request header.
@ -46,7 +46,7 @@ func MeasureTTFB(url string, cookie string, verbose bool) (int64, error) {
// Perform the request.
resp, err := client.Do(req)
if err != nil {
return 0, err
return 0, 0, err
}
defer resp.Body.Close()
@ -57,7 +57,7 @@ func MeasureTTFB(url string, cookie string, verbose bool) (int64, error) {
fmt.Println("Received response status:", resp.Status)
}
return ttfb, nil
return ttfb, resp.StatusCode, nil
}
func main() {
@ -86,17 +86,17 @@ func main() {
}
// Measure the TTFB.
ttfb, err := MeasureTTFB(url, cookie, *verbose)
ttfb, statusCode, err := MeasureTTFB(url, cookie, *verbose)
if err != nil {
fmt.Printf("Error measuring TTFB: %v\n", err)
os.Exit(1)
}
// Only print the TTFB in milliseconds by default.
// Print the TTFB and HTTP status code.
if *verbose {
fmt.Printf("Time to First Byte for %s: %d ms\n", url, ttfb)
fmt.Printf("Time to First Byte for %s: %d ms, HTTP Status: %d\n", url, ttfb, statusCode)
} else {
fmt.Printf("%d\n", ttfb)
fmt.Printf("%d,%d\n", ttfb, statusCode)
}
}