Created
October 22, 2021 08:09
-
-
Save monirz/41984736ce5a2dc89f48aee3e1855db7 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 ( | |
"bytes" | |
"fmt" | |
"io" | |
"io/ioutil" | |
"net/http" | |
"runtime" | |
"sync" | |
"time" | |
uuid "github.com/satori/go.uuid" | |
) | |
type result struct { | |
index int | |
res *http.Response | |
statusCode int | |
err error | |
} | |
func PostHouse(id int) (*http.Response, error) { | |
url := "http://ip:8099/api/posts" | |
tr := &http.Transport{ | |
MaxIdleConnsPerHost: 100, | |
MaxIdleConns: 100, | |
MaxConnsPerHost: 100, | |
} | |
client := &http.Client{ | |
Transport: tr, | |
Timeout: 120 * time.Second, | |
} | |
bid := uuid.NewV4() | |
uid := uuid.NewV4() | |
var jsonData = []byte(`{ | |
"bid": "` + bid.String() + `", | |
"hhList": [ | |
{ | |
"uid": "` + uid.String() + `", | |
} | |
] | |
}`) | |
// client.Timeout = time.Second * 200 | |
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData)) | |
req.Close = true | |
req.Header.Set("Content-Type", "application/json") | |
req.Header.Set("Authorization", "Bearer ...") | |
if err != nil { | |
return &http.Response{}, err | |
} | |
res, err := client.Do(req) | |
if err != nil { | |
return &http.Response{}, err | |
} | |
if err != nil { | |
return &http.Response{}, err | |
} | |
io.Copy(ioutil.Discard, res.Body) | |
return res, nil | |
} | |
func main() { | |
runtime.GOMAXPROCS(runtime.NumCPU()) | |
start := time.Now() | |
defer func() { | |
fmt.Println("Execution Time: ", time.Since(start).Seconds()) | |
}() | |
concurrencyLimit := 50000 | |
m := make(map[int]int) | |
semaphoreChan := make(chan struct{}, concurrencyLimit) | |
resultsChan := make(chan *result, concurrencyLimit) | |
wg := sync.WaitGroup{} | |
for i := 0; i < concurrencyLimit; i++ { | |
wg.Add(1) | |
go func(id int) { | |
semaphoreChan <- struct{}{} | |
resp, err := PostHouse(id) | |
if err != nil { | |
fmt.Println("error getting comic", err) | |
} | |
result := &result{id, resp, resp.StatusCode, err} | |
resultsChan <- result | |
<-semaphoreChan | |
wg.Done() | |
}(i) | |
} | |
wg.Wait() | |
close(semaphoreChan) | |
close(resultsChan) | |
for v := range resultsChan { | |
m[v.statusCode]++ | |
if v.err != nil { | |
fmt.Println(v.err) | |
} | |
} | |
for k, v := range m { | |
fmt.Println(k, v) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment