/
baker_test.go
67 lines (59 loc) · 1.61 KB
/
baker_test.go
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
package main
import (
"crypto/rand"
"encoding/base64"
"io"
"io/ioutil"
"net/http"
"testing"
)
var bakerClient *http.Client
// GenerateRandomBytes returns securely generated random bytes.
// It will return an error if the system's secure random
// number generator fails to function correctly, in which
// case the caller should not continue.
func GenerateRandomBytes(n int) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
// Note that err == nil only if we read len(b) bytes.
if err != nil {
return nil, err
}
return b, nil
}
// GenerateRandomString returns a URL-safe, base64 encoded
// securely generated random string.
// It will return an error if the system's secure random
// number generator fails to function correctly, in which
// case the caller should not continue.
func GenerateRandomString(s int) (string, error) {
b, err := GenerateRandomBytes(s)
return base64.URLEncoding.EncodeToString(b), err
}
func init() {
transport := http.DefaultTransport.(*http.Transport)
transport.MaxIdleConnsPerHost = 100
bakerClient = &http.Client{
Transport: transport,
}
}
func BenchmarkGenQrcodeImg(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
// Each goroutine has its own bytes.Buffer.
for pb.Next() {
// Example: this will give us a 44 byte, base64 encoded output
token, err := GenerateRandomString(32)
if err != nil {
panic(err)
} else {
resp, err := bakerClient.Get("http://localhost:8080/merchant_qrcode?content=hehehe" + token)
if err != nil {
panic(err)
} else {
defer resp.Body.Close()
io.Copy(ioutil.Discard, resp.Body)
}
}
}
})
}