File: bench.go

package info (click to toggle)
golang-github-rootless-containers-bypass4netns 0.4.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 596 kB
  • sloc: sh: 1,936; python: 542; makefile: 33
file content (103 lines) | stat: -rw-r--r-- 2,247 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package main

import (
	"encoding/json"
	"flag"
	"fmt"
	"io"
	"net/http"
	"os"
	"time"
)

var (
	url       = flag.String("url", "http://localhost/blk-1m", "")
	threadNum = flag.Int("thread-num", 1, "")
	count     = flag.Int("count", 1, "")
)

type BenchmarkResult struct {
	Url                string  `json:"url"`
	Count              int     `json:"count"`
	TotalElapsedSecond float64 `json:"totalElapsedSecond"`
	TotalSize          int64   `json:"totalSize"`
}

func main() {
	flag.Parse()

	//fmt.Printf("url = %s\n", *url)
	//fmt.Printf("thread-num = %d\n", *threadNum)
	//fmt.Printf("count = %d\n", *count)

	resultsChan := make(chan BenchmarkResult, *count)

	for i := 0; i < *threadNum; i++ {
		go bench(*url, *count, resultsChan)
	}

	results := []BenchmarkResult{}
	for i := 0; i < *threadNum; i++ {
		r := <-resultsChan
		results = append(results, r)
	}

	res, err := json.Marshal(results)
	if err != nil {
		fmt.Printf("failed Marshal err=%q", err)
		panic("error")
	}
	fmt.Fprintln(os.Stdout, string(res))
}

func bench(url string, count int, resultChan chan BenchmarkResult) {
	bufferSize := 1024 * 1024 * 128 // 128 MiB
	buffer := make([]byte, bufferSize)
	result := BenchmarkResult{
		Url:                url,
		Count:              count,
		TotalElapsedSecond: 0,
		TotalSize:          0,
	}

	for i := 0; i < count; i++ {
		req, err := http.NewRequest("GET", url, nil)
		if err != nil {
			fmt.Printf("failed NewRequest err=%q", err)
			panic("error")
		}
		for {
			start := time.Now()
			resp, err := http.DefaultClient.Do(req)
			if err != nil {
				fmt.Printf("failed Do err=%q, retrying... %d/%d", err, i, count)
				time.Sleep(100 * time.Millisecond)
				continue
			}

			if resp.StatusCode != 200 {
				fmt.Printf("unexpected status code %d", resp.StatusCode)
				panic("error")
			} else {
				for {
					readSize, err := resp.Body.Read(buffer)
					if err != nil && err != io.EOF {
						fmt.Printf("failed Copy() err=%q", err)
						panic("error")
					}
					if readSize == 0 {
						end := time.Now()
						elapsed := end.Sub(start).Seconds()
						result.TotalElapsedSecond += elapsed
						break
					}
					result.TotalSize += int64(readSize)
				}
			}
			resp.Body.Close()
			break
		}
	}

	resultChan <- result
}