Skip to content

Commit accf557

Browse files
committed
add benchmark for func getTopN
1 parent 7fcd14e commit accf557

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

Makefile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,7 @@ build:
33
go build -o bin/yaml-readme .
44
copy: build
55
cp bin/yaml-readme /usr/local/bin
6-
6+
test:
7+
go test ./...
8+
benchmark:
9+
go test -benchmem -run=^$$ -bench ^BenchmarkGetTopN$$ github.com/linuxsuren/yaml-readme/function

function/github_test.go

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -373,18 +373,22 @@ func TestGetTopN(t *testing.T) {
373373
count int
374374
expect map[string]int
375375
}{{
376-
name: "normal",
376+
name: "normal",
377+
values: items,
378+
count: 3,
379+
expect: map[string]int{
380+
"seven": 11,
381+
"six": 10,
382+
"five": 9,
383+
},
384+
}, {
385+
name: "less data",
377386
values: map[string]int{
378-
"linuxsuren": 1,
379-
"fake": 2,
380-
"test": 3,
381-
"rick": 4,
387+
"linuxsuren": 2,
382388
},
383-
count: 3,
389+
count: 2,
384390
expect: map[string]int{
385-
"rick": 4,
386-
"test": 3,
387-
"fake": 2,
391+
"linuxsuren": 2,
388392
},
389393
}}
390394
for _, tt := range tests {
@@ -394,3 +398,23 @@ func TestGetTopN(t *testing.T) {
394398
})
395399
}
396400
}
401+
402+
var items = map[string]int{
403+
"linuxsuren": 1,
404+
"fake": 2,
405+
"test": 3,
406+
"rick": 4,
407+
"one": 5,
408+
"two": 6,
409+
"three": 7,
410+
"four": 8,
411+
"five": 9,
412+
"six": 10,
413+
"seven": 11,
414+
}
415+
416+
func BenchmarkGetTopN(b *testing.B) {
417+
for i := 0; i < b.N; i++ {
418+
getTopN(items, 3)
419+
}
420+
}

main_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import (
66
"io/ioutil"
77
"reflect"
88
"testing"
9+
10+
"github.com/stretchr/testify/assert"
911
)
10-
import "github.com/stretchr/testify/assert"
1112

1213
func Test_sortBy(t *testing.T) {
1314
type args struct {
@@ -302,6 +303,7 @@ printGHTable
302303
printHelp
303304
printPages
304305
printStarHistory
306+
printStargazers
305307
printToc
306308
printVisitorCount
307309
render

0 commit comments

Comments
 (0)