xref: /linux-tools/misc/apps/simpleclient.go (revision 08efb56e0eeb408ac7d4c6488994eeb8f587aaa0)
1*08efb56eSDavid Wangpackage main
2*08efb56eSDavid Wang
3*08efb56eSDavid Wangimport (
4*08efb56eSDavid Wang	"fmt"
5*08efb56eSDavid Wang	"io"
6*08efb56eSDavid Wang	"log"
7*08efb56eSDavid Wang	"net/http"
8*08efb56eSDavid Wang    "flag"
9*08efb56eSDavid Wang    "strings"
10*08efb56eSDavid Wang    "time"
11*08efb56eSDavid Wang)
12*08efb56eSDavid Wang
13*08efb56eSDavid Wangvar url = flag.String("url", "", "target server")
14*08efb56eSDavid Wangvar count = flag.Int("n", 128, "loop count")
15*08efb56eSDavid Wangfunc main() {
16*08efb56eSDavid Wang    flag.Parse()
17*08efb56eSDavid Wang    if *url == "" {
18*08efb56eSDavid Wang        log.Fatal("server empty")
19*08efb56eSDavid Wang    }
20*08efb56eSDavid Wang    ss := make(map[string]int)
21*08efb56eSDavid Wang    tr := http.Transport{ DisableKeepAlives: true }
22*08efb56eSDavid Wang    for i:=0; i<*count; i++ {
23*08efb56eSDavid Wang        c := http.Client{Timeout: time.Second, Transport: &tr}
24*08efb56eSDavid Wang        res, err := c.Get(*url)
25*08efb56eSDavid Wang        if err != nil {
26*08efb56eSDavid Wang            log.Fatal(err)
27*08efb56eSDavid Wang        }
28*08efb56eSDavid Wang        body, err := io.ReadAll(res.Body)
29*08efb56eSDavid Wang        res.Body.Close()
30*08efb56eSDavid Wang        if res.StatusCode !=200 {
31*08efb56eSDavid Wang            log.Fatalf("Response failed with status code: %d and\nbody: %s\n", res.StatusCode, body)
32*08efb56eSDavid Wang        }
33*08efb56eSDavid Wang        if err != nil {
34*08efb56eSDavid Wang            log.Fatal(err)
35*08efb56eSDavid Wang        }
36*08efb56eSDavid Wang        ss[strings.Trim(string(body), " \r\t\n")]+=1
37*08efb56eSDavid Wang        c.CloseIdleConnections()
38*08efb56eSDavid Wang    }
39*08efb56eSDavid Wang    for s, c := range(ss) {
40*08efb56eSDavid Wang        fmt.Println(s, c)
41*08efb56eSDavid Wang    }
42*08efb56eSDavid Wang}
43