From b601cfa2a56f6855cee801398712978ec97bdf91 Mon Sep 17 00:00:00 2001 From: "s.kamardin" Date: Mon, 18 Jan 2016 13:07:28 +0300 Subject: [PATCH] add bencharks for indexing --- match/any.go | 2 ++ match/any_test.go | 7 +++++ match/contains_test.go | 7 +++++ match/list_test.go | 7 +++++ match/match_test.go | 3 +++ match/max_test.go | 7 +++++ match/min_test.go | 7 +++++ match/nothing_test.go | 41 ++++++++++++++++++++++++++++ match/prefix_suffix_test.go | 7 +++++ match/prefix_test.go | 7 +++++ match/range_test.go | 54 +++++++++++++++++++++++++++++++++++++ match/row_test.go | 51 +++++++++++++++++++++++++++++++++++ match/single_test.go | 44 ++++++++++++++++++++++++++++++ match/suffix_test.go | 7 +++++ match/super_test.go | 41 ++++++++++++++++++++++++++++ match/text_test.go | 44 ++++++++++++++++++++++++++++++ 16 files changed, 336 insertions(+) create mode 100644 match/nothing_test.go create mode 100644 match/range_test.go create mode 100644 match/row_test.go create mode 100644 match/single_test.go create mode 100644 match/super_test.go create mode 100644 match/text_test.go diff --git a/match/any.go b/match/any.go index a1885c5..6470c62 100644 --- a/match/any.go +++ b/match/any.go @@ -21,6 +21,8 @@ func (self Any) Index(s string) (int, []int) { switch found { case -1: sub = s + case 0: + return 0, []int{0} default: sub = s[:found] } diff --git a/match/any_test.go b/match/any_test.go index e526b83..c436267 100644 --- a/match/any_test.go +++ b/match/any_test.go @@ -35,3 +35,10 @@ func TestAnyIndex(t *testing.T) { } } } + +func BenchmarkIndexAny(b *testing.B) { + p := Any{bench_separators} + for i := 0; i < b.N; i++ { + p.Index(bench_pattern) + } +} diff --git a/match/contains_test.go b/match/contains_test.go index e45d3e0..b7e66c7 100644 --- a/match/contains_test.go +++ b/match/contains_test.go @@ -52,3 +52,10 @@ func TestContainsIndex(t *testing.T) { } } } + +func BenchmarkIndexContains(b *testing.B) { + m := Contains{bench_separators, true} + for i := 0; i < b.N; i++ { + m.Index(bench_pattern) + } +} diff --git a/match/list_test.go b/match/list_test.go index c985ebc..a772fdf 100644 --- a/match/list_test.go +++ b/match/list_test.go @@ -38,3 +38,10 @@ func TestListIndex(t *testing.T) { } } } + +func BenchmarkIndexList(b *testing.B) { + m := List{"def", false} + for i := 0; i < b.N; i++ { + m.Index(bench_pattern) + } +} diff --git a/match/match_test.go b/match/match_test.go index e92776a..c3b2985 100644 --- a/match/match_test.go +++ b/match/match_test.go @@ -5,6 +5,9 @@ import ( "testing" ) +const bench_separators = "." +const bench_pattern = "abcdefghijklmnopqrstuvwxyz0123456789" + func TestMergeSegments(t *testing.T) { for id, test := range []struct { segments [][]int diff --git a/match/max_test.go b/match/max_test.go index d1ab803..bbd6de0 100644 --- a/match/max_test.go +++ b/match/max_test.go @@ -35,3 +35,10 @@ func TestMaxIndex(t *testing.T) { } } } + +func BenchmarkIndexMax(b *testing.B) { + m := Max{10} + for i := 0; i < b.N; i++ { + m.Index(bench_pattern) + } +} diff --git a/match/min_test.go b/match/min_test.go index c8aa794..c823223 100644 --- a/match/min_test.go +++ b/match/min_test.go @@ -35,3 +35,10 @@ func TestMinIndex(t *testing.T) { } } } + +func BenchmarkIndexMin(b *testing.B) { + m := Min{10} + for i := 0; i < b.N; i++ { + m.Index(bench_pattern) + } +} diff --git a/match/nothing_test.go b/match/nothing_test.go new file mode 100644 index 0000000..1b96c58 --- /dev/null +++ b/match/nothing_test.go @@ -0,0 +1,41 @@ +package match + +import ( + "reflect" + "testing" +) + +func TestNothingIndex(t *testing.T) { + for id, test := range []struct { + fixture string + index int + segments []int + }{ + { + "abc", + 0, + []int{0}, + }, + { + "", + 0, + []int{0}, + }, + } { + p := Nothing{} + index, segments := p.Index(test.fixture) + if index != test.index { + t.Errorf("#%d unexpected index: exp: %d, act: %d", id, test.index, index) + } + if !reflect.DeepEqual(segments, test.segments) { + t.Errorf("#%d unexpected segments: exp: %v, act: %v", id, test.segments, segments) + } + } +} + +func BenchmarkIndexNothing(b *testing.B) { + m := Max{10} + for i := 0; i < b.N; i++ { + m.Index(bench_pattern) + } +} diff --git a/match/prefix_suffix_test.go b/match/prefix_suffix_test.go index ab65096..baf9427 100644 --- a/match/prefix_suffix_test.go +++ b/match/prefix_suffix_test.go @@ -45,3 +45,10 @@ func TestPrefixSuffixIndex(t *testing.T) { } } } + +func BenchmarkIndexPrefixSuffix(b *testing.B) { + m := PrefixSuffix{"qew", "sqw"} + for i := 0; i < b.N; i++ { + m.Index(bench_pattern) + } +} diff --git a/match/prefix_test.go b/match/prefix_test.go index 8f93f97..3ee3012 100644 --- a/match/prefix_test.go +++ b/match/prefix_test.go @@ -35,3 +35,10 @@ func TestPrefixIndex(t *testing.T) { } } } + +func BenchmarkIndexPrefix(b *testing.B) { + m := Prefix{"qew"} + for i := 0; i < b.N; i++ { + m.Index(bench_pattern) + } +} diff --git a/match/range_test.go b/match/range_test.go new file mode 100644 index 0000000..e55bccd --- /dev/null +++ b/match/range_test.go @@ -0,0 +1,54 @@ +package match + +import ( + "reflect" + "testing" +) + +func TestRangeIndex(t *testing.T) { + for id, test := range []struct { + lo, hi rune + not bool + fixture string + index int + segments []int + }{ + { + 'a', 'z', + false, + "abc", + 0, + []int{1}, + }, + { + 'a', 'c', + false, + "abcd", + 0, + []int{1}, + }, + { + 'a', 'c', + true, + "abcd", + 3, + []int{1}, + }, + } { + m := Range{test.lo, test.hi, test.not} + index, segments := m.Index(test.fixture) + if index != test.index { + t.Errorf("#%d unexpected index: exp: %d, act: %d", id, test.index, index) + } + if !reflect.DeepEqual(segments, test.segments) { + t.Errorf("#%d unexpected segments: exp: %v, act: %v", id, test.segments, segments) + } + } +} + +func BenchmarkIndexRange(b *testing.B) { + m := Range{'0', '9', false} + for i := 0; i < b.N; i++ { + m.Index(bench_pattern) + } +} diff --git a/match/row_test.go b/match/row_test.go new file mode 100644 index 0000000..c9422c4 --- /dev/null +++ b/match/row_test.go @@ -0,0 +1,51 @@ +package match + +import ( + "reflect" + "testing" +) + +func TestRowIndex(t *testing.T) { + for id, test := range []struct { + matchers Matchers + length int + fixture string + index int + segments []int + }{ + { + Matchers{ + NewText("abc"), + NewText("def"), + Single{}, + }, + 7, + "qweabcdefghij", + 3, + []int{7}, + }, + { + Matchers{ + NewText("abc"), + NewText("def"), + Single{}, + }, + 7, + "abcd", + -1, + nil, + }, + } { + p := Row{ + Matchers: test.matchers, + RunesLength: test.length, + } + index, segments := p.Index(test.fixture) + if index != test.index { + t.Errorf("#%d unexpected index: exp: %d, act: %d", id, test.index, index) + } + if !reflect.DeepEqual(segments, test.segments) { + t.Errorf("#%d unexpected segments: exp: %v, act: %v", id, test.segments, segments) + } + } +} diff --git a/match/single_test.go b/match/single_test.go new file mode 100644 index 0000000..1e9ba71 --- /dev/null +++ b/match/single_test.go @@ -0,0 +1,44 @@ +package match + +import ( + "reflect" + "testing" +) + +func TestSingleIndex(t *testing.T) { + for id, test := range []struct { + separators string + fixture string + index int + segments []int + }{ + { + ".", + ".abc", + 1, + []int{1}, + }, + { + ".", + ".", + -1, + nil, + }, + } { + p := Single{test.separators} + index, segments := p.Index(test.fixture) + if index != test.index { + t.Errorf("#%d unexpected index: exp: %d, act: %d", id, test.index, index) + } + if !reflect.DeepEqual(segments, test.segments) { + t.Errorf("#%d unexpected segments: exp: %v, act: %v", id, test.segments, segments) + } + } +} + +func BenchmarkIndexSingle(b *testing.B) { + m := Single{bench_separators} + for i := 0; i < b.N; i++ { + m.Index(bench_pattern) + } +} diff --git a/match/suffix_test.go b/match/suffix_test.go index 5c3e434..aca6eef 100644 --- a/match/suffix_test.go +++ b/match/suffix_test.go @@ -35,3 +35,10 @@ func TestSuffixIndex(t *testing.T) { } } } + +func BenchmarkIndexSuffix(b *testing.B) { + m := Suffix{"qwe"} + for i := 0; i < b.N; i++ { + m.Index(bench_pattern) + } +} diff --git a/match/super_test.go b/match/super_test.go new file mode 100644 index 0000000..b649fb1 --- /dev/null +++ b/match/super_test.go @@ -0,0 +1,41 @@ +package match + +import ( + "reflect" + "testing" +) + +func TestSuperIndex(t *testing.T) { + for id, test := range []struct { + fixture string + index int + segments []int + }{ + { + "abc", + 0, + []int{0, 1, 2, 3}, + }, + { + "", + 0, + []int{0}, + }, + } { + p := Super{} + index, segments := p.Index(test.fixture) + if index != test.index { + t.Errorf("#%d unexpected index: exp: %d, act: %d", id, test.index, index) + } + if !reflect.DeepEqual(segments, test.segments) { + t.Errorf("#%d unexpected segments: exp: %v, act: %v", id, test.segments, segments) + } + } +} + +func BenchmarkIndexSuper(b *testing.B) { + m := Super{} + for i := 0; i < b.N; i++ { + m.Index(bench_pattern) + } +} diff --git a/match/text_test.go b/match/text_test.go new file mode 100644 index 0000000..b7e1d5a --- /dev/null +++ b/match/text_test.go @@ -0,0 +1,44 @@ +package match + +import ( + "reflect" + "testing" +) + +func TestTextIndex(t *testing.T) { + for id, test := range []struct { + text string + fixture string + index int + segments []int + }{ + { + "b", + "abc", + 1, + []int{1}, + }, + { + "f", + "abcd", + -1, + nil, + }, + } { + m := NewText(test.text) + index, segments := m.Index(test.fixture) + if index != test.index { + t.Errorf("#%d unexpected index: exp: %d, act: %d", id, test.index, index) + } + if !reflect.DeepEqual(segments, test.segments) { + t.Errorf("#%d unexpected segments: exp: %v, act: %v", id, test.segments, segments) + } + } +} + +func BenchmarkIndexText(b *testing.B) { + m := NewText("foo") + for i := 0; i < b.N; i++ { + m.Index(bench_pattern) + } +}