1
0
mirror of https://github.com/astaxie/beego.git synced 2025-06-27 18:40:19 +00:00

Improve monitoring management module

This commit is contained in:
astaxie
2013-11-15 18:08:53 +08:00
parent 18335194bc
commit 097bcb3b5b
4 changed files with 57 additions and 12 deletions

View File

@ -1,7 +1,8 @@
package admin
import (
"encoding/json"
"io"
"strconv"
"sync"
"time"
)
@ -60,14 +61,21 @@ func (m *UrlMap) AddStatistics(requestMethod, requestUrl, requestController stri
}
}
func (m *UrlMap) GetMap() []byte {
func (m *UrlMap) GetMap(rw io.Writer) {
m.lock.RLock()
defer m.lock.RUnlock()
r, err := json.Marshal(m.urlmap)
if err != nil {
return []byte("")
rw.Write([]byte("requestURL avgTime"))
for k, v := range m.urlmap {
rw.Write([]byte(k + ""))
for kk, vv := range v {
rw.Write([]byte(kk))
rw.Write([]byte(strconv.FormatInt(vv.RequestNum, 10)))
rw.Write([]byte(strconv.FormatInt(int64(vv.TotalTime), 10)))
rw.Write([]byte(strconv.FormatInt(int64(vv.MaxTime), 10)))
rw.Write([]byte(strconv.FormatInt(int64(vv.MinTime), 10)))
rw.Write([]byte(strconv.FormatInt(int64(vv.TotalTime)/vv.RequestNum, 10)))
}
}
return r
}
var StatisticsMap *UrlMap

View File

@ -1,6 +1,7 @@
package admin
import (
"os"
"testing"
"time"
)
@ -13,5 +14,5 @@ func TestStatics(t *testing.T) {
StatisticsMap.AddStatistics("POST", "/api/user/astaxie", "&admin.user", time.Duration(1200000))
StatisticsMap.AddStatistics("POST", "/api/user/xiemengjun", "&admin.user", time.Duration(1300000))
StatisticsMap.AddStatistics("DELETE", "/api/user", "&admin.user", time.Duration(1400000))
s := StatisticsMap.GetMap()
StatisticsMap.GetMap(os.Stdout)
}