Skip to content

Instantly share code, notes, and snippets.

@kcollasarundell
Last active January 25, 2023 06:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kcollasarundell/d876a7a64af7b16710fd136865a2e738 to your computer and use it in GitHub Desktop.
Save kcollasarundell/d876a7a64af7b16710fd136865a2e738 to your computer and use it in GitHub Desktop.

How to use.

See the BenchmarkUploadFile this will run an auto sizing benchmark that if you drop the printf stuff from the upload will give you a comparable command for validating changes.

Change the uploadFile func ot suit your need and perhaps remove the individual print of time but you should be able to get comparable numbers from this

go test -bench=.

module moop
go 1.19
package main
import (
"crypto/tls"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"sync"
"time"
)
func main() {
f, err := os.Open("tmp/fakename")
if err != nil {
panic(err)
}
UploadFile("https://fakeurlhere", "randomstring", f)
}
func UploadFile(url, key string, b io.Reader) {
req, err := http.NewRequest("POST", url, b)
if err != nil {
return
}
// Don't forget to set the content type, this will contain the boundary.
req.Header.Set("Content-Type", "bob")
transp, _ := createTransport()
jobCreateTime := time.Now()
resp, err := transp.Do(req)
timeDifference := time.Now().Sub(jobCreateTime)
fmt.Println("time difference is ", timeDifference)
if resp != nil {
defer resp.Body.Close()
responseBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
fmt.Printf("Response is %s %s\n", responseBody, err)
}
}
}
var m sync.Mutex
var DefaultClient *http.Client
func createTransport() (*http.Client, error) {
m.Lock()
defer m.Unlock()
if DefaultClient != nil {
return DefaultClient, nil
}
DefaultClient = http.DefaultClient
defaultRoundTripper := http.DefaultTransport
defaultTransport := defaultRoundTripper.(*http.Transport).Clone()
//if !ok {
// panic(fmt.Sprintf("defaultRoundTripper not an *http.Transport"))
//}
defaultTransport.TLSClientConfig = &tls.Config{
InsecureSkipVerify: true,
}
defaultTransport.MaxIdleConns = 400
defaultTransport.MaxIdleConnsPerHost = 400
defaultTransport.IdleConnTimeout = 60 * time.Minute
defaultTransport.ForceAttemptHTTP2 = false
DefaultClient.Transport = defaultTransport
return DefaultClient, nil
}
package main
import (
"bytes"
"crypto/rand"
"fmt"
"io/ioutil"
"net/http"
"net/http/httptest"
"sync"
"testing"
)
func BenchmarkUploadFile(b *testing.B) {
fs := fakeserver()
wg := &sync.WaitGroup{}
for n := 0; n < b.N; n++ {
wg.Add(1)
blob := make([]byte, 512*1024)
rand.Read(blob)
go func(u string, number int, wg *sync.WaitGroup) {
defer wg.Done()
UploadFile(u, "MOO", bytes.NewReader(blob))
}(fs.URL, n, wg)
}
wg.Wait()
}
func fakeserver() *httptest.Server {
ts := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
_, err := ioutil.ReadAll(r.Body)
if err != nil {
fmt.Printf("error is is %s", err)
}
defer r.Body.Close()
fmt.Fprintln(w, "Hello, client")
}))
createFakeTransport(ts)
return ts
}
func createFakeTransport(ts *httptest.Server) (*http.Client, error) {
m.Lock()
defer m.Unlock()
if DefaultClient != nil {
return DefaultClient, nil
}
DefaultClient = ts.Client()
return DefaultClient, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment