Skip to content

Instantly share code, notes, and snippets.

@ethanquix
Created January 12, 2024 17:18
Show Gist options
  • Save ethanquix/b42a96a527a15c7fb73dff83da4addf5 to your computer and use it in GitHub Desktop.
Save ethanquix/b42a96a527a15c7fb73dff83da4addf5 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
"log"
"net/http"
"sync"
"testing"
"time"
"golang.org/x/net/context/ctxhttp"
)
func doReq(wg *sync.WaitGroup) {
defer wg.Done()
ctx, _ := context.WithTimeout(context.Background(), time.Millisecond*1000)
//defer cancel()
req, err := http.NewRequest("GET", "https://jsonplaceholder.typicode.com/posts", nil)
if err != nil {
log.Fatal(err)
}
//req = req.WithContext(ctx)
_, err = ctxhttp.Do(ctx, http.DefaultClient, req)
}
func TestABC(t *testing.T) {
now := time.Now()
wg := &sync.WaitGroup{}
for i := 0; i < 400; i++ {
wg.Add(1)
go doReq(wg)
}
wg.Wait()
fmt.Printf("took %d ms\n", time.Since(now).Milliseconds())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment