|
|
@@ -3,7 +3,11 @@
|
|
|
|
|
|
package sanitizer
|
|
|
|
|
|
-import "testing"
|
|
|
+import (
|
|
|
+ "os"
|
|
|
+ "strconv"
|
|
|
+ "testing"
|
|
|
+)
|
|
|
|
|
|
func TestTruncateHTML(t *testing.T) {
|
|
|
tests := []struct {
|
|
|
@@ -84,3 +88,45 @@ func TestTruncateHTML(t *testing.T) {
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+func BenchmarkTruncateHTML(b *testing.B) {
|
|
|
+ benches := []struct {
|
|
|
+ filename string
|
|
|
+ limit int
|
|
|
+ }{
|
|
|
+ {
|
|
|
+ filename: "miniflux_github.html",
|
|
|
+ limit: 100,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ filename: "miniflux_github.html",
|
|
|
+ limit: 10_000,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ filename: "miniflux_wikipedia.html",
|
|
|
+ limit: 100,
|
|
|
+ },
|
|
|
+ {
|
|
|
+ filename: "miniflux_wikipedia.html",
|
|
|
+ limit: 100_000,
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
+ for _, f := range benches {
|
|
|
+ data, err := os.ReadFile("testdata/" + f.filename)
|
|
|
+ if err != nil {
|
|
|
+ b.Fatalf(`Unable to read file %q: %v`, f.filename, err)
|
|
|
+ }
|
|
|
+
|
|
|
+ b.Run(f.filename+"_"+strconv.Itoa(f.limit), func(b *testing.B) {
|
|
|
+ var junk string
|
|
|
+
|
|
|
+ str := string(data)
|
|
|
+ for b.Loop() {
|
|
|
+ junk = TruncateHTML(str, 100)
|
|
|
+ }
|
|
|
+
|
|
|
+ _ = junk
|
|
|
+ })
|
|
|
+ }
|
|
|
+}
|