Last active
July 21, 2023 18:20
-
-
Save codeliger/626151c3a696c0c2c4b179bcbb586329 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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