From 4dd313fa182a8e58ca724686b5cdd685f2192652 Mon Sep 17 00:00:00 2001 From: Tracreed Date: Sat, 22 Dec 2018 22:24:23 +0100 Subject: [PATCH] Improved benchmarking --- search_test.go | 49 ++++++++++++++++++++++++++++++++++--------------- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/search_test.go b/search_test.go index fed084d..cfb9ed9 100644 --- a/search_test.go +++ b/search_test.go @@ -1,6 +1,7 @@ package search import ( + "fmt" "git.fuyu.moe/Fuyu/assert" "strings" "testing" @@ -10,20 +11,20 @@ func TestAdd(t *testing.T) { var DB Search as := assert.New(t) 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.Contains("another", DB.Words[0][1]) + as.Eq(`test`, DB.Words[0][0][0]) + as.Contains(`another`, DB.Words[0][1]) as.Eq(50, len(DB.Words)) } func TestMatch(t *testing.T) { var DB Search as := assert.New(t) - DB.Add(2, [][]string{[]string{"test"}, []string{"another", "test"}, []string{"third", "one"}}) - DB.Add(1, [][]string{[]string{"test"}, []string{"another", "test"}, []string{"third", "test"}}) - res := DB.Match("third") + DB.Add(2, [][]string{[]string{`test`}, []string{`another`, `test`}, []string{`third`, `one`}}) + DB.Add(1, [][]string{[]string{`test`}, []string{`another`, `test`}, []string{`third`, `test`}}) + res := DB.Match(`third`) as.Eq(2, res[0]) as.Eq(2, len(res)) } @@ -31,7 +32,7 @@ func TestMatch(t *testing.T) { func TestCleanSearch(t *testing.T) { var DB Search 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) { @@ -40,7 +41,7 @@ func TestIgnore(t *testing.T) { expectedIgnored := []string{`a`, `of`, `in`, `the`, `wa`, `ga`, `no`, `ni`, `wo`, `he`, `o`, `ka`} DB.Ignore(expectedIgnored) 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) { @@ -49,15 +50,33 @@ func TestReplace(t *testing.T) { expectedReplace := strings.Split(`〜☆♪・〈〉「」!『』²Ⅱ+[](),.!?\/{}+-_=~"'@#$%^&*|;:<>`, ``) DB.Replace(expectedReplace) 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) { - var DB Search - for i := 0; i < 10000; i++ { - DB.Add(i, [][]string{[]string{string(i)}, []string{"another", "test" + string(i)}, []string{"third", "test"}}) - } - for n := 0; n < b.N; n++ { - DB.Match(`5`) + for k := 1; k <= 1000000; k = k * 10 { + var DB Search + for i := 0; i < k; i++ { + 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++ { + 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`}}) + } + }) +}