Skip to content

Commit

Permalink
run limitio tests in parallel
Browse files Browse the repository at this point in the history
  • Loading branch information
creativeprojects committed Apr 26, 2024
1 parent 2229c8f commit 5e4e693
Showing 1 changed file with 56 additions and 47 deletions.
103 changes: 56 additions & 47 deletions limitio/stream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/creativeprojects/imap/limitio"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const burst = 1024 // 1KB of burst
Expand All @@ -24,9 +25,9 @@ func getRates() []float64 {

func getSources() []*bytes.Reader {
return []*bytes.Reader{
bytes.NewReader(bytes.Repeat([]byte{0}, 64*1024)), // 64KB
bytes.NewReader(bytes.Repeat([]byte{1}, 256*1024)), // 256KB
bytes.NewReader(bytes.Repeat([]byte{2}, 1024*1024)), // 1MB
bytes.NewReader(bytes.Repeat([]byte{10}, 64*1024)), // 64KB
bytes.NewReader(bytes.Repeat([]byte{11}, 256*1024)), // 256KB
bytes.NewReader(bytes.Repeat([]byte{12}, 1024*1024)), // 1MB
}
}

Expand All @@ -36,28 +37,32 @@ func TestRead(t *testing.T) {
t.Skip("skipping test in short mode.")
}

for _, src := range getSources() {
for _, limit := range getRates() {
src.Seek(0, 0)
sio := limitio.NewReader(src)
sio.SetRateLimit(limit, burst)
start := time.Now()
n, err := io.Copy(io.Discard, sio)
elapsed := time.Since(start)
if err != nil {
t.Error("io.Copy failed", err)
}
realRate := float64(n) / elapsed.Seconds()
percent := realRate / limit * 100
assert.InDelta(t, 100, percent, 2) // 2% error margin
t.Logf(
"read %s / %s: Real %s/sec Limit %s/sec. (%f %%)",
iBytes(uint64(n)),
elapsed,
iBytes(uint64(realRate)),
iBytes(uint64(limit)),
percent,
)
for _, limit := range getRates() {
for _, src := range getSources() {
t.Run(fmt.Sprintf("Read %s at %s/sec", iBytes(uint64(src.Len())), iBytes(uint64(limit))), func(t *testing.T) {
t.Parallel()
_, err := src.Seek(0, 0)
require.NoError(t, err)
sio := limitio.NewReader(src)
sio.SetRateLimit(limit, burst)
start := time.Now()
n, err := io.Copy(io.Discard, sio)
elapsed := time.Since(start)
if err != nil {
t.Error("io.Copy failed", err)
}
realRate := float64(n) / elapsed.Seconds()
percent := realRate / limit * 100
assert.InDelta(t, 100, percent, 2) // 2% error margin
t.Logf(
"read %s / %s: Real %s/sec Limit %s/sec. (%.2f %%)",
iBytes(uint64(n)),
elapsed,
iBytes(uint64(realRate)),
iBytes(uint64(limit)),
percent,
)
})
}
}
}
Expand All @@ -68,28 +73,32 @@ func TestWrite(t *testing.T) {
t.Skip("skipping test in short mode.")
}

for _, src := range getSources() {
for _, limit := range getRates() {
src.Seek(0, 0)
sio := limitio.NewWriter(io.Discard)
sio.SetRateLimit(limit, burst)
start := time.Now()
n, err := io.Copy(sio, src)
elapsed := time.Since(start)
if err != nil {
t.Error("io.Copy failed", err)
}
realRate := float64(n) / elapsed.Seconds()
percent := realRate / limit * 100
assert.InDelta(t, 100, percent, 2) // 2% error margin
t.Logf(
"write %s / %s: Real %s/sec Limit %s/sec. (%f %%)",
iBytes(uint64(n)),
elapsed,
iBytes(uint64(realRate)),
iBytes(uint64(limit)),
percent,
)
for _, limit := range getRates() {
for _, src := range getSources() {
t.Run(fmt.Sprintf("Read %s at %s/sec", iBytes(uint64(src.Len())), iBytes(uint64(limit))), func(t *testing.T) {
t.Parallel()
_, err := src.Seek(0, 0)
require.NoError(t, err)
sio := limitio.NewWriter(io.Discard)
sio.SetRateLimit(limit, burst)
start := time.Now()
n, err := io.Copy(sio, src)
elapsed := time.Since(start)
if err != nil {
t.Error("io.Copy failed", err)
}
realRate := float64(n) / elapsed.Seconds()
percent := realRate / limit * 100
assert.InDelta(t, 100, percent, 2) // 2% error margin
t.Logf(
"write %s / %s: Real %s/sec Limit %s/sec. (%.2f %%)",
iBytes(uint64(n)),
elapsed,
iBytes(uint64(realRate)),
iBytes(uint64(limit)),
percent,
)
})
}
}
}
Expand Down

0 comments on commit 5e4e693

Please sign in to comment.