-
Notifications
You must be signed in to change notification settings - Fork 1
/
comp_test.go
76 lines (63 loc) · 1.51 KB
/
comp_test.go
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package rewrite
import (
"bytes"
"strings"
"testing"
)
const testHTML = `
<html>
<head>
</head>
<body id="body">
<h1>Example Header</h1>
<h2>Sub Header</h2>
<p id="meow">Asaf Test Content</p>
</body>
</html>
`
const testValue = "Great Test Value"
func BenchmarkAppend(b *testing.B) {
appendedValue := "<div></div>"
b.Run("std lib", func(b *testing.B) {
for n := 0; n < b.N; n++ {
writer, _ := Load(strings.NewReader(testHTML))
_ = writer.Append("id=body", appendedValue)
}
b.ReportAllocs()
})
b.Run("stream", func(b *testing.B) {
readers := make([]*strings.Reader, b.N)
buffers := make([]*bytes.Buffer, b.N)
for n := 0; n < b.N; n++ {
readers[n] = strings.NewReader(testHTML)
buffers[n] = bytes.NewBuffer(make([]byte, 0, len(testHTML)*2))
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
_ = Append(readers[n], buffers[n], "id=body", appendedValue)
}
b.ReportAllocs()
})
}
func BenchmarkSet(b *testing.B) {
b.Run("std lib", func(b *testing.B) {
for n := 0; n < b.N; n++ {
writer, _ := Load(strings.NewReader(testHTML))
_ = writer.Set("id=meow", testValue)
}
b.ReportAllocs()
})
b.Run("stream", func(b *testing.B) {
readers := make([]*strings.Reader, b.N)
buffers := make([]*bytes.Buffer, b.N)
for n := 0; n < b.N; n++ {
readers[n] = strings.NewReader(testHTML)
buffers[n] = bytes.NewBuffer(make([]byte, 0, len(testHTML)*2))
}
b.ResetTimer()
for n := 0; n < b.N; n++ {
_ = Set(readers[n], buffers[n], "id=meow", testValue)
}
b.ReportAllocs()
})
}