Skip to content

Instantly share code, notes, and snippets.

@codeliger
Last active July 21, 2023 18:20
Show Gist options
  • Save codeliger/626151c3a696c0c2c4b179bcbb586329 to your computer and use it in GitHub Desktop.
Save codeliger/626151c3a696c0c2c4b179bcbb586329 to your computer and use it in GitHub Desktop.
package test
import (
"bytes"
"crypto/rand"
"io"
"testing"
)
func generateRandomFile() io.Reader {
buf := &bytes.Buffer{}
buf.Grow(10 * 1024 * 1024)
_, _ = io.CopyN(buf, io.LimitReader(rand.Reader, 10*1024*1024), 10*1024*1024)
return buf
}
func BenchmarkReadAll(b *testing.B) {
randomFileBuffer := generateRandomFile()
b.ResetTimer()
for i := 0; i < b.N; i++ {
b, _ := io.ReadAll(randomFileBuffer)
_ = b
}
}
func BenchmarkCopy(b *testing.B) {
randomFileBuffer := generateRandomFile()
b.ResetTimer()
for i := 0; i < b.N; i++ {
buf := &bytes.Buffer{}
io.Copy(buf, randomFileBuffer)
b := buf.Bytes()
_ = b
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment