package parallel_test import ( "encoding/json" "fmt" "net/http" "testing" "git.tuxpa.in/a/lambda/parallel" ) var requests = make([]*http.Request, 20) func init() { for i := range requests { r, _ := http.NewRequest("GET", fmt.Sprintf("https://jsonplaceholder.typicode.com/todos/%d", i), nil) requests[i] = r } } type someResult struct { Title string `json:"title"` Id int `json:"id"` } func TestParallelRequest4Routine(t *testing.T) { results, errs := parallel.MapErrorV(requests, func(r *http.Request) (*someResult, error) { out := &someResult{} res, err := http.DefaultClient.Do(r) if err != nil { return nil, err } defer res.Body.Close() err = json.NewDecoder(res.Body).Decode(out) return out, err }, 4) for i, v := range results { if errs[i] != nil { t.Logf("err: %s", errs[i]) } else { t.Logf("result: %+v", v) } } } func BenchmarkParallelRequest1Routine(b *testing.B) { for i := 0; i < b.N; i++ { parallel.MapErrorV(requests, func(r *http.Request) (*someResult, error) { out := &someResult{} res, err := http.DefaultClient.Do(r) if err != nil { return nil, err } defer res.Body.Close() err = json.NewDecoder(res.Body).Decode(out) return out, err }, 1) } } func BenchmarkParallelRequest4Routine(b *testing.B) { for i := 0; i < b.N; i++ { parallel.MapErrorV(requests, func(r *http.Request) (*someResult, error) { out := &someResult{} res, err := http.DefaultClient.Do(r) if err != nil { return nil, err } defer res.Body.Close() err = json.NewDecoder(res.Body).Decode(out) return out, err }, 4) } }