/
client_example_main.go
121 lines (94 loc) · 2.77 KB
/
client_example_main.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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package main
import (
"log"
"github.com/trendrr/goshire/cheshire"
"time"
c "github.com/trendrr/goshire/client"
// "os"
// "runtime/pprof"
_ "net/http/pprof"
"net/http"
"runtime"
)
func asyncTest(client c.Client, total int) {
resChan := make(chan *cheshire.Response, 20)
errorChan := make(chan error, 200)
start := time.Now().Unix()
sent := total
go func() {
for i :=0; i < total; i++ {
if i % 1000 == 0 {
log.Printf("Sending %d", i)
}
err := client.ApiCall(cheshire.NewRequest("/ping", "GET"), resChan, errorChan)
if err != nil {
log.Printf("apicall error %s", err)
sent--
}
// if i % 2 == 0 {
// time.Sleep(1 * time.Millisecond)
// }
}
}()
count := 0
log.Println("Starting select!")
for {
select {
case res := <-resChan:
count++
if count % 1000 == 0 {
log.Printf("Recieved 1000 more, total %d, total time: %d", count, (time.Now().Unix() - start))
log.Printf("RESULT %s", res)
}
case err :=<-errorChan:
count++
log.Printf("ERROR FROM CHAN %s", err)
}
if count == sent {
log.Println("FINISHED!")
break
}
}
log.Printf("Pinged %d in %d", total, (time.Now().Unix() - start))
}
func syncTest(client c.Client, total int) {
start := time.Now().Unix()
for i :=0; i < total; i++ {
if i % 1000 == 0 {
log.Printf("Sending %d", i)
}
_, err := client.ApiCallSync(cheshire.NewRequest("/ping", "GET"), 2 * time.Second)
if err != nil {
log.Printf("apicall error %s", err)
}
}
log.Printf("Pinged %d in %d", total, (time.Now().Unix() - start))
}
func main() {
runtime.GOMAXPROCS(runtime.NumCPU())
//assumes a running server on port 8009
log.Println("HERE")
// start the http server for profiling
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
// client := NewHttp("localhost:8010")
client := c.NewBin("localhost", 8011)
// client := c.NewJson("localhost", 8009)
// with Poolsize = 10 and maxInflight = 500
// 2013/05/16 12:56:28 Pinged 100000 in 38 seconds
client.PoolSize = 10
client.MaxInFlight = 500
err := client.Connect()
if err != nil {
log.Println(err)
}
defer client.Close()
//warm it up
res, err := client.ApiCallSync(cheshire.NewRequest("/ping", "GET"), 10*time.Second)
if err != nil {
log.Printf("error %s")
}
log.Println(res)
asyncTest(client, 1000000)
}