1
0
mirror of https://github.com/astaxie/beego.git synced 2024-12-22 17:40:48 +00:00

Add ability to get statistics in json format

This commit is contained in:
Bill Davis 2014-10-09 16:32:56 -04:00
parent a760e46f98
commit ca3e7568a1
2 changed files with 32 additions and 0 deletions

View File

@ -15,6 +15,7 @@
package toolbox
import (
"encoding/json"
"fmt"
"sync"
"time"
@ -111,6 +112,31 @@ func (m *UrlMap) GetMap() map[string]interface{} {
return content
}
func (m *UrlMap) GetMapJSON() ([]byte, error) {
return json.Marshal(m)
}
func (m UrlMap) MarshalJSON() ([]byte, error) {
resultLists := make([]map[string]interface{}, 0)
for k, v := range m.urlmap {
for kk, vv := range v {
result := map[string]interface{}{
"request_url": k,
"method": kk,
"times": vv.RequestNum,
"total_time": toS(vv.TotalTime),
"max_time": toS(vv.MaxTime),
"min_time": toS(vv.MinTime),
"avg_time": toS(time.Duration(int64(vv.TotalTime) / vv.RequestNum)),
}
resultLists = append(resultLists, result)
}
}
return json.Marshal(resultLists)
}
// global statistics data map
var StatisticsMap *UrlMap

View File

@ -28,4 +28,10 @@ func TestStatics(t *testing.T) {
StatisticsMap.AddStatistics("POST", "/api/user/xiemengjun", "&admin.user", time.Duration(13000))
StatisticsMap.AddStatistics("DELETE", "/api/user", "&admin.user", time.Duration(1400))
t.Log(StatisticsMap.GetMap())
jsonString, err := StatisticsMap.GetMapJSON()
if err != nil {
t.Errorf(err.Error())
}
t.Log(string(jsonString))
}