@@ -15,53 +15,174 @@ func MarkdownTableByCounts(counts []*Count) (content string) {
15
15
type Options struct {
16
16
AddHtmlFormat bool
17
17
WarnUseTime int64 // 超过该时间 出现警告 为毫秒数
18
+ Columns []* TableColumn
19
+ }
20
+
21
+ type TableColumn struct {
22
+ Label string `json:"label"`
23
+ GetCol func (count * Count , options * Options ) (res string )
24
+ }
25
+
26
+ var (
27
+ TableColumnName = & TableColumn {
28
+ Label : "名称" ,
29
+ GetCol : func (count * Count , options * Options ) (res string ) {
30
+ res = count .Name
31
+ return
32
+ },
33
+ }
34
+ TableColumnTime = & TableColumn {
35
+ Label : "任务时间" ,
36
+ GetCol : func (count * Count , options * Options ) (res string ) {
37
+ if options .AddHtmlFormat {
38
+ res = " %s <br>-<br> %s |"
39
+ } else {
40
+ res = " %s - %s |"
41
+ }
42
+ res = fmt .Sprintf (res ,
43
+ util .TimeFormat (time .UnixMilli (count .StartTime / int64 (time .Millisecond )), "2006-01-02 15:04:05.000" ),
44
+ util .TimeFormat (time .UnixMilli (count .EndTime / int64 (time .Millisecond )), "2006-01-02 15:04:05.000" ),
45
+ )
46
+ return
47
+ },
48
+ }
49
+ TableColumnCount = & TableColumn {
50
+ Label : "总/成功/失败" ,
51
+ GetCol : func (count * Count , options * Options ) (res string ) {
52
+ if options .AddHtmlFormat {
53
+ res = " %d <br> <font color='green'>%d</font> <br> <font color='red'>%d</font> |"
54
+ } else {
55
+ res = " %d / %d / %d |"
56
+ }
57
+ res = fmt .Sprintf (res , count .Count , count .SuccessCount , count .ErrorCount )
58
+ return
59
+ },
60
+ }
61
+ TableColumnTotalTime = & TableColumn {
62
+ Label : "任务用时" ,
63
+ GetCol : func (count * Count , options * Options ) (res string ) {
64
+ res = ToTimeStr (count .TotalTime / 1000000 )
65
+ return
66
+ },
67
+ }
68
+ TableColumnExecuteTime = & TableColumn {
69
+ Label : "执行用时" ,
70
+ GetCol : func (count * Count , options * Options ) (res string ) {
71
+ res = ToTimeStr (count .ExecuteTime / 1000000 )
72
+ return
73
+ },
74
+ }
75
+ TableColumnUseTime = & TableColumn {
76
+ Label : "累计用时" ,
77
+ GetCol : func (count * Count , options * Options ) (res string ) {
78
+ res = ToTimeStr (count .UseTime / 1000000 )
79
+ return
80
+ },
81
+ }
82
+ TableColumnTps = & TableColumn {
83
+ Label : "TPS" ,
84
+ GetCol : func (count * Count , options * Options ) (res string ) {
85
+ res = count .Tps
86
+ return
87
+ },
88
+ }
89
+ TableColumnAvg = & TableColumn {
90
+ Label : "Avg" ,
91
+ GetCol : func (count * Count , options * Options ) (res string ) {
92
+ res = GetTableTimeOut (count .AvgValue , count .Avg , options )
93
+ return
94
+ },
95
+ }
96
+ TableColumnMin = & TableColumn {
97
+ Label : "Min" ,
98
+ GetCol : func (count * Count , options * Options ) (res string ) {
99
+ res = GetTableTimeOut (float64 (count .MinUseTime )/ float64 (time .Millisecond ), count .Min , options )
100
+ return
101
+ },
102
+ }
103
+ TableColumnMax = & TableColumn {
104
+ Label : "Max" ,
105
+ GetCol : func (count * Count , options * Options ) (res string ) {
106
+ res = GetTableTimeOut (float64 (count .MaxUseTime )/ float64 (time .Millisecond ), count .Max , options )
107
+ return
108
+ },
109
+ }
110
+ TableColumnT50 = & TableColumn {
111
+ Label : "T50" ,
112
+ GetCol : func (count * Count , options * Options ) (res string ) {
113
+ res = GetTableTimeOut (util .StringToFloat64 (count .T50 ), count .T50 , options )
114
+ return
115
+ },
116
+ }
117
+ TableColumnT80 = & TableColumn {
118
+ Label : "T80" ,
119
+ GetCol : func (count * Count , options * Options ) (res string ) {
120
+ res = GetTableTimeOut (util .StringToFloat64 (count .T80 ), count .T80 , options )
121
+ return
122
+ },
123
+ }
124
+ TableColumnT90 = & TableColumn {
125
+ Label : "T90" ,
126
+ GetCol : func (count * Count , options * Options ) (res string ) {
127
+ res = GetTableTimeOut (util .StringToFloat64 (count .T90 ), count .T90 , options )
128
+ return
129
+ },
130
+ }
131
+ TableColumnT99 = & TableColumn {
132
+ Label : "T99" ,
133
+ GetCol : func (count * Count , options * Options ) (res string ) {
134
+ res = GetTableTimeOut (util .StringToFloat64 (count .T99 ), count .T99 , options )
135
+ return
136
+ },
137
+ }
138
+ defaultColumns = []* TableColumn {TableColumnTime ,
139
+ TableColumnCount ,
140
+ TableColumnTotalTime ,
141
+ TableColumnTps ,
142
+ TableColumnAvg ,
143
+ TableColumnMin ,
144
+ TableColumnMax ,
145
+ TableColumnT50 ,
146
+ TableColumnT80 ,
147
+ TableColumnT90 ,
148
+ TableColumnT99 ,
149
+ }
150
+ )
151
+
152
+ func GetTableTimeOut (v float64 , s string , options * Options ) string {
153
+ if options .AddHtmlFormat && options .WarnUseTime > 0 && int64 (v ) >= options .WarnUseTime {
154
+ return fmt .Sprintf ("<font color='red'>%s</font>" , s )
155
+ }
156
+ return s
18
157
}
19
158
20
159
func MarkdownTable (counts []* Count , options * Options ) (content string ) {
21
160
if options == nil {
22
161
options = & Options {}
23
162
}
24
- content += fmt .Sprintf ("| 任务时间 | 总/成功/失败 | 任务用时| 执行用时 | 累计用时 |TPS |Avg |Min |Max |T50 |T80 | T90 | T99 | \n " )
25
- content += fmt .Sprintf ("| :------: | :------: | :------: | :------: | :------: |:------: | :------: | :------: | :------: | :------: | :------: | :------: | :------: | \n " )
26
-
27
- getTimeOut := func (v float64 , s string ) string {
28
- if options .AddHtmlFormat && options .WarnUseTime > 0 && int64 (v ) >= options .WarnUseTime {
29
- return fmt .Sprintf ("<font color='red'>%s</font>" , s )
30
- }
31
- return s
163
+ columns := options .Columns
164
+ if len (columns ) == 0 {
165
+ columns = defaultColumns
32
166
}
33
- var s string
34
- for _ , count := range counts {
167
+ content += "|"
168
+ for _ , column := range columns {
169
+ content += " " + column .Label + " |"
170
+ }
171
+ content += "\n "
172
+ content += "|"
173
+ for _ , _ = range columns {
174
+ content += " :------: |"
175
+ }
176
+ content += "\n "
35
177
36
- content += fmt .Sprintf ("|" )
37
- if options .AddHtmlFormat {
38
- s = " %s <br>-<br> %s |"
39
- } else {
40
- s = " %s - %s |"
41
- }
42
- content += fmt .Sprintf (s ,
43
- util .TimeFormat (time .UnixMilli (count .StartTime / int64 (time .Millisecond )), "2006-01-02 15:04:05.000" ),
44
- util .TimeFormat (time .UnixMilli (count .EndTime / int64 (time .Millisecond )), "2006-01-02 15:04:05.000" ),
45
- )
46
- if options .AddHtmlFormat {
47
- s = " %d <br> <font color='green'>%d</font> <br> <font color='red'>%d</font> |"
48
- } else {
49
- s = " %d / %d / %d |"
178
+ for _ , count := range counts {
179
+ content += "|"
180
+ for _ , column := range columns {
181
+ content += " " + column .GetCol (count , options ) + " |"
50
182
}
51
- content += fmt .Sprintf (s , count .Count , count .SuccessCount , count .ErrorCount )
52
- content += fmt .Sprintf (" %s |" , ToTimeStr (count .TotalTime / 1000000 ))
53
- content += fmt .Sprintf (" %s |" , ToTimeStr (count .ExecuteTime / 1000000 ))
54
- content += fmt .Sprintf (" %s |" , ToTimeStr (count .UseTime / 1000000 ))
55
- content += fmt .Sprintf (" %s |" , count .Tps )
56
- content += fmt .Sprintf (" %s |" , getTimeOut (float64 (count .AvgValue ), count .Avg ))
57
- content += fmt .Sprintf (" %s |" , getTimeOut (float64 (count .MinUseTime )/ float64 (time .Millisecond ), count .Min ))
58
- content += fmt .Sprintf (" %s |" , getTimeOut (float64 (count .MaxUseTime )/ float64 (time .Millisecond ), count .Max ))
59
- content += fmt .Sprintf (" %s |" , getTimeOut (util .StringToFloat64 (count .T50 ), count .T50 ))
60
- content += fmt .Sprintf (" %s |" , getTimeOut (util .StringToFloat64 (count .T80 ), count .T80 ))
61
- content += fmt .Sprintf (" %s |" , getTimeOut (util .StringToFloat64 (count .T90 ), count .T90 ))
62
- content += fmt .Sprintf (" %s |" , getTimeOut (util .StringToFloat64 (count .T99 ), count .T99 ))
63
- content += fmt .Sprintf ("\n " )
183
+ content += "\n "
64
184
}
185
+
65
186
return
66
187
}
67
188
0 commit comments