Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test code & format utils funtcion name #3

Merged
merged 1 commit into from
Sep 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion searcher/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,7 @@ func (e *Engine) MultiSearch(request *model.SearchRequest) (*model.SearchResult,
Order: request.Order,
}

_time := utils.ExecTime(func() {
_time := utils.ExecTimeWithNanoseconds(func() {

base := len(words)
wg := &sync.WaitGroup{}
Expand Down
6 changes: 3 additions & 3 deletions searcher/sorts/sort.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ func (e *IdSort) GetAll(order string) []uint32 {
scores := make([]int, 0)
ids := make([]uint32, 0)
it := e.Tree.Iterator()
_tt := utils.ExecTime(func() {
_tt := utils.ExecTimeWithNanoseconds(func() {
for it.Next() {
scores = append(scores, it.Value().(int))
ids = append(ids, it.Key().(uint32))
}
})
log.Println("迭代耗时:", _tt)

_t := utils.ExecTime(func() {
_t := utils.ExecTimeWithNanoseconds(func() {
//ids 降序
if order == "desc" {
for i, j := 0, len(ids)-1; i < j; i, j = i+1, j-1 {
Expand All @@ -58,7 +58,7 @@ func (e *IdSort) GetAll(order string) []uint32 {
})
log.Println("id排序耗时:", _t)

_t = utils.ExecTime(func() {
_t = utils.ExecTimeWithNanoseconds(func() {
// 排序,得分越高 排越前
for i := 0; i < len(scores); i++ {
for j := i + 1; j < len(scores); j++ {
Expand Down
8 changes: 3 additions & 5 deletions searcher/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,16 @@ import (
"time"
)

func ExecTime(fn func()) float64 {
func ExecTimeWithNanoseconds(fn func()) float64 {
start := time.Now()
fn()
tc := float64(time.Since(start).Nanoseconds())
return tc / 1e6
return float64(time.Since(start).Nanoseconds()) / 1e6
}

func ExecTimeWithError(fn func() error) (float64, error) {
start := time.Now()
err := fn()
tc := float64(time.Since(start).Nanoseconds())
return tc / 1e6, err
return float64(time.Since(start).Nanoseconds()) / 1e6, err
}

func Encoder(data interface{}) []byte {
Expand Down
21 changes: 21 additions & 0 deletions searcher/utils/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package utils

import (
"fmt"
"github.com/go-playground/assert/v2"
"testing"
)

func TestExecTimeWithNanoseconds(t *testing.T) {
fmt.Println("TestExecTime ",ExecTimeWithNanoseconds(func() {
fmt.Println("handle function")
}))
}

func TestExecTimeWithError(t *testing.T){
_,err := ExecTimeWithError(func() error {
return fmt.Errorf("error handle function")
})
assert.Equal(t,fmt.Errorf("error handle function"),err)
}

18 changes: 9 additions & 9 deletions tests/sort_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ func TestSort(t *testing.T) {
var data4 = make([]int, len(data))
copy(data4, data)

_time := utils.ExecTime(func() {
_time := utils.ExecTimeWithNanoseconds(func() {
BubbleSort(data1)
})
fmt.Println("冒泡排序耗时:", _time)
//fmt.Println("data1", data1)
//快速排序

_time = utils.ExecTime(func() {
_time = utils.ExecTimeWithNanoseconds(func() {
//QuickSortAsc(data2, 0, len(data2)-1)
utils.QuickSortAsc(data2, 0, len(data2)-1, func(i int, j int) {
//log.Println(i, j)
Expand All @@ -50,13 +50,13 @@ func TestSort(t *testing.T) {
fmt.Println("快速排序耗时:", _time)
//fmt.Println("data2", data2)

_time = utils.ExecTime(func() {
_time = utils.ExecTimeWithNanoseconds(func() {
SelectSort(data3)
})
fmt.Println("选择排序耗时:", _time)
//fmt.Println("data3", data3)

_time = utils.ExecTime(func() {
_time = utils.ExecTimeWithNanoseconds(func() {
InsertSort(data4)
})
fmt.Println("插入排序耗时:", _time)
Expand Down Expand Up @@ -152,7 +152,7 @@ func TestFastSort(t *testing.T) {

}

_time := utils.ExecTime(func() {
_time := utils.ExecTimeWithNanoseconds(func() {
//utils.QuickSortDesc(data, 0, len(data)-1, func(i int, j int) {

//})
Expand Down Expand Up @@ -229,13 +229,13 @@ func TestFind(t *testing.T) {
data2 = append(data2, val)
}

t1 := utils.ExecTime(func() {
t1 := utils.ExecTimeWithNanoseconds(func() {
sort.Sort(sort.IntSlice(data))
})
fmt.Println("快排用时", t1)

//fmt.Println(find(data, 1))
t2 := utils.ExecTime(func() {
t2 := utils.ExecTimeWithNanoseconds(func() {
BucketSort(data2)
for i, j := 0, len(data2)-1; i < j; i, j = i+1, j-1 {
data2[i], data2[j] = data2[j], data2[i]
Expand Down Expand Up @@ -272,7 +272,7 @@ func TestMerge(t *testing.T) {
data2 = append(data2, uint32(v))
}

t1 := utils.ExecTime(func() {
t1 := utils.ExecTimeWithNanoseconds(func() {
temp := make([]uint32, 0)
for _, v := range data1 {
if found, _ := find(temp, v); found {
Expand All @@ -284,7 +284,7 @@ func TestMerge(t *testing.T) {

fmt.Println("二分法去重", t1)

t2 := utils.ExecTime(func() {
t2 := utils.ExecTimeWithNanoseconds(func() {
temp := make(map[uint32]bool, len(data2))
d := make([]uint32, 0)
for _, val := range data2 {
Expand Down