Created
November 12, 2013 21:24
-
-
Save scottcagno/7438992 to your computer and use it in GitHub Desktop.
tcp benchmarker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// * | |
// * Copyright 2013, Scott Cagno, All rights reserved. | |
// * BSD Licensed - sites.google.com/site/bsdc3license | |
// * NARDb :: Not A Relational Database | |
// * | |
package main | |
import ( | |
"encoding/json" | |
"fmt" | |
"log" | |
"nardb" | |
"net" | |
"sync" | |
"time" | |
) | |
const ( | |
COUNT = 100000 | |
) | |
type Bench struct { | |
name string | |
start, count int | |
conn net.Conn | |
} | |
func InitBench(name, host string, start, count int) *Bench { | |
conn, err := net.Dial("tcp", host) | |
if err != nil { | |
log.Fatal(err) | |
} | |
return &Bench{ | |
name: name, | |
conn: conn, | |
start: start, | |
count: count, | |
} | |
} | |
func (self *Bench) Run(wg *sync.WaitGroup) { | |
dec, enc := json.NewDecoder(self.conn), json.NewEncoder(self.conn) | |
for i := self.start; i < self.start+self.count; i++ { | |
enc.Encode(nardb.Msg{Col: "bnch", Cmd: "set", Key: i, Val: []interface{}{"bar", "baz"}}) | |
var v interface{} | |
dec.Decode(&v) | |
if v == nil { | |
log.Panicln("Empty response!") | |
} | |
} | |
enc.Encode(nardb.Msg{Cmd: "exit"}) | |
self.conn.Close() | |
wg.Done() | |
} | |
func main() { | |
// heading | |
fmt.Println("Benchmarking server...") | |
// initialize 10 benchmark clients | |
// each client sending 100,000 requests | |
b0 := InitBench("b0", "localhost:1234", 0*COUNT, COUNT) | |
b1 := InitBench("b1", "localhost:1234", 1*COUNT, COUNT) | |
b2 := InitBench("b2", "localhost:1234", 2*COUNT, COUNT) | |
b3 := InitBench("b3", "localhost:1234", 3*COUNT, COUNT) | |
b4 := InitBench("b4", "localhost:1234", 4*COUNT, COUNT) | |
b5 := InitBench("b5", "localhost:1234", 5*COUNT, COUNT) | |
b6 := InitBench("b6", "localhost:1234", 6*COUNT, COUNT) | |
b7 := InitBench("b7", "localhost:1234", 7*COUNT, COUNT) | |
b8 := InitBench("b8", "localhost:1234", 8*COUNT, COUNT) | |
b9 := InitBench("b9", "localhost:1234", 9*COUNT, COUNT) | |
// run each benchmark client on a sperate thread | |
// 10 concurrent clients, totaling 1 million requests | |
var wg sync.WaitGroup | |
wg.Add(10) | |
t := time.Now().Unix() | |
go b0.Run(&wg) | |
go b1.Run(&wg) | |
go b2.Run(&wg) | |
go b3.Run(&wg) | |
go b4.Run(&wg) | |
go b5.Run(&wg) | |
go b6.Run(&wg) | |
go b7.Run(&wg) | |
go b8.Run(&wg) | |
go b9.Run(&wg) | |
wg.Wait() | |
ts := time.Now().Unix() - t | |
// server request stats | |
fmt.Printf("Server took %d seconds to complete %d requests (%d/rps)\n", ts, 10*COUNT, int64(10*COUNT)/ts) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment