Minor adjustments to modern go

This commit is contained in:
Ingo Oppermann
2025-12-05 16:38:05 +01:00
parent b5b16a6f9a
commit 29143753f6
12 changed files with 39 additions and 65 deletions

View File

@@ -1776,9 +1776,7 @@ func BenchmarkParserString(b *testing.B) {
[]byte(`[https @ 0x557c840d1080] [info] Opening 'https://ch-fra-n16.livespotting.com/vpu/e9slfpe3/z60wzayk_720_100794.ts' for reading`),
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
parser.Parse(data[0])
parser.Parse(data[1])
}

View File

@@ -754,7 +754,7 @@ func BenchmarkAllProcesses(b *testing.B) {
err = json.Unmarshal(data.Bytes(), &process)
require.NoError(b, err)
for i := 0; i < 1000; i++ {
for i := range 1000 {
process.ID = "test_" + strconv.Itoa(i)
encoded, err := json.Marshal(&process)
@@ -767,9 +767,7 @@ func BenchmarkAllProcesses(b *testing.B) {
mock.Request(b, http.StatusOK, router, "POST", "/", &data)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
response := mock.RequestEx(b, http.StatusOK, router, "GET", "/", nil, false)
require.Equal(b, response.Code, 200)
}

View File

@@ -392,7 +392,7 @@ func BenchmarkCompress(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
h(c)
@@ -423,7 +423,7 @@ func BenchmarkCompressJSON(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
rec := httptest.NewRecorder()
c := e.NewContext(req, rec)
h(c)

View File

@@ -39,7 +39,7 @@ func BenchmarkRewrite(b *testing.B) {
buffer := &mem.Buffer{}
prefix := []byte("/path/to/foobar/")
for i := 0; i < b.N; i++ {
for b.Loop() {
r.buffer.Reset()
r.Write(data)

View File

@@ -70,7 +70,7 @@ func BenchmarkHLSSegmentReader(b *testing.B) {
rd := bytes.NewReader(data)
r := io.NopCloser(rd)
for i := 0; i < b.N; i++ {
for b.Loop() {
rd.Reset(data)
br := &segmentReader{
reader: io.NopCloser(r),
@@ -139,7 +139,7 @@ func BenchmarkHLSRewrite(b *testing.B) {
u, err := url.Parse("http://example.com/test.m3u8")
require.NoError(b, err)
for i := 0; i < b.N; i++ {
for b.Loop() {
br := &sessionRewriter{
buffer: mem.Get(),
}

View File

@@ -158,7 +158,7 @@ func BenchmarkHeaderSize(b *testing.B) {
buffer := &mem.Buffer{}
for i := 0; i < b.N; i++ {
for b.Loop() {
headerSize(header, buffer)
}
}

View File

@@ -118,15 +118,13 @@ func BenchmarkEnforce(b *testing.B) {
names := []string{}
for i := 0; i < 1000; i++ {
for i := range 1000 {
name := fmt.Sprintf("user%d", i)
names = append(names, name)
am.AddPolicy(name, "$none", []string{"foobar"}, "**", []string{"ANY"})
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
name := names[rand.IntN(1000)]
ok, _ := am.Enforce(name, "$none", "foobar", "baz", "read")
require.True(b, ok)
@@ -145,7 +143,7 @@ func BenchmarkConcurrentEnforce(b *testing.B) {
names := []string{}
for i := 0; i < 1000; i++ {
for i := range 1000 {
name := fmt.Sprintf("user%d", i)
names = append(names, name)
am.AddPolicy(name, "$none", []string{"foobar"}, "**", []string{"ANY"})
@@ -160,7 +158,7 @@ func BenchmarkConcurrentEnforce(b *testing.B) {
go func() {
defer readerWg.Done()
for i := 0; i < b.N; i++ {
for b.Loop() {
name := names[rand.IntN(1000)]
ok, _ := am.Enforce(name, "$none", "foobar", "baz", "read")
require.True(b, ok)

View File

@@ -109,9 +109,7 @@ func benchmarkMemList(b *testing.B, fs Filesystem) {
fs.WriteFile(path, []byte("foobar"))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
fs.List("/", ListOptions{
Pattern: "/5/**",
})
@@ -119,15 +117,13 @@ func benchmarkMemList(b *testing.B, fs Filesystem) {
}
func benchmarkMemRemoveList(b *testing.B, fs Filesystem) {
for i := 0; i < 1000; i++ {
for i := range 1000 {
id := rand.StringAlphanumeric(8)
path := fmt.Sprintf("/%d/%s.dat", i, id)
fs.WriteFile(path, []byte("foobar"))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
fs.RemoveList("/", ListOptions{
Pattern: "/5/**",
})
@@ -137,16 +133,14 @@ func benchmarkMemRemoveList(b *testing.B, fs Filesystem) {
func benchmarkMemReadFile(b *testing.B, fs Filesystem) {
nFiles := 1000
for i := 0; i < nFiles; i++ {
for i := range nFiles {
path := fmt.Sprintf("/%d.dat", i)
fs.WriteFile(path, []byte(rand.StringAlphanumeric(2*1024)))
}
r := gorand.New(gorand.NewSource(42))
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
num := r.Intn(nFiles)
f := fs.Open("/" + strconv.Itoa(num) + ".dat")
f.Close()
@@ -156,14 +150,12 @@ func benchmarkMemReadFile(b *testing.B, fs Filesystem) {
func benchmarkMemWriteFile(b *testing.B, fs Filesystem) {
nFiles := 50000
for i := 0; i < nFiles; i++ {
for i := range nFiles {
path := fmt.Sprintf("/%d.dat", i)
fs.WriteFile(path, []byte(rand.StringAlphanumeric(1)))
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for i := 0; b.Loop(); i++ {
path := fmt.Sprintf("/%d.dat", i%nFiles)
fs.WriteFile(path, []byte(rand.StringAlphanumeric(1)))
}
@@ -181,11 +173,11 @@ func benchmarkMemReadFileWhileWriting(b *testing.B, fs Filesystem) {
data := []byte(rand.StringAlphanumeric(2 * 1024))
for i := 0; i < nWriters; i++ {
for i := range nWriters {
writerWg.Add(1)
go func(ctx context.Context, from int) {
for i := 0; i < nFiles; i++ {
for i := range nFiles {
path := fmt.Sprintf("/%d.dat", from+i)
fs.WriteFile(path, data)
}
@@ -215,12 +207,12 @@ func benchmarkMemReadFileWhileWriting(b *testing.B, fs Filesystem) {
readerWg := sync.WaitGroup{}
for i := 0; i < nReaders; i++ {
for range nReaders {
readerWg.Add(1)
go func() {
defer readerWg.Done()
for i := 0; i < b.N; i++ {
for b.Loop() {
num := gorand.Intn(nWriters * nFiles)
f := fs.Open("/" + strconv.Itoa(num) + ".dat")
f.Close()

View File

@@ -27,7 +27,7 @@ func BenchmarkBufferReadFrom(b *testing.B) {
r := bytes.NewReader(data)
for i := 0; i < b.N; i++ {
for b.Loop() {
r.Seek(0, io.SeekStart)
buf := &Buffer{}
buf.ReadFrom(r)
@@ -39,7 +39,7 @@ func BenchmarkBytesBufferReadFrom(b *testing.B) {
r := bytes.NewReader(data)
for i := 0; i < b.N; i++ {
for b.Loop() {
r.Seek(0, io.SeekStart)
buf := &bytes.Buffer{}
buf.ReadFrom(r)

View File

@@ -751,11 +751,9 @@ func BenchmarkScannerText(b *testing.B) {
data = append(data, []byte(line)...)
}
b.ResetTimer()
lastline := ""
for i := 0; i < b.N; i++ {
for b.Loop() {
r := bytes.NewReader(data)
scanner := bufio.NewScanner(r)
scanner.Split(bufio.ScanLines)
@@ -781,11 +779,9 @@ func BenchmarkScannerBytes(b *testing.B) {
data = append(data, []byte(line)...)
}
b.ResetTimer()
lastline := []byte{}
for i := 0; i < b.N; i++ {
for b.Loop() {
r := bytes.NewReader(data)
scanner := bufio.NewScanner(r)
scanner.Split(bufio.ScanLines)

View File

@@ -1707,7 +1707,7 @@ func BenchmarkGetProcessIDs(b *testing.B) {
rs, err := getDummyRestreamer(nil, nil, nil, nil, false)
require.NoError(b, err)
for i := 0; i < 1000; i++ {
for i := range 1000 {
process := getDummyProcess()
process.ID = "test_" + strconv.Itoa(i)
@@ -1715,9 +1715,7 @@ func BenchmarkGetProcessIDs(b *testing.B) {
require.Equal(b, nil, err, "Failed to add process (%s)", err)
}
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
ids := rs.GetProcessIDs("", "", "", "")
require.NotEmpty(b, ids)
require.Equal(b, 1000, len(ids))
@@ -1728,7 +1726,7 @@ func BenchmarkGetProcess(b *testing.B) {
rs, err := getDummyRestreamer(nil, nil, nil, nil, false)
require.NoError(b, err)
for i := 0; i < 1000; i++ {
for i := range 1000 {
process := getDummyProcess()
process.ID = "test_" + strconv.Itoa(i)
@@ -1738,9 +1736,7 @@ func BenchmarkGetProcess(b *testing.B) {
rand := rand.New(rand.NewSource(42))
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
n := rand.Intn(1000)
p, err := rs.GetProcess(app.NewProcessID("test_"+strconv.Itoa(n), ""))
require.NotNil(b, p)
@@ -1754,7 +1750,7 @@ func BenchmarkGetProcessState(b *testing.B) {
n := 10
for i := 0; i < n; i++ {
for i := range n {
process := getDummyProcess()
process.ID = "test_" + strconv.Itoa(i)
process.Autostart = true
@@ -1767,16 +1763,14 @@ func BenchmarkGetProcessState(b *testing.B) {
time.Sleep(10 * time.Second)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
n := rand.Intn(n)
s, err := rs.GetProcessState(app.NewProcessID("test_"+strconv.Itoa(n), ""))
require.NotNil(b, s)
require.Nil(b, err)
}
for i := 0; i < n; i++ {
for range n {
rs.DeleteProcess(app.NewProcessID("test_"+strconv.Itoa(n), ""), true)
}
}

View File

@@ -373,8 +373,7 @@ func BenchmarkCleanup(b *testing.B) {
rfs := cleanfs.(*filesystem)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
rfs.cleanup()
}
}
@@ -393,7 +392,7 @@ func BenchmarkPurge(b *testing.B) {
ids := make([]string, nProcs)
for i := 0; i < nProcs; i++ {
for i := range nProcs {
id := rand.StringAlphanumeric(8)
patterns := []Pattern{
@@ -435,7 +434,7 @@ func BenchmarkPurge(b *testing.B) {
}
// Fill the filesystem with files
for j := 0; j < nProcs; j++ {
for j := range nProcs {
path := fmt.Sprintf("/%d/%s.m3u8", j, ids[j])
memfs.WriteFile(path, []byte("foobar"))
path = fmt.Sprintf("/%d/%s_0.m3u8", j, ids[j])
@@ -452,8 +451,7 @@ func BenchmarkPurge(b *testing.B) {
rfs := cleanfs.(*filesystem)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for b.Loop() {
rfs.purge(rfs.cleanupPatterns[ids[42]])
}
}