2018-09-14 18:24:48 -06:00
|
|
|
package main
|
2018-05-09 21:52:52 -06:00
|
|
|
|
|
|
|
import (
|
|
|
|
"archive/zip"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
2019-12-07 21:17:13 -07:00
|
|
|
"math/rand"
|
2018-05-09 21:52:52 -06:00
|
|
|
"os"
|
|
|
|
"testing"
|
2019-12-07 21:17:13 -07:00
|
|
|
"time"
|
2018-05-09 21:52:52 -06:00
|
|
|
)
|
|
|
|
|
2019-12-07 21:17:13 -07:00
|
|
|
func TestZipPerformance(t *testing.T) {
|
|
|
|
// I get 4.8s for 10,000 reads
|
|
|
|
if os.Getenv("BENCHMARK") == "" {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
rng := rand.New(rand.NewSource(rand.Int63()))
|
|
|
|
|
|
|
|
tf, err := ioutil.TempFile("", "zipfs")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.Remove(tf.Name())
|
|
|
|
|
|
|
|
w := zip.NewWriter(tf)
|
|
|
|
for i := 0; i < 100; i += 1 {
|
|
|
|
fsize := 1000
|
|
|
|
switch {
|
|
|
|
case i % 10 == 0:
|
|
|
|
fsize = 400000
|
|
|
|
case i % 20 == 6:
|
|
|
|
fsize = 5000000
|
|
|
|
case i == 80:
|
|
|
|
fsize = 1000000000
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := w.Create(fmt.Sprintf("%d.bin", i))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err := io.CopyN(f, rng, int64(fsize)); err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
w.Close()
|
|
|
|
|
|
|
|
tfsize, err := tf.Seek(0, 2)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
startTime := time.Now()
|
|
|
|
nReads := 10000
|
|
|
|
for i := 0; i < 10000; i += 1 {
|
|
|
|
r, err := zip.NewReader(tf, tfsize)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
filenum := rng.Intn(len(r.File))
|
|
|
|
f, err := r.File[filenum].Open()
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
buf, err := ioutil.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
}
|
|
|
|
t.Log("Read file of size", len(buf))
|
|
|
|
f.Close()
|
|
|
|
}
|
|
|
|
t.Log(nReads, "reads took", time.Since(startTime))
|
|
|
|
t.Error("moo")
|
|
|
|
}
|
|
|
|
|
2019-08-15 19:46:49 -06:00
|
|
|
func TestZipfs(t *testing.T) {
|
|
|
|
tf, err := ioutil.TempFile("", "zipfs")
|
2018-05-09 21:52:52 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer os.Remove(tf.Name())
|
2018-09-17 17:00:08 -06:00
|
|
|
|
2018-05-09 21:52:52 -06:00
|
|
|
w := zip.NewWriter(tf)
|
|
|
|
f, err := w.Create("moo.txt")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// no Close method
|
|
|
|
|
|
|
|
_, err = fmt.Fprintln(f, "The cow goes moo")
|
|
|
|
//.Write([]byte("The cow goes moo"))
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
w.Close()
|
|
|
|
tf.Close()
|
2018-09-17 17:00:08 -06:00
|
|
|
|
2018-05-09 21:52:52 -06:00
|
|
|
// Now read it in
|
2019-08-15 19:46:49 -06:00
|
|
|
mb, err := OpenZipfs(tf.Name())
|
2018-05-09 21:52:52 -06:00
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cow, err := mb.Open("moo.txt")
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
2018-09-17 17:00:08 -06:00
|
|
|
|
2018-05-09 21:52:52 -06:00
|
|
|
line := make([]byte, 200)
|
|
|
|
n, err := cow.Read(line)
|
|
|
|
if (err != nil) && (err != io.EOF) {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if string(line[:n]) != "The cow goes moo\n" {
|
|
|
|
t.Log(line)
|
|
|
|
t.Error("Contents didn't match")
|
|
|
|
return
|
|
|
|
}
|
2018-09-17 17:00:08 -06:00
|
|
|
|
2018-05-09 21:52:52 -06:00
|
|
|
}
|