github.com/serversong/goreporter@v0.0.0-20200325104552-3cfaf44fd178/linters/spellcheck/misspell/benchmark_test.go (about) 1 package misspell 2 3 import ( 4 "bytes" 5 "io/ioutil" 6 "testing" 7 ) 8 9 var ( 10 sampleClean string 11 sampleDirty string 12 tmpCount int 13 tmp string 14 rep *Replacer 15 ) 16 17 func init() { 18 19 buf := bytes.Buffer{} 20 for i := 0; i < len(DictMain); i += 2 { 21 buf.WriteString(DictMain[i+1] + " ") 22 if i%5 == 0 { 23 buf.WriteString("\n") 24 } 25 } 26 sampleClean = buf.String() 27 sampleDirty = sampleClean + DictMain[0] + "\n" 28 rep = New() 29 } 30 31 // BenchmarkCleanString takes a clean string (one with no errors) 32 func BenchmarkCleanString(b *testing.B) { 33 b.ResetTimer() 34 b.ReportAllocs() 35 var updated string 36 var diffs []Diff 37 var count int 38 for n := 0; n < b.N; n++ { 39 updated, diffs = rep.Replace(sampleClean) 40 count += len(diffs) 41 } 42 43 // prevent compilier optimizations 44 tmpCount = count 45 tmp = updated 46 } 47 48 func discardDiff(_ Diff) { 49 tmpCount++ 50 } 51 52 // BenchmarkCleanStream takes a clean reader (no misspells) and outputs to a buffer 53 func BenchmarkCleanStream(b *testing.B) { 54 b.ResetTimer() 55 b.ReportAllocs() 56 tmpCount = 0 57 buf := bytes.NewBufferString(sampleClean) 58 out := bytes.NewBuffer(make([]byte, 0, len(sampleClean)+100)) 59 for n := 0; n < b.N; n++ { 60 buf.Reset() 61 buf.WriteString(sampleClean) 62 out.Reset() 63 rep.ReplaceReader(buf, out, discardDiff) 64 } 65 } 66 67 // BenchmarkCleanStreamDiscard takes a clean reader and discards output 68 func BenchmarkCleanStreamDiscard(b *testing.B) { 69 b.ResetTimer() 70 b.ReportAllocs() 71 72 buf := bytes.NewBufferString(sampleClean) 73 tmpCount = 0 74 for n := 0; n < b.N; n++ { 75 buf.Reset() 76 buf.WriteString(sampleClean) 77 rep.ReplaceReader(buf, ioutil.Discard, discardDiff) 78 } 79 } 80 81 // BenchmarkCleanString takes a clean string (one with no errors) 82 func BenchmarkDirtyString(b *testing.B) { 83 b.ResetTimer() 84 b.ReportAllocs() 85 var updated string 86 var diffs []Diff 87 var count int 88 for n := 0; n < b.N; n++ { 89 updated, diffs = rep.Replace(sampleDirty) 90 count += len(diffs) 91 } 92 93 // prevent compilier optimizations 94 tmpCount = count 95 tmp = updated 96 } 97 98 func BenchmarkCompile(b *testing.B) { 99 r := New() 100 b.ReportAllocs() 101 b.ResetTimer() 102 for n := 0; n < b.N; n++ { 103 r.Compile() 104 } 105 }