Added stats, set test to more cores to check stressing.

This commit is contained in:
2021-11-16 20:28:08 -03:00
parent a722ce0db4
commit 64fe630ae9
4 changed files with 104 additions and 9 deletions
+37
View File
@@ -10,19 +10,46 @@ import (
"regexp"
"simplemq/lib/random"
"simplemq/lib/types"
"strings"
"sync"
"time"
)
func dbg(s string, p ...interface{}) {
log.Printf(s, p...)
}
type MQStats struct {
ActMsgs int64
Nreads float64
AvgMsgs float64
}
func (m *MQStats) Init() {
m.Nreads = 1
go func() {
for {
m.AvgMsgs = float64(m.ActMsgs) / m.Nreads
m.Nreads++
time.Sleep(time.Second)
}
}()
}
func (m *MQStats) Inc() {
m.ActMsgs++
}
type MQServer struct {
pool sync.Map
subs sync.Map
once sync.Map
stats MQStats
}
func (s *MQServer) Stats() MQStats {
return s.stats
}
func (s *MQServer) Get(id string) *MQConn {
raw, ok := s.pool.Load(id)
if !ok {
@@ -91,6 +118,7 @@ func NewServer() *MQServer {
ret.pool = sync.Map{}
ret.subs = sync.Map{}
ret.once = sync.Map{}
ret.stats.Init()
return ret
}
@@ -112,6 +140,7 @@ func (m *MQConn) Recv() error {
m.Close()
return err
}
server.stats.ActMsgs++
//dbg("MQConn.Recv:: Got new message: %s", m.id)
err = json.Unmarshal(mbs, ret)
if err != nil {
@@ -207,6 +236,7 @@ func (m *MQConn) Close() {
func (c *MQConn) Write(m *types.Msg) error {
c.mtx.Lock()
defer c.mtx.Unlock()
server.stats.ActMsgs++
return c.conn.WriteJSON(m)
}
@@ -228,3 +258,10 @@ func Serve(writer http.ResponseWriter, request *http.Request) {
})
mqconn.Loop()
}
func Stats(writer http.ResponseWriter, request *http.Request) {
b := strings.Builder{}
s := server.stats
b.WriteString(fmt.Sprintf("Total Msgs:%v, Avg Msgs/s: %v, NReads: %v", s.ActMsgs, s.AvgMsgs, s.Nreads))
writer.Write([]byte(b.String()))
}