Process []byte instread of string in parser

This commit is contained in:
Ingo Oppermann
2024-07-26 11:31:47 +02:00
parent d391e274d7
commit 70a49f8bdb
7 changed files with 209 additions and 125 deletions

View File

@@ -1,12 +1,15 @@
package process
import (
"bufio"
"bytes"
"fmt"
"sync"
"testing"
"time"
"github.com/datarhei/core/v16/internal/testhelper"
"github.com/datarhei/core/v16/math/rand"
"github.com/stretchr/testify/require"
)
@@ -701,3 +704,63 @@ func TestProcessCallbacksOnBeforeStart(t *testing.T) {
require.Equal(t, 1, len(lines))
require.Equal(t, "no, not now", lines[0].Data)
}
func BenchmarkScannerText(b *testing.B) {
data := []byte{}
for i := 0; i < 1000; i++ {
line := rand.String(100) + "\n"
data = append(data, []byte(line)...)
}
b.ResetTimer()
lastline := ""
for i := 0; i < b.N; i++ {
r := bytes.NewReader(data)
scanner := bufio.NewScanner(r)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
line := scanner.Text()
lastline = line
}
err := scanner.Err()
require.NoError(b, err)
}
fmt.Printf("%s\n", lastline)
}
func BenchmarkScannerBytes(b *testing.B) {
data := []byte{}
for i := 0; i < 1000; i++ {
line := rand.String(100) + "\n"
data = append(data, []byte(line)...)
}
b.ResetTimer()
lastline := []byte{}
for i := 0; i < b.N; i++ {
r := bytes.NewReader(data)
scanner := bufio.NewScanner(r)
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
line := scanner.Bytes()
lastline = line
}
err := scanner.Err()
require.NoError(b, err)
}
fmt.Printf("%s\n", lastline)
}