-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
58 lines (52 loc) · 919 Bytes
/
Copy pathmain_test.go
File metadata and controls
58 lines (52 loc) · 919 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package main
import (
"bufio"
"bytes"
"io"
"math/rand"
"os"
"testing"
"yapper/markov"
)
func BenchmarkMarkov(b *testing.B) {
f, _ := os.Open("./data/bench_text")
fc, _ := io.ReadAll(f)
f.Close()
const cl = 3
mk, err := markov.NewRecorder(cl, func(s string) string { return s }, StringHasherConst3)
if err != nil {
panic(err)
}
for iter := range bytes.SplitSeq(fc, []byte("\n\n")) {
sc := bufio.NewScanner(bytes.NewReader(iter))
sc.Split(SplitWords)
for sc.Scan() {
text := sc.Text()
mk.Push(text)
if text == "." {
mk.Flush()
}
}
if err := sc.Err(); err != nil {
panic(err)
}
mk.Flush()
}
ms := mk.Compile().NewSampler()
rnd := rand.New(rand.NewSource(0))
const wc = 500
for b.Loop() {
ms.Flush()
rnd.Seed(0)
for range wc {
_, err := ms.Next(rnd)
if err == io.EOF {
ms.Flush()
continue
}
if err != nil {
panic(err)
}
}
}
}