Improved benchmarking

This commit is contained in:
David Alasow 2018-12-22 22:24:23 +01:00
parent 25124060dd
commit 4dd313fa18

View File

@ -1,6 +1,7 @@
package search package search
import ( import (
"fmt"
"git.fuyu.moe/Fuyu/assert" "git.fuyu.moe/Fuyu/assert"
"strings" "strings"
"testing" "testing"
@ -10,20 +11,20 @@ func TestAdd(t *testing.T) {
var DB Search var DB Search
as := assert.New(t) as := assert.New(t)
for i := 0; i < 50; i++ { for i := 0; i < 50; i++ {
DB.Add(i, [][]string{[]string{"test"}, []string{"another", "test"}, []string{"third", "one"}}) DB.Add(i, [][]string{[]string{`test`}, []string{`another`, `test`}, []string{`third`, `one`}})
} }
as.Eq("test", DB.Words[0][0][0]) as.Eq(`test`, DB.Words[0][0][0])
as.Contains("another", DB.Words[0][1]) as.Contains(`another`, DB.Words[0][1])
as.Eq(50, len(DB.Words)) as.Eq(50, len(DB.Words))
} }
func TestMatch(t *testing.T) { func TestMatch(t *testing.T) {
var DB Search var DB Search
as := assert.New(t) as := assert.New(t)
DB.Add(2, [][]string{[]string{"test"}, []string{"another", "test"}, []string{"third", "one"}}) DB.Add(2, [][]string{[]string{`test`}, []string{`another`, `test`}, []string{`third`, `one`}})
DB.Add(1, [][]string{[]string{"test"}, []string{"another", "test"}, []string{"third", "test"}}) DB.Add(1, [][]string{[]string{`test`}, []string{`another`, `test`}, []string{`third`, `test`}})
res := DB.Match("third") res := DB.Match(`third`)
as.Eq(2, res[0]) as.Eq(2, res[0])
as.Eq(2, len(res)) as.Eq(2, len(res))
} }
@ -31,7 +32,7 @@ func TestMatch(t *testing.T) {
func TestCleanSearch(t *testing.T) { func TestCleanSearch(t *testing.T) {
var DB Search var DB Search
as := assert.New(t) as := assert.New(t)
as.SameElements(DB.CleanSearch("Title of a anime in here"), []string{`title`, `of`, `a`, `anime`, `in`, `here`}) as.SameElements(DB.CleanSearch(`Title of a anime in here`), []string{`title`, `of`, `a`, `anime`, `in`, `here`})
} }
func TestIgnore(t *testing.T) { func TestIgnore(t *testing.T) {
@ -40,7 +41,7 @@ func TestIgnore(t *testing.T) {
expectedIgnored := []string{`a`, `of`, `in`, `the`, `wa`, `ga`, `no`, `ni`, `wo`, `he`, `o`, `ka`} expectedIgnored := []string{`a`, `of`, `in`, `the`, `wa`, `ga`, `no`, `ni`, `wo`, `he`, `o`, `ka`}
DB.Ignore(expectedIgnored) DB.Ignore(expectedIgnored)
as.SameElements(expectedIgnored, DB.IgnoreWords) as.SameElements(expectedIgnored, DB.IgnoreWords)
as.SameElements(DB.CleanSearch("Title of a anime in here"), []string{`title`, `anime`, `here`}) as.SameElements(DB.CleanSearch(`Title of a anime in here`), []string{`title`, `anime`, `here`})
} }
func TestReplace(t *testing.T) { func TestReplace(t *testing.T) {
@ -49,15 +50,33 @@ func TestReplace(t *testing.T) {
expectedReplace := strings.Split(`〜☆♪・〈〉「」!『』²Ⅱ+[](),.!?\/{}+-_=~"'@#$%^&*|;:<>`, ``) expectedReplace := strings.Split(`〜☆♪・〈〉「」!『』²Ⅱ+[](),.!?\/{}+-_=~"'@#$%^&*|;:<>`, ``)
DB.Replace(expectedReplace) DB.Replace(expectedReplace)
as.SameElements(expectedReplace, DB.RemoveCharacters) as.SameElements(expectedReplace, DB.RemoveCharacters)
as.SameElements(DB.CleanSearch("Ti☆tle〜 of☆a anime² in♪ 「here」"), []string{`ti`, `tle`, `of`, `a`, `anime`, `in`, `here`}) as.SameElements(DB.CleanSearch(`Ti☆tle〜 of☆a anime² in♪ 「here」`), []string{`ti`, `tle`, `of`, `a`, `anime`, `in`, `here`})
} }
func BenchmarkMatch(b *testing.B) { func BenchmarkMatch(b *testing.B) {
for k := 1; k <= 1000000; k = k * 10 {
var DB Search var DB Search
for i := 0; i < 10000; i++ { for i := 0; i < k; i++ {
DB.Add(i, [][]string{[]string{string(i)}, []string{"another", "test" + string(i)}, []string{"third", "test"}}) DB.Add(i, [][]string{[]string{string(i)}, []string{`another`, `test` + string(i)}, []string{`third`, `test`}})
} }
b.Run(fmt.Sprintf(`s-%d`, k), func(b *testing.B) {
for n := 0; n < b.N; n++ { for n := 0; n < b.N; n++ {
DB.Match(`5`) DB.Match(`5`)
} }
})
b.Run(fmt.Sprintf(`l-%d`, k), func(b *testing.B) {
for n := 0; n < b.N; n++ {
DB.Match(`A test search`)
}
})
}
}
func BenchmarkAdd(b *testing.B) {
var DB Search
b.Run(`Adding`, func(b *testing.B) {
for i := 0; i < b.N; i++ {
DB.Add(i, [][]string{[]string{string(i)}, []string{`another`, `test` + string(i)}, []string{`third`, `test`}})
}
})
} }