Skip to content

Commit d3f20d5

Browse files
authored
Add: Go bench in examples (#102)
1 parent 750163c commit d3f20d5

File tree

4 files changed

+491
-0
lines changed

4 files changed

+491
-0
lines changed

examples/bench.go

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
"io"
7+
"math/rand"
8+
"net"
9+
"os"
10+
"time"
11+
"bytes"
12+
"flag"
13+
"strconv"
14+
)
15+
16+
var(
17+
limitSeconds int
18+
numConnections int
19+
hostname string
20+
port int
21+
batch int
22+
html bool
23+
req string
24+
buffer bytes.Buffer
25+
)
26+
27+
func load_buffer() {
28+
for i := 0; i < batch; i++ {
29+
a := rand.Intn(1000)
30+
b := rand.Intn(1000)
31+
if ( batch > 1 ) { buffer.WriteString(fmt.Sprintf(`[`)) }
32+
if i < batch-1 {
33+
buffer.WriteString(fmt.Sprintf(`{"jsonrpc":"2.0","method":"validate_session","params":{"user_id":%d,"session_id":%d},"id":%d},`, a, b, i))
34+
} else {
35+
buffer.WriteString(fmt.Sprintf(`{"jsonrpc":"2.0","method":"validate_session","params":{"user_id":%d,"session_id":%d},"id":%d}`, a, b, i))
36+
}
37+
}
38+
if ( batch > 1 ) { buffer.WriteString(fmt.Sprintf(`]`)) }
39+
//fmt.Printf("%s\n",buffer.String())
40+
}
41+
42+
func client(c chan int, tcpAddr *net.TCPAddr, tid int ) {
43+
reply := make([]byte, 4096)
44+
transmits := 0
45+
conn, err := net.DialTCP("tcp", nil, tcpAddr)
46+
if err != nil {
47+
println("Dial failed:", err.Error())
48+
os.Exit(1)
49+
}
50+
51+
start := time.Now()
52+
for {
53+
_, err = conn.Write(buffer.Bytes())
54+
if err != nil {
55+
fmt.Printf("Write Error: %v\n", err)
56+
break
57+
}
58+
59+
_, err := conn.Read(reply)
60+
//fmt.Printf("Reply\n%s",reply[:l])
61+
if err != nil && !errors.Is(err, io.EOF) {
62+
break
63+
}
64+
if time.Since(start).Seconds() >= float64(limitSeconds) {
65+
break
66+
}
67+
transmits++
68+
}
69+
conn.Close()
70+
c <- transmits
71+
}
72+
73+
func formatInt(number int64) string {
74+
output := strconv.FormatInt(number, 10)
75+
startOffset := 3
76+
if number < 0 {
77+
startOffset++
78+
}
79+
for outputIndex := len(output); outputIndex > startOffset; {
80+
outputIndex -= 3
81+
output = output[:outputIndex] + "," + output[outputIndex:]
82+
}
83+
return output
84+
}
85+
86+
87+
func main() {
88+
89+
flag.StringVar(&hostname, "h", "localhost", "hostname")
90+
flag.IntVar(&port, "p", 8545, "port")
91+
flag.IntVar(&numConnections, "c", 16, "Number of connections")
92+
flag.IntVar(&limitSeconds, "s", 2, "Stop after n seconds")
93+
flag.IntVar(&batch, "b", 1, "Batch n requests together")
94+
flag.BoolVar(&html, "html", false, "Send an html request instead of jsonrpc")
95+
flag.Parse()
96+
97+
fmt.Printf("DELME before connecting")
98+
servAddr := fmt.Sprintf(`%s:%d`,hostname,port)
99+
tcpAddr, err := net.ResolveTCPAddr("tcp", servAddr)
100+
if err != nil {
101+
println("ResolveTCPAddr failed:", err.Error())
102+
os.Exit(1)
103+
}
104+
105+
load_buffer();
106+
107+
fmt.Printf("Benchmarking jsonrpc for %d seconds with %d connections and a batch size of %d \n", limitSeconds, numConnections, batch);
108+
109+
start := time.Now()
110+
c := make(chan int)
111+
for i := 0; i < numConnections; i++ {
112+
go client( c, tcpAddr, i )
113+
}
114+
115+
// Wait for all connections to finish
116+
transmits := 0
117+
for i := 0; i < numConnections; i++ {
118+
transmits += <-c
119+
}
120+
121+
elapsed := time.Since(start)
122+
latency := float64(elapsed.Microseconds()) / float64(transmits)
123+
speed := int64((float64(transmits) / float64(elapsed.Seconds())) * float64(batch))
124+
fmt.Printf(" %s commands/second, mean latency %.1fu\n", formatInt(speed), latency)
125+
126+
}

examples/lua/json-echo.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- example script demonstrating HTTP pipelining
2+
3+
init = function(args)
4+
5+
depth = tonumber(args[1]) or 1
6+
wrk.headers["Content-Type"] = "application/json"
7+
8+
local r = {}
9+
for i=1,depth do
10+
r[i] = wrk.format('POST','/', nil, '{"jsonrpc":"2.0","method":"echo","params":{"data":"echomesomedata"},"id":0}')
11+
end
12+
req = table.concat(r)
13+
14+
end
15+
16+
request = function()
17+
return req
18+
end

examples/lua/json.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- example script demonstrating HTTP pipelining
2+
3+
init = function(args)
4+
5+
depth = tonumber(args[1]) or 1
6+
wrk.headers["Content-Type"] = "application/json"
7+
8+
local r = {}
9+
for i=1,depth do
10+
r[i] = wrk.format('POST','/json', nil, '{"jsonrpc":"2.0","method":"validate_session","params":{"user_id":55,"session_id":21},"id":0}')
11+
end
12+
req = table.concat(r)
13+
14+
end
15+
16+
request = function()
17+
return req
18+
end

0 commit comments

Comments
 (0)