Add ability to get statistics in json format
Showing
2 changed files
with
32 additions
and
0 deletions
| ... | @@ -15,6 +15,7 @@ | ... | @@ -15,6 +15,7 @@ |
| 15 | package toolbox | 15 | package toolbox |
| 16 | 16 | ||
| 17 | import ( | 17 | import ( |
| 18 | "encoding/json" | ||
| 18 | "fmt" | 19 | "fmt" |
| 19 | "sync" | 20 | "sync" |
| 20 | "time" | 21 | "time" |
| ... | @@ -111,6 +112,31 @@ func (m *UrlMap) GetMap() map[string]interface{} { | ... | @@ -111,6 +112,31 @@ func (m *UrlMap) GetMap() map[string]interface{} { |
| 111 | return content | 112 | return content |
| 112 | } | 113 | } |
| 113 | 114 | ||
| 115 | func (m *UrlMap) GetMapJSON() ([]byte, error) { | ||
| 116 | return json.Marshal(m) | ||
| 117 | } | ||
| 118 | |||
| 119 | func (m UrlMap) MarshalJSON() ([]byte, error) { | ||
| 120 | |||
| 121 | resultLists := make([]map[string]interface{}, 0) | ||
| 122 | |||
| 123 | for k, v := range m.urlmap { | ||
| 124 | for kk, vv := range v { | ||
| 125 | result := map[string]interface{}{ | ||
| 126 | "request_url": k, | ||
| 127 | "method": kk, | ||
| 128 | "times": vv.RequestNum, | ||
| 129 | "total_time": toS(vv.TotalTime), | ||
| 130 | "max_time": toS(vv.MaxTime), | ||
| 131 | "min_time": toS(vv.MinTime), | ||
| 132 | "avg_time": toS(time.Duration(int64(vv.TotalTime) / vv.RequestNum)), | ||
| 133 | } | ||
| 134 | resultLists = append(resultLists, result) | ||
| 135 | } | ||
| 136 | } | ||
| 137 | return json.Marshal(resultLists) | ||
| 138 | } | ||
| 139 | |||
| 114 | // global statistics data map | 140 | // global statistics data map |
| 115 | var StatisticsMap *UrlMap | 141 | var StatisticsMap *UrlMap |
| 116 | 142 | ... | ... |
| ... | @@ -28,4 +28,10 @@ func TestStatics(t *testing.T) { | ... | @@ -28,4 +28,10 @@ func TestStatics(t *testing.T) { |
| 28 | StatisticsMap.AddStatistics("POST", "/api/user/xiemengjun", "&admin.user", time.Duration(13000)) | 28 | StatisticsMap.AddStatistics("POST", "/api/user/xiemengjun", "&admin.user", time.Duration(13000)) |
| 29 | StatisticsMap.AddStatistics("DELETE", "/api/user", "&admin.user", time.Duration(1400)) | 29 | StatisticsMap.AddStatistics("DELETE", "/api/user", "&admin.user", time.Duration(1400)) |
| 30 | t.Log(StatisticsMap.GetMap()) | 30 | t.Log(StatisticsMap.GetMap()) |
| 31 | |||
| 32 | jsonString, err := StatisticsMap.GetMapJSON() | ||
| 33 | if err != nil { | ||
| 34 | t.Errorf(err.Error()) | ||
| 35 | } | ||
| 36 | t.Log(string(jsonString)) | ||
| 31 | } | 37 | } | ... | ... |
-
Please register or sign in to post a comment