Created
July 14, 2022 15:19
-
-
Save jleedev/56dd2d2e3d1863aae27c05bd29cc4a56 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"crypto/tls" | |
"fmt" | |
"io" | |
"log" | |
"net/http" | |
"sync" | |
"time" | |
) | |
// need caddy running with file_server browse | |
// const URL string = "https://localhost" | |
type Outcome struct { | |
length int | |
duration time.Duration | |
body string | |
} | |
func run(client *http.Client) Outcome { | |
start := time.Now() | |
req, err := http.NewRequest("GET", URL, nil) | |
duration := time.Since(start) | |
if err != nil { | |
log.Fatal(err) | |
} | |
req.Header.Add("Accept", `application/json`) | |
req.Header.Add("Accept-Encoding", `gzip`) | |
req.Header.Add("Pragma", `no-cache`) | |
req.Header.Add("Cache-Control", `no-cache`) | |
resp, err := client.Do(req) | |
if err != nil { | |
log.Fatal(err) | |
} | |
body, err := io.ReadAll(resp.Body) | |
if err != nil { | |
log.Fatal(err) | |
} | |
return Outcome{ | |
length: len(body), | |
duration: duration, | |
body: ``, | |
// body: string(body), | |
} | |
} | |
func bench(client *http.Client, times int, expect Outcome) { | |
wg := sync.WaitGroup{} | |
wg.Add(times) | |
for i := 0; i < times; i++ { | |
time.Sleep(expect.duration) | |
go func() { | |
outcome := run(client) | |
if outcome.length != expect.length { | |
log.Fatalf("expect %#v, got %#v", expect, outcome) | |
} else { | |
fmt.Print(".") | |
} | |
wg.Done() | |
}() | |
} | |
wg.Wait() | |
} | |
func main() { | |
log.Print("ahoyy") | |
tr := &http.Transport{ | |
TLSClientConfig: &tls.Config{ | |
InsecureSkipVerify: true, | |
}, | |
ForceAttemptHTTP2: true, | |
} | |
client := &http.Client{Transport: tr} | |
log.Print("warming up") | |
run(client) | |
log.Print("measuring") | |
expect := run(client) | |
log.Printf("calibrated to %#v", expect) | |
expect.duration = time.Duration(0.8 * float64(expect.duration)) | |
log.Printf("fudged to %#v", expect) | |
for i := 0; i < 10; i++ { | |
bench(client, 4, expect) | |
log.Print("again") | |
time.Sleep(25 * time.Millisecond) | |
} | |
log.Print("gave up") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment