Skip to content

Commit

Permalink
Add: Connection tests in Go (#105)
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkReedZ authored May 14, 2024
1 parent 4d9c250 commit 43c2ecd
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 135 deletions.
52 changes: 23 additions & 29 deletions examples/bench.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ import (
"os"
"time"
"bytes"
"flag"
"strconv"
"flag"
"strconv"
)

var(
Expand All @@ -28,15 +28,10 @@ func load_buffer() {
for i := 0; i < batch; i++ {
a := rand.Intn(1000)
b := rand.Intn(1000)
if ( batch > 1 ) { buffer.WriteString(fmt.Sprintf(`[`)) }
if i < batch-1 {
buffer.WriteString(fmt.Sprintf(`{"jsonrpc":"2.0","method":"validate_session","params":{"user_id":%d,"session_id":%d},"id":%d},`, a, b, i))
} else {
buffer.WriteString(fmt.Sprintf(`{"jsonrpc":"2.0","method":"validate_session","params":{"user_id":%d,"session_id":%d},"id":%d}`, a, b, i))
}
jRPC := fmt.Sprintf(`{"jsonrpc":"2.0","method":"validate_session","params":{"user_id":%d,"session_id":%d},"id":0}`, a, b)
buffer.WriteString(fmt.Sprintf("POST / HTTP/1.1\r\nHost: localhost:8545\r\nUser-Agent: python-requests/2.31.0\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: keep-alive\r\nContent-Length: %d\r\nContent-Type: application/json\r\n\r\n%s", len(jRPC), jRPC))
}
if ( batch > 1 ) { buffer.WriteString(fmt.Sprintf(`]`)) }
//fmt.Printf("%s\n",buffer.String())
fmt.Printf("%s\n",buffer.String())
}

func client(c chan int, tcpAddr *net.TCPAddr, tid int ) {
Expand Down Expand Up @@ -86,41 +81,40 @@ func formatInt(number int64) string {

func main() {

flag.StringVar(&hostname, "h", "localhost", "hostname")
flag.IntVar(&port, "p", 8545, "port")
flag.IntVar(&numConnections, "c", 16, "Number of connections")
flag.IntVar(&limitSeconds, "s", 2, "Stop after n seconds")
flag.IntVar(&batch, "b", 1, "Batch n requests together")
flag.BoolVar(&html, "html", false, "Send an html request instead of jsonrpc")
flag.Parse()
flag.StringVar(&hostname, "h", "localhost", "hostname")
flag.IntVar(&port, "p", 8545, "port")
flag.IntVar(&numConnections, "c", 16, "Number of connections")
flag.IntVar(&limitSeconds, "s", 2, "Stop after n seconds")
flag.IntVar(&batch, "b", 1, "Batch n requests together")
flag.BoolVar(&html, "html", false, "Send an html request instead of jsonrpc")
flag.Parse()

load_buffer();

fmt.Printf("DELME before connecting")
servAddr := fmt.Sprintf(`%s:%d`,hostname,port)
servAddr := fmt.Sprintf(`%s:%d`,hostname,port)
tcpAddr, err := net.ResolveTCPAddr("tcp", servAddr)
if err != nil {
println("ResolveTCPAddr failed:", err.Error())
os.Exit(1)
}

load_buffer();

fmt.Printf("Benchmarking jsonrpc for %d seconds with %d connections and a batch size of %d \n", limitSeconds, numConnections, batch);
fmt.Printf("Benchmarking jsonrpc for %d seconds with %d connections and a batch size of %d \n", limitSeconds, numConnections, batch);

start := time.Now()
c := make(chan int)
c := make(chan int)
for i := 0; i < numConnections; i++ {
go client( c, tcpAddr, i )
}

// Wait for all connections to finish
transmits := 0
// Wait for all connections to finish
transmits := 0
for i := 0; i < numConnections; i++ {
transmits += <-c
}

elapsed := time.Since(start)
latency := float64(elapsed.Microseconds()) / float64(transmits)
speed := int64((float64(transmits) / float64(elapsed.Seconds())) * float64(batch))
fmt.Printf(" %s commands/second, mean latency %.1fu\n", formatInt(speed), latency)
elapsed := time.Since(start)
latency := float64(elapsed.Microseconds()) / float64(transmits)
speed := int64((float64(transmits) / float64(elapsed.Seconds())) * float64(batch))
fmt.Printf(" %s commands/second, mean latency %.1fu\n", formatInt(speed), latency)

}
167 changes: 61 additions & 106 deletions examples/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"errors"
"fmt"
"io"
"math/rand"
"net"
"os"
"time"
Expand All @@ -24,53 +23,17 @@ var(
buffer bytes.Buffer
)

func load_buffer( n int) {
for i := 0; i < n; i++ {
a := rand.Intn(1000)
b := rand.Intn(1000)
if ( n > 1 ) { buffer.WriteString(fmt.Sprintf(`[`)) }
if i < n-1 {
buffer.WriteString(fmt.Sprintf(`{"jsonrpc":"2.0","method":"validate_session","params":{"user_id":%d,"session_id":%d},"id":%d},`, a, b, i))
} else {
buffer.WriteString(fmt.Sprintf(`{"jsonrpc":"2.0","method":"validate_session","params":{"user_id":%d,"session_id":%d},"id":%d}`, a, b, i))
}
}
if ( n > 1 ) { buffer.WriteString(fmt.Sprintf(`]`)) }
func load_buffer() {
a := 46
b := 23
jRPC := fmt.Sprintf(`{"jsonrpc":"2.0","method":"validate_session","params":{"user_id":%d,"session_id":%d},"id":0}`, a, b)
buffer.WriteString(fmt.Sprintf("POST / HTTP/1.1\r\nHost: localhost:8545\r\nUser-Agent: python-requests/2.31.0\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: keep-alive\r\nContent-Length: %d\r\nContent-Type: application/json\r\n\r\n%s", len(jRPC), jRPC))
//fmt.Printf("%s\n",buffer.String())
}

func test_rpc(tcpAddr *net.TCPAddr ) {
print(" Test rpc ... ")
conn, err := net.DialTCP("tcp", nil, tcpAddr)
if err != nil {
println("connection failed:", err.Error())
return
}
reply := make([]byte, 4096)

req = fmt.Sprintf(`{"jsonrpc":"2.0","method":"validate_session","params":{"user_id":46,"session_id":0},"id":0}`)
_, err = conn.Write([]byte(req))
if err != nil {
println("write error: %v\n", err)
return
}
n, err := conn.Read(reply)
if err != nil && !errors.Is(err, io.EOF) {
println("read error: %v\n", err)
return
}
rep := fmt.Sprintf(`{"jsonrpc":"2.0","id":0,"result":true}`)
if rep != string(reply[:n]) {
println("unexpected reply")
println(" exp: ", rep)
println(" act: ", string(reply[:n]))
return
}

println("successful")
}
func test_html(tcpAddr *net.TCPAddr ) {
print(" Test html ... ")
func test_http(tcpAddr *net.TCPAddr ) {
print(" Test http ... ")
conn, err := net.DialTCP("tcp", nil, tcpAddr)
if err != nil {
println("connection failed:", err.Error())
Expand Down Expand Up @@ -170,6 +133,20 @@ func test_partial(tcpAddr *net.TCPAddr ) {
println("successful")
}

func test_close(tcpAddr *net.TCPAddr ) {
conn, err := net.DialTCP("tcp", nil, tcpAddr)
if err != nil {
println("connection failed:", err.Error())
return
}
_, err = conn.Write(buffer.Bytes())
if err != nil {
fmt.Printf("Write Error: %v\n", err)
}
conn.Close()
}



func client(c chan int, tcpAddr *net.TCPAddr, tid int ) {
reply := make([]byte, 4096)
Expand Down Expand Up @@ -203,47 +180,18 @@ func client(c chan int, tcpAddr *net.TCPAddr, tid int ) {
conn.Close()
c <- transmits
}
func test_echo(tcpAddr *net.TCPAddr ) {
print(" Test rpc ... ")
conn, err := net.DialTCP("tcp", nil, tcpAddr)
if err != nil {
println("connection failed:", err.Error())
return
}
reply := make([]byte, 4096)

req := fmt.Sprintf(`{"jsonrpc":"2.0","method":"validate_session","params":{"user_id":46,"session_id":0},"id":0}`)
_, err = conn.Write([]byte(req))
if err != nil {
println("write error: %v\n", err)
return
}
n, err := conn.Read(reply)
if err != nil && !errors.Is(err, io.EOF) {
println("read error: %v\n", err)
return
}
rep := fmt.Sprintf(`{"jsonrpc":"2.0","method":"validate_session","params":{"user_id":46,"session_id":0},"id":0}`)
if rep != string(reply[:n]) {
println("unexpected reply")
println(" exp: ", rep)
println(" act: ", string(reply[:n]))
return
}

println("successful")
}

func test_generic(name string, tcpAddr *net.TCPAddr, req []byte, reply []byte ) {
func test_generic(name string, tcpAddr *net.TCPAddr, req []byte, rep []byte ) {
print(" Test ",name," ... ")
conn, err := net.DialTCP("tcp", nil, tcpAddr)
if err != nil {
println("connection failed:", err.Error())
return
}
rbuf := make([]byte, 4096)
request := fmt.Sprintf("POST / HTTP/1.1\r\nHost: localhost:8558\r\nUser-Agent: python-requests/2.31.0\r\nAccept-Encoding: gzip, deflate\r\nAccept: */*\r\nConnection: keep-alive\r\nContent-Length: %d\r\nContent-Type: application/json\r\n\r\n%s", len(req), req)

_, err = conn.Write([]byte(req))
_, err = conn.Write([]byte(request))
if err != nil {
println("write error: %v\n", err)
return
Expand All @@ -253,10 +201,11 @@ func test_generic(name string, tcpAddr *net.TCPAddr, req []byte, reply []byte )
println("read error: %v\n", err)
return
}
if string(reply) != string(rbuf[:n]) {
//reply := fmt.Sprintf("HTTP/1.1 200 OK\r\nContent-Length: %d\r\nContent-Type: application/json\r\n\r\n%s", len(rep), rep)
if string(rep) != string(rbuf[78:n]) {
println("unexpected reply")
println(" exp: ", string(reply))
println(" act: ", string(rbuf[:n]))
println(" exp: ", string(rep))
println(" act: ", string(rbuf[78:n]))
return
}

Expand All @@ -266,19 +215,14 @@ func test_generic(name string, tcpAddr *net.TCPAddr, req []byte, reply []byte )

func main() {

flag.StringVar(&hostname, "h", "localhost", "hostname")
flag.IntVar(&port, "p", 8545, "port")
flag.IntVar(&numConnections, "c", 16, "Number of connections")
flag.IntVar(&limitSeconds, "s", 2, "Stop after n seconds")
flag.IntVar(&batch, "b", 1, "Batch n requests together")
flag.BoolVar(&html, "html", false, "Send an html request instead of jsonrpc")
flag.Parse()

//var b [16]bytes.Buffer
//b[0].WriteString("foo")
//b[1].WriteString("bar")
//fmt.Println(b)

flag.StringVar(&hostname, "h", "localhost", "hostname")
flag.IntVar(&port, "p", 8545, "port")
flag.IntVar(&numConnections, "c", 16, "Number of connections")
flag.IntVar(&limitSeconds, "s", 2, "Stop after n seconds")
flag.IntVar(&batch, "b", 1, "Batch n requests together")
flag.BoolVar(&html, "html", false, "Send an html request instead of jsonrpc")
flag.Parse()

servAddr := fmt.Sprintf(`%s:%d`,hostname,port)
tcpAddr, err := net.ResolveTCPAddr("tcp", servAddr)
if err != nil {
Expand All @@ -287,7 +231,7 @@ func main() {
}

//test_rpc(tcpAddr)
//test_html(tcpAddr)
//test_http(tcpAddr)
//test_big(tcpAddr)
//test_partial(tcpAddr)

Expand All @@ -306,24 +250,35 @@ func main() {

req = fmt.Sprintf(`{"jsonrpc":"2.0","method":"set","params":{"key":"test","value":"val"},"id":0}`)
rep = fmt.Sprintf(`{"jsonrpc":"2.0","id":0,"result":"OK"}`)
test_generic("create_user", tcpAddr, []byte(req), []byte(rep) )
test_generic("set", tcpAddr, []byte(req), []byte(rep) )
req = fmt.Sprintf(`{"jsonrpc":"2.0","method":"get","params":{"key":"test","value":"val"},"id":0}`)
rep = fmt.Sprintf(`{"jsonrpc":"2.0","id":0,"result":"val"}`)
test_generic("create_user", tcpAddr, []byte(req), []byte(rep) )
test_generic("get", tcpAddr, []byte(req), []byte(rep) )


//load_buffer(batch)
print(" Test closing connections ... ")
n := 1
for n < 2000 {
test_close(tcpAddr)
n += 1
}
println("successful")

test_http(tcpAddr)

//start := time.Now()
//c := make(chan int)
////for i := 0; i < numConnections; i++ {
//go client( c, tcpAddr, i )
//}
load_buffer()

// Wait for all connections to finish
//transmits := 0
//for i := 0; i < numConnections; i++ {
//transmits += <-c
//}
print(" Test many connections ... ")
num := 1024
c := make(chan int)
for i := 0; i < num; i++ {
go client( c, tcpAddr, i )
}

// Wait for all connections to finish
transmits := 0
for i := 0; i < num; i++ {
transmits += <-c
}

}

0 comments on commit 43c2ecd

Please sign in to comment.