Skip to content

Instantly share code, notes, and snippets.

@felixge
Created January 10, 2023 14:56
Show Gist options
  • Save felixge/f99014dac01401ab7f273532b1c024f3 to your computer and use it in GitHub Desktop.
Save felixge/f99014dac01401ab7f273532b1c024f3 to your computer and use it in GitHub Desktop.
package main
import (
"bytes"
"compress/gzip"
"io"
"os"
"testing"
)
func TestInterleavedIO(t *testing.T) {
// Open big file with random data
inFile, err := os.Open("random.data")
if err != nil {
t.Fatal("could not open input file: create with `dd if=/dev/random of=random.data bs=1MB count=250`", err)
}
defer inFile.Close()
// Open the output file.
outFile, err := os.Create("random.data.gz")
if err != nil {
t.Fatal("could not open input file: ", err)
}
defer outFile.Close()
// Create the gzip writer.
gw := gzip.NewWriter(outFile)
// Compress the data from inFile by copying it to gw
if _, err := io.Copy(gw, inFile); err != nil {
t.Fatal("failed to copy data ", err)
}
if err := gw.Flush(); err != nil {
t.Fatal("failed to flush gzip writer ", err)
}
}
func TestSequentialIO(t *testing.T) {
// Read all input data into memory
input, err := os.ReadFile("random.data")
if err != nil {
t.Fatal("failed to read input file: create with `dd if=/dev/random of=random.data bs=1MB count=250`", err)
}
// Gzip compress inData into output (in memory, no I/O)
var output bytes.Buffer
gw := gzip.NewWriter(&output)
if _, err := gw.Write(input); err != nil {
t.Fatal("failed to write data ", err)
}
if err := gw.Flush(); err != nil {
t.Fatal("failed to flush gzip writer ", err)
}
// Write outFile to disk
if err := os.WriteFile("random.data.gz", output.Bytes(), 0666); err != nil {
t.Fatal("failed to write gzip data", err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment