Skip to content

Commit

Permalink
benchmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
tidwall committed Nov 3, 2017
1 parent 23d55c2 commit 6a86476
Show file tree
Hide file tree
Showing 16 changed files with 305 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<p align="center">
<img
src="resources/logo.png"
src="logo.png"
width="213" height="75" border="0" alt="evio">
<br>
<a href="https://travis-ci.org/tidwall/evio"><img src="https://img.shields.io/travis/tidwall/evio.svg?style=flat-square" alt="Build Status"></a>
Expand Down
2 changes: 2 additions & 0 deletions benchmarks/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
bin/
socket
8 changes: 8 additions & 0 deletions benchmarks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
## evio benchmark tools

Required:

- [wrk](https://github.com/wg/wrk) for HTTP
- [tcpkali](https://github.com/machinezone/tcpkali) for Echo
- [Redis](http://redis.io) for Redis

37 changes: 37 additions & 0 deletions benchmarks/bench-echo.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#!/bin/bash

#set -e

echo ""
echo "--- ECHO START ---"
echo ""

cd $(dirname "${BASH_SOURCE[0]}")
function cleanup {
echo "--- ECHO DONE ---"
kill $(jobs -rp)
wait $(jobs -rp) 2>/dev/null
}
trap cleanup EXIT

mkdir -p bin

$(pkill net-echo-server || printf "")
$(pkill evio-echo-server || printf "")

function gobench {
printf "\e[96m[%s]\e[0m\n" $1
if [ "$3" != "" ]; then
go build -o $2 $3
fi
$2 -port $4 &
sleep 1
echo "Sending 6 byte packets, 50 connections"
nl=$'\r\n'
tcpkali -c 50 -m "PING{$nl}" 127.0.0.1:$4
echo "--- DONE ---"
echo ""
}

gobench "net/echo" bin/net-echo-server net-echo-server/main.go 5001
gobench "evio/echo" bin/evio-echo-server ../examples/echo-server/main.go 5002
39 changes: 39 additions & 0 deletions benchmarks/bench-http.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#!/bin/bash

#set -e

echo ""
echo "--- HTTP START ---"
echo ""

cd $(dirname "${BASH_SOURCE[0]}")
function cleanup {
echo "--- HTTP DONE ---"
kill $(jobs -rp)
wait $(jobs -rp) 2>/dev/null
}
trap cleanup EXIT

mkdir -p bin
$(pkill net-http-server || printf "")
$(pkill fasthttp-server || printf "")
$(pkill iris-server || printf "")
$(pkill evio-http-server || printf "")

function gobench {
printf "\e[96m[%s]\e[0m\n" $1
if [ "$3" != "" ]; then
go build -o $2 $3
fi
$2 -port $4 &
sleep 1
echo "Using 4 threads, 50 connections, 10 seconds"
wrk -t4 -c50 -d10 http://127.0.0.1:$4
echo "--- DONE ---"
echo ""
}

gobench "net/http" bin/net-http-server net-http-server/main.go 8081
gobench "iris" bin/iris-server iris-server/main.go 8082
gobench "fasthttp" bin/fasthttp-server fasthttp-server/main.go 8083
gobench "evio/http" bin/evio-http-server ../examples/http-server/main.go 8084
40 changes: 40 additions & 0 deletions benchmarks/bench-redis.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash

#set -e

echo ""
echo "--- REDIS START ---"
echo ""


cd $(dirname "${BASH_SOURCE[0]}")
function cleanup {
echo "--- REDIS DONE ---"
kill $(jobs -rp)
wait $(jobs -rp) 2>/dev/null
}
trap cleanup EXIT

mkdir -p bin

$(pkill redis-server || printf "")
$(pkill evio-redis-server || printf "")

function gobench {
printf "\e[96m[%s]\e[0m\n" $1
if [ "$3" != "" ]; then
go build -o $2 $3
fi
$2 --port $4 &
sleep 1
echo "Sending pings, 50 connections, 1 packet pipeline"
redis-benchmark -p $4 -t ping -q -P 1
echo "Sending pings, 50 connections, 10 packet pipeline"
redis-benchmark -p $4 -t ping -q -P 10
echo "Sending pings, 50 connections, 20 packet pipeline"
redis-benchmark -p $4 -t ping -q -P 20
echo "--- DONE ---"
echo ""
}
gobench "real/redis" redis-server "" 6392
gobench "evio/redis" bin/evio-redis-server ../examples/redis-server/main.go 6393
9 changes: 9 additions & 0 deletions benchmarks/bench.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

#set -e

cd $(dirname "${BASH_SOURCE[0]}")

./bench-http.sh
./bench-echo.sh
./bench-redis.sh
32 changes: 32 additions & 0 deletions benchmarks/fasthttp-server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright 2017 Joshua J Baker. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

package main

import (
"flag"
"fmt"
"log"

"github.com/valyala/fasthttp"
)

var res string

func main() {
var port int
flag.IntVar(&port, "port", 8080, "server port")
flag.Parse()
go log.Printf("http server started on port %d", port)
err := fasthttp.ListenAndServe(fmt.Sprintf(":%d", port),
func(c *fasthttp.RequestCtx) {
_, werr := c.WriteString("Hello World!\r\n")
if werr != nil {
log.Fatal(werr)
}
})
if err != nil {
log.Fatal(err)
}
}
31 changes: 31 additions & 0 deletions benchmarks/iris-server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright 2017 Joshua J Baker. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

package main

import (
"flag"
"fmt"
"log"

"github.com/kataras/iris"
)

var res string

func main() {
var port int
flag.IntVar(&port, "port", 8080, "server port")
flag.Parse()
go log.Printf("http server started on port %d", port)
app := iris.New()
app.Get("/", func(ctx iris.Context) {
ctx.WriteString("Hello World!\r\n")
})
err := app.Run(iris.Addr(fmt.Sprintf(":%d", port)))
if err != nil {
log.Fatal(err)
}

}
47 changes: 47 additions & 0 deletions benchmarks/net-echo-server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright 2017 Joshua J Baker. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

package main

import (
"flag"
"fmt"
"log"
"net"
)

func main() {
var port int
flag.IntVar(&port, "port", 5000, "server port")
flag.Parse()
ln, err := net.Listen("tcp4", fmt.Sprintf(":%d", port))
if err != nil {
log.Fatal(err)
}
defer ln.Close()
log.Printf("echo server started on port %d", port)
var id int
for {
conn, err := ln.Accept()
if err != nil {
log.Fatal(err)
}
id++
go func(id int, conn net.Conn) {
defer func() {
//log.Printf("closed: %d", id)
conn.Close()
}()
//log.Printf("opened: %d: %s", id, conn.RemoteAddr().String())
var packet [0xFFF]byte
for {
n, err := conn.Read(packet[:])
if err != nil {
return
}
conn.Write(packet[:n])
}
}(id, conn)
}
}
36 changes: 36 additions & 0 deletions benchmarks/net-http-server/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// Copyright 2017 Joshua J Baker. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.

package main

import (
"flag"
"fmt"
"log"
"net/http"
"strings"
)

var res string

func main() {
var port int
var aaaa bool
flag.IntVar(&port, "port", 8080, "server port")
flag.BoolVar(&aaaa, "aaaa", false, "aaaaa....")
flag.Parse()
if aaaa {
res = strings.Repeat("a", 1024)
} else {
res = "Hello World!\r\n"
}
log.Printf("http server started on port %d", port)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(res))
})
err := http.ListenAndServe(fmt.Sprintf(":%d", port), nil)
if err != nil {
log.Fatal(err)
}
}
2 changes: 1 addition & 1 deletion evio_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func serve(events Events, lns []*listener) error {
c.outpos += n
if len(c.outbuf)-c.outpos == 0 {
c.outpos = 0
c.outbuf = nil
c.outbuf = c.outbuf[:0]
}
}
if c.action == Shutdown {
Expand Down
21 changes: 16 additions & 5 deletions examples/echo-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,35 @@
package main

import (
"flag"
"fmt"
"log"
"net"

"github.com/tidwall/evio"
)

func main() {
var events evio.Events
var port int
flag.IntVar(&port, "port", 5000, "server port")
flag.Parse()

var events evio.Events
events.Serving = func(wake func(id int) bool, addrs []net.Addr) (action evio.Action) {
log.Print("echo server started on port 5000")
log.Printf("echo server started on port %d", port)
return
}
events.Opened = func(id int, addr evio.Addr) (out []byte, opts evio.Options, action evio.Action) {
//log.Printf("opened: %d: %s", id, addr.Remote.String())
return
}
events.Closed = func(id int, err error) (action evio.Action) {
//log.Printf("closed: %d", id)
return
}

events.Data = func(id int, in []byte) (out []byte, action evio.Action) {
out = in
return
}

log.Fatal(evio.Serve(events, "tcp://0.0.0.0:5000"))
log.Fatal(evio.Serve(events, fmt.Sprintf("tcp://:%d", port)))
}
20 changes: 5 additions & 15 deletions examples/http-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,28 +63,18 @@ func main() {

events.Opened = func(id int, addr evio.Addr) (out []byte, opts evio.Options, action evio.Action) {
conns[id] = &conn{addr: addr}
log.Printf("%s: opened", addr.Remote.String())
//log.Printf("opened: %d: %s: %s", id, addr.Local.String(), addr.Remote.String())
return
}

events.Closed = func(id int, err error) (action evio.Action) {
c := conns[id]
log.Printf("%s: closed: %v", c.addr.Remote.String(), err)
// c := conns[id]
// log.Printf("closed: %d: %s: %s", id, c.addr.Local.String(), c.addr.Remote.String())
delete(conns, id)
return
}

events.Data = func(id int, in []byte) (out []byte, action evio.Action) {
// out = []byte(`HTTP/1.1 200 OK
// Server: evio
// Date: Thu, 02 Nov 2017 15:48:57 GMT
// Content-Type: text/plain; charset=utf-8
// Content-Length: 1024

// aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa`)

// return

if in == nil {
return
}
Expand Down Expand Up @@ -112,7 +102,7 @@ func main() {
return
}
// We at least want the single http address.
addrs := []string{fmt.Sprintf("tcp://localhost:%d", port)}
addrs := []string{fmt.Sprintf("tcp://:%d", port)}
if tlspem != "" {
// load the cert and key pair from the concat'd pem file.
cer, err := tls.LoadX509KeyPair(tlspem, tlspem)
Expand All @@ -121,7 +111,7 @@ func main() {
}
config := &tls.Config{Certificates: []tls.Certificate{cer}}
// Update the address list to include https.
addrs = append(addrs, fmt.Sprintf("tcp://localhost:%d", tlsport))
addrs = append(addrs, fmt.Sprintf("tcp://:%d", tlsport))

// TLS translate the events
events = evio.Translate(events,
Expand Down
Loading

0 comments on commit 6a86476

Please sign in to comment.