11package timing
22
33import (
4- "io"
54 "os"
65 "strings"
76 "sync"
@@ -21,14 +20,14 @@ func TestNewStats(t *testing.T) {
2120func TestRecord (t * testing.T ) {
2221 s := NewStats ()
2322
24- s .Record (CategoryHTTP , OperationTypeFetch , "test-op" , 100 * time .Millisecond , true , time .Now ())
23+ s .Record (CategoryHTTP , OperationTypeAPIFetch , "test-op" , 100 * time .Millisecond , true , time .Now ())
2524
2625 assert .Equal (t , 1 , s .TotalOperations ())
2726 assert .Equal (t , 100 * time .Millisecond , s .TotalTime ())
2827
2928 ops := s .GetCategoryStats (CategoryHTTP )
3029 assert .Len (t , ops , 1 )
31- assert .Equal (t , OperationTypeFetch , ops [0 ].Type )
30+ assert .Equal (t , OperationTypeAPIFetch , ops [0 ].Type )
3231 assert .Equal (t , "test-op" , ops [0 ].Name )
3332 assert .Equal (t , 100 * time .Millisecond , ops [0 ].Duration )
3433 assert .True (t , ops [0 ].Success )
@@ -37,9 +36,9 @@ func TestRecord(t *testing.T) {
3736func TestRecordMultipleCategories (t * testing.T ) {
3837 s := NewStats ()
3938
40- s .Record (CategoryHTTP , OperationTypeFetch , "http-op" , 50 * time .Millisecond , true , time .Now ())
41- s .Record (CategoryGit , OperationTypeClone , "git-op" , 100 * time .Millisecond , true , time .Now ())
42- s .Record (CategoryHelm , OperationTypeDownload , "helm-op" , 75 * time .Millisecond , true , time .Now ())
39+ s .Record (CategoryHTTP , OperationTypeAPIFetch , "http-op" , 50 * time .Millisecond , true , time .Now ())
40+ s .Record (CategoryGit , OperationTypeWrite , "git-op" , 100 * time .Millisecond , true , time .Now ())
41+ s .Record (CategoryHelm , OperationTypeGenerate , "helm-op" , 75 * time .Millisecond , true , time .Now ())
4342
4443 assert .Equal (t , 3 , s .TotalOperations ())
4544 assert .Equal (t , 225 * time .Millisecond , s .TotalTime ())
@@ -52,9 +51,9 @@ func TestRecordMultipleCategories(t *testing.T) {
5251func TestRecordMultipleOperationsSameCategory (t * testing.T ) {
5352 s := NewStats ()
5453
55- s .Record (CategoryHTTP , OperationTypeFetch , "op1" , 10 * time .Millisecond , true , time .Now ())
56- s .Record (CategoryHTTP , OperationTypeFetch , "op2" , 20 * time .Millisecond , true , time .Now ())
57- s .Record (CategoryHTTP , OperationTypeFetch , "op3" , 30 * time .Millisecond , true , time .Now ())
54+ s .Record (CategoryHTTP , OperationTypeAPIFetch , "op1" , 10 * time .Millisecond , true , time .Now ())
55+ s .Record (CategoryHTTP , OperationTypeAPIFetch , "op2" , 20 * time .Millisecond , true , time .Now ())
56+ s .Record (CategoryHTTP , OperationTypeAPIFetch , "op3" , 30 * time .Millisecond , true , time .Now ())
5857
5958 ops := s .GetCategoryStats (CategoryHTTP )
6059 assert .Len (t , ops , 3 )
@@ -63,7 +62,7 @@ func TestRecordMultipleOperationsSameCategory(t *testing.T) {
6362func TestRecordFailure (t * testing.T ) {
6463 s := NewStats ()
6564
66- s .Record (CategoryHTTP , OperationTypeFetch , "failed-op" , 100 * time .Millisecond , false , time .Now ())
65+ s .Record (CategoryHTTP , OperationTypeAPIFetch , "failed-op" , 100 * time .Millisecond , false , time .Now ())
6766
6867 ops := s .GetCategoryStats (CategoryHTTP )
6968 assert .Len (t , ops , 1 )
@@ -79,7 +78,7 @@ func TestGetCategoryStatsEmpty(t *testing.T) {
7978
8079func TestGetCategoryStatsUnknownCategory (t * testing.T ) {
8180 s := NewStats ()
82- s .Record (CategoryHTTP , OperationTypeFetch , "test" , 100 * time .Millisecond , true , time .Now ())
81+ s .Record (CategoryHTTP , OperationTypeAPIFetch , "test" , 100 * time .Millisecond , true , time .Now ())
8382
8483 ops := s .GetCategoryStats (CategoryOCI )
8584 assert .Nil (t , ops )
@@ -154,43 +153,63 @@ func TestFormatDuration(t *testing.T) {
154153func TestPrintSummaryEmpty (t * testing.T ) {
155154 s := NewStats ()
156155
157- s .PrintSummary (io .Discard )
156+ var buf strings.Builder
157+ s .PrintSummary (& buf )
158+
159+ assert .Contains (t , buf .String (), "### Statistics" )
160+ assert .Contains (t , buf .String (), "**Overall:** 0µs (0 operations)" )
158161}
159162
160163func TestPrintSummaryWithData (t * testing.T ) {
161164 s := NewStats ()
162165
163- s .Record (CategoryHTTP , OperationTypeFetch , "op1" , 10 * time .Millisecond , true , time .Now ())
164- s .Record (CategoryHTTP , OperationTypeFetch , "op2" , 20 * time .Millisecond , true , time .Now ())
165- s .Record (CategoryHTTP , OperationTypeFetch , "op3" , 30 * time .Millisecond , true , time .Now ())
166- s .Record (CategoryGit , OperationTypeClone , "repo1" , 100 * time .Millisecond , true , time .Now ())
167- s .Record (CategoryGit , OperationTypeClone , "repo2" , 200 * time .Millisecond , true , time .Now ())
166+ s .Record (CategoryHTTP , OperationTypeAPIFetch , "op1" , 10 * time .Millisecond , true , time .Now ())
167+ s .Record (CategoryHTTP , OperationTypeAPIFetch , "op2" , 20 * time .Millisecond , true , time .Now ())
168+ s .Record (CategoryHTTP , OperationTypeAPIFetch , "op3" , 30 * time .Millisecond , true , time .Now ())
169+ s .Record (CategoryGit , OperationTypeGenerate , "repo1" , 100 * time .Millisecond , true , time .Now ())
170+ s .Record (CategoryGit , OperationTypeGenerate , "repo2" , 200 * time .Millisecond , true , time .Now ())
171+
172+ var buf strings.Builder
173+ s .PrintSummary (& buf )
168174
169- s .PrintSummary (io .Discard )
175+ assert .Contains (t , buf .String (), "### Statistics" )
176+ assert .Contains (t , buf .String (), "**Overall:** 360ms (5 operations)" )
177+ assert .Contains (t , buf .String (), "#### Http" )
178+ assert .Contains (t , buf .String (), "#### Git" )
170179}
171180
172181func TestPrintSummaryAllCategories (t * testing.T ) {
173182 s := NewStats ()
174183
175- s .Record (CategoryHTTP , OperationTypeFetch , "http-op" , 10 * time .Millisecond , true , time .Now ())
176- s .Record (CategoryGit , OperationTypeClone , "git-op" , 20 * time .Millisecond , true , time .Now ())
177- s .Record (CategoryHelm , OperationTypeDownload , "helm-op" , 30 * time .Millisecond , true , time .Now ())
178- s .Record (CategoryOCI , OperationTypePull , "oci-op" , 40 * time .Millisecond , true , time .Now ())
184+ s .Record (CategoryHTTP , OperationTypeAPIFetch , "http-op" , 10 * time .Millisecond , true , time .Now ())
185+ s .Record (CategoryGit , OperationTypeGenerate , "git-op" , 20 * time .Millisecond , true , time .Now ())
186+ s .Record (CategoryHelm , OperationTypeGenerate , "helm-op" , 30 * time .Millisecond , true , time .Now ())
187+ s .Record (CategoryOCI , OperationTypeGenerate , "oci-op" , 40 * time .Millisecond , true , time .Now ())
179188 s .Record (CategoryGeneration , OperationTypeWrite , "gen-op" , 50 * time .Millisecond , true , time .Now ())
180189 s .Record (CategoryMisc , OperationTypeUpdate , "misc-op" , 60 * time .Millisecond , true , time .Now ())
181190
182- s .PrintSummary (io .Discard )
191+ var buf strings.Builder
192+ s .PrintSummary (& buf )
193+
194+ assert .Contains (t , buf .String (), "### Statistics" )
195+ assert .Contains (t , buf .String (), "**Overall:** 210ms (6 operations)" )
196+ assert .Contains (t , buf .String (), "#### Http" )
197+ assert .Contains (t , buf .String (), "#### Git" )
198+ assert .Contains (t , buf .String (), "#### Helm" )
199+ assert .Contains (t , buf .String (), "#### Oci" )
200+ assert .Contains (t , buf .String (), "#### Generation" )
201+ assert .Contains (t , buf .String (), "#### Misc" )
183202}
184203
185204func TestConcurrentRecording (t * testing.T ) {
186205 s := NewStats ()
187206
188207 var wg sync.WaitGroup
189- for i := 0 ; i < 100 ; i ++ {
208+ for i := range 100 {
190209 wg .Add (1 )
191210 go func (n int ) {
192211 defer wg .Done ()
193- s .Record (CategoryHTTP , OperationTypeFetch , "op" , time .Duration (n )* time .Millisecond , true , time .Now ())
212+ s .Record (CategoryHTTP , OperationTypeAPIFetch , "op" , time .Duration (n )* time .Millisecond , true , time .Now ())
194213 }(i )
195214 }
196215 wg .Wait ()
@@ -228,7 +247,7 @@ func TestOpenLogFileThenRecord(t *testing.T) {
228247 err := s .OpenLogFile (tmpFile )
229248 assert .NoError (t , err )
230249
231- s .Record (CategoryHTTP , OperationTypeFetch , "test-op" , 100 * time .Millisecond , true , time .Now ())
250+ s .Record (CategoryHTTP , OperationTypeAPIFetch , "test-op" , 100 * time .Millisecond , true , time .Now ())
232251 s .CloseLogFile ()
233252
234253 content , err := os .ReadFile (tmpFile )
@@ -248,12 +267,12 @@ func TestOpenLogFileInvalidPath(t *testing.T) {
248267func TestPrintSummaryWriter (t * testing.T ) {
249268 s := NewStats ()
250269
251- s .Record (CategoryHTTP , OperationTypeFetch , "op1" , 10 * time .Millisecond , true , time .Now ())
270+ s .Record (CategoryHTTP , OperationTypeAPIFetch , "op1" , 10 * time .Millisecond , true , time .Now ())
252271
253272 var buf strings.Builder
254273 s .PrintSummary (& buf )
255274
256- assert .Contains (t , buf .String (), "Update Statistics" )
275+ assert .Contains (t , buf .String (), "### Statistics" )
257276 assert .Contains (t , buf .String (), "**Overall:**" )
258277 assert .Contains (t , buf .String (), "#### Http" )
259278}
0 commit comments