From 29143753f61f3ddbed6ac456e17ef14532f39abe Mon Sep 17 00:00:00 2001 From: Ingo Oppermann Date: Fri, 5 Dec 2025 16:38:05 +0100 Subject: [PATCH] Minor adjustments to modern go --- ffmpeg/parse/parser_test.go | 4 +-- http/handler/api/process_test.go | 6 ++-- http/middleware/compress/compress_test.go | 4 +-- http/middleware/hlsrewrite/hlsrewrite_test.go | 2 +- http/middleware/session/HLS_test.go | 4 +-- http/middleware/session/session_test.go | 2 +- iam/policy/manager_test.go | 10 +++---- io/fs/mem_test.go | 30 +++++++------------ mem/buffer_test.go | 4 +-- process/process_test.go | 8 ++--- restream/core_test.go | 20 +++++-------- restream/fs/fs_test.go | 10 +++---- 12 files changed, 39 insertions(+), 65 deletions(-) diff --git a/ffmpeg/parse/parser_test.go b/ffmpeg/parse/parser_test.go index cb1fae2e..430bd2d1 100644 --- a/ffmpeg/parse/parser_test.go +++ b/ffmpeg/parse/parser_test.go @@ -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]) } diff --git a/http/handler/api/process_test.go b/http/handler/api/process_test.go index a70238e7..c1cb4ebf 100644 --- a/http/handler/api/process_test.go +++ b/http/handler/api/process_test.go @@ -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) } diff --git a/http/middleware/compress/compress_test.go b/http/middleware/compress/compress_test.go index 2d0d5b8b..646913ce 100644 --- a/http/middleware/compress/compress_test.go +++ b/http/middleware/compress/compress_test.go @@ -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) diff --git a/http/middleware/hlsrewrite/hlsrewrite_test.go b/http/middleware/hlsrewrite/hlsrewrite_test.go index a149feaa..cb1880dd 100644 --- a/http/middleware/hlsrewrite/hlsrewrite_test.go +++ b/http/middleware/hlsrewrite/hlsrewrite_test.go @@ -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) diff --git a/http/middleware/session/HLS_test.go b/http/middleware/session/HLS_test.go index b5e7a958..71f7bdc0 100644 --- a/http/middleware/session/HLS_test.go +++ b/http/middleware/session/HLS_test.go @@ -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(), } diff --git a/http/middleware/session/session_test.go b/http/middleware/session/session_test.go index f5c0c597..013819af 100644 --- a/http/middleware/session/session_test.go +++ b/http/middleware/session/session_test.go @@ -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) } } diff --git a/iam/policy/manager_test.go b/iam/policy/manager_test.go index 830fb3ad..87c37059 100644 --- a/iam/policy/manager_test.go +++ b/iam/policy/manager_test.go @@ -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) diff --git a/io/fs/mem_test.go b/io/fs/mem_test.go index be179d5c..0a5932f6 100644 --- a/io/fs/mem_test.go +++ b/io/fs/mem_test.go @@ -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() diff --git a/mem/buffer_test.go b/mem/buffer_test.go index a19c1a72..a32efb7c 100644 --- a/mem/buffer_test.go +++ b/mem/buffer_test.go @@ -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) diff --git a/process/process_test.go b/process/process_test.go index bfcf841b..d38839e4 100644 --- a/process/process_test.go +++ b/process/process_test.go @@ -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) diff --git a/restream/core_test.go b/restream/core_test.go index 055a9463..07e141ab 100644 --- a/restream/core_test.go +++ b/restream/core_test.go @@ -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) } } diff --git a/restream/fs/fs_test.go b/restream/fs/fs_test.go index cf66a494..231e0cfd 100644 --- a/restream/fs/fs_test.go +++ b/restream/fs/fs_test.go @@ -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]]) } }