func TestReadWriteFile(t *testing.T) { t.Parallel() f, err := os.Create("tmpfile") if err != nil { t.Fatal(err) } defer func() { f.Close() os.Remove("tmpfile") }() data := make([]byte, 1024*1024) rand.Read(data) err = msgp.WriteFile(rawBytes(data), f) if err != nil { t.Fatal(err) } var out rawBytes err = msgp.ReadFile(&out, f) if err != nil { t.Fatal(err) } if !bytes.Equal([]byte(out), []byte(data)) { t.Fatal("Input and output not equal.") } }
func BenchmarkWriteReadFile(b *testing.B) { // let's not run out of disk space... if b.N > 10000000 { b.N = 10000000 } fname := "bench-tmpfile" f, err := os.Create(fname) if err != nil { b.Fatal(err) } defer func(f *os.File, name string) { f.Close() os.Remove(name) }(f, fname) data := make(Blobs, b.N) for i := range data { data[i].Name = blobstrings[prand.Intn(len(blobstrings))] data[i].Float = blobfloats[prand.Intn(len(blobfloats))] data[i].Amount = blobints[prand.Intn(len(blobints))] data[i].Bytes = blobbytes[prand.Intn(len(blobbytes))] } b.SetBytes(int64(data.Msgsize() / b.N)) b.ResetTimer() err = msgp.WriteFile(data, f) if err != nil { b.Fatal(err) } err = msgp.ReadFile(&data, f) if err != nil { b.Fatal(err) } }