Skip to content

Commit

Permalink
Merge pull request lotusdblabs#7 from SchopenhauerZhang/main
Browse files Browse the repository at this point in the history
add test code & format utils funtcion name
CocaineCong authored Oct 9, 2023
2 parents 4dd2157 + daca9c3 commit b7f787d
Showing 5 changed files with 37 additions and 18 deletions.
2 changes: 1 addition & 1 deletion searcher/engine.go
Original file line number Diff line number Diff line change
@@ -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{}
6 changes: 3 additions & 3 deletions searcher/sorts/sort.go
Original file line number Diff line number Diff line change
@@ -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 {
@@ -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++ {
8 changes: 3 additions & 5 deletions searcher/utils/utils.go
Original file line number Diff line number Diff line change
@@ -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 {
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
@@ -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)
@@ -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)
@@ -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) {

//})
@@ -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]
@@ -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 {
@@ -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 {

0 comments on commit b7f787d

Please sign in to comment.