Skip to content

Commit

Permalink
Feat metrics (#59)
Browse files Browse the repository at this point in the history
* add metrics option

* fix t.Fatal in a goroutine
  • Loading branch information
Allenxuxu committed Mar 10, 2021
1 parent 8e27f6e commit ca32d5c
Show file tree
Hide file tree
Showing 17 changed files with 808 additions and 152 deletions.
4 changes: 2 additions & 2 deletions README-ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,7 @@ import (
"github.com/Allenxuxu/gev/connection"
pb "github.com/Allenxuxu/gev/example/protobuf/proto"
"github.com/Allenxuxu/gev/plugins/protobuf"
"github.com/golang/protobuf/proto"
"google.golang.org/protobuf/proto"
)

type example struct{}
Expand Down Expand Up @@ -749,7 +749,7 @@ import (

pb "github.com/Allenxuxu/gev/example/protobuf/proto"
"github.com/Allenxuxu/gev/plugins/protobuf"
"github.com/golang/protobuf/proto"
"google.golang.org/protobuf/proto"
)

func main() {
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -679,7 +679,7 @@ import (
"github.com/Allenxuxu/gev/connection"
pb "github.com/Allenxuxu/gev/example/protobuf/proto"
"github.com/Allenxuxu/gev/plugins/protobuf"
"github.com/golang/protobuf/proto"
"google.golang.org/protobuf/proto"
)

type example struct{}
Expand Down Expand Up @@ -750,7 +750,7 @@ import (

pb "github.com/Allenxuxu/gev/example/protobuf/proto"
"github.com/Allenxuxu/gev/plugins/protobuf"
"github.com/golang/protobuf/proto"
"google.golang.org/protobuf/proto"
)

func main() {
Expand Down
35 changes: 35 additions & 0 deletions benchmarks/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash

set -e

echo ""
echo "--- BENCH PING PONG START ---"
echo ""

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

mkdir -p bin
$(pkill -9 gev-echo-server || printf "")

function gobench {
echo "--- $1 ---"
if [ "$3" != "" ]; then
go build -o $2 $3
fi
GOMAXPROCS=4 $2 --port $4 --loops 4 &

sleep 1
echo "*** 1000 connections, 10 seconds, 4096 byte packets"
GOMAXPROCS=4 go run client/main.go -c 1000 -t 100 -m 4096 -a 127.0.0.1:$4
echo "--- DONE ---"
echo ""
}

gobench "GEV" bin/gev-echo-server ../example/echo/echo.go 5000

23 changes: 22 additions & 1 deletion connection/connection.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import (
"strconv"
"time"

"github.com/Allenxuxu/gev/metrics"

"github.com/Allenxuxu/gev/eventloop"
"github.com/Allenxuxu/gev/log"
"github.com/Allenxuxu/gev/poller"
Expand Down Expand Up @@ -143,8 +145,9 @@ func (c *Connection) WriteBufferLength() int64 {

// HandleEvent 鍐呴儴浣跨敤锛宔vent loop 鍥炶皟
func (c *Connection) HandleEvent(fd int, events poller.Event) {
now := time.Now()
if c.idleTime > 0 {
_ = c.activeTime.Swap(time.Now().Unix())
_ = c.activeTime.Swap(now.Unix())
}

if events&poller.EventErr != 0 {
Expand All @@ -162,6 +165,24 @@ func (c *Connection) HandleEvent(fd int, events poller.Event) {

c.inBufferLen.Swap(int64(c.inBuffer.Length()))
c.outBufferLen.Swap(int64(c.outBuffer.Length()))

if metrics.Enable.Get() {
metrics.ConnBufferCap.WithLabelValues("in", c.PeerAddr()).Set(float64(c.inBuffer.Capacity()))
metrics.ConnBufferLen.WithLabelValues("in", c.PeerAddr()).Set(float64(c.inBuffer.Length()))

metrics.ConnBufferCap.WithLabelValues("out", c.PeerAddr()).Set(float64(c.outBuffer.Capacity()))
metrics.ConnBufferLen.WithLabelValues("out", c.PeerAddr()).Set(float64(c.outBuffer.Length()))

var action string
if events&poller.EventWrite != 0 {
action = "write"
} else if events&poller.EventRead != 0 {
action = "read"
}
if len(action) > 0 {
metrics.ConnHandlerDuration.WithLabelValues(action).Set(float64(time.Since(now).Microseconds()))
}
}
}

func (c *Connection) handlerProtocol(buffer *ringbuffer.RingBuffer) []byte {
Expand Down
10 changes: 10 additions & 0 deletions eventloop/eventloop.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package eventloop

import (
"sync"
"time"

"github.com/Allenxuxu/gev/metrics"

"github.com/Allenxuxu/gev/log"
"github.com/Allenxuxu/gev/poller"
Expand Down Expand Up @@ -131,6 +134,13 @@ func (l *EventLoop) doPendingFunc() {
l.mu.Unlock()

length := len(pf)
if metrics.Enable.Get() && length > 0 {
now := time.Now()
defer func() {
metrics.DoPendingFuncDuration.Set(float64(time.Since(now).Microseconds()))
}()
}

for i := 0; i < length; i++ {
pf[i]()
}
Expand Down
4 changes: 3 additions & 1 deletion example/echo/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ func main() {
s, err := gev.NewServer(handler,
gev.Network("tcp"),
gev.Address(":"+strconv.Itoa(port)),
gev.NumLoops(loops))
gev.NumLoops(loops),
gev.MetricsServer("", ":9091"),
)
if err != nil {
panic(err)
}
Expand Down
4 changes: 3 additions & 1 deletion example/idleconnection/echo.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ func main() {
gev.Network("tcp"),
gev.Address(":"+strconv.Itoa(port)),
gev.NumLoops(loops),
gev.IdleTime(5*time.Second))
gev.IdleTime(5*time.Second),
gev.MetricsServer("", ":9091"),
)
if err != nil {
panic(err)
}
Expand Down
2 changes: 1 addition & 1 deletion example/protobuf/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

pb "github.com/Allenxuxu/gev/example/protobuf/proto"
"github.com/Allenxuxu/gev/plugins/protobuf"
"github.com/golang/protobuf/proto"
"google.golang.org/protobuf/proto"
)

func main() {
Expand Down
Loading

0 comments on commit ca32d5c

Please sign in to comment.