| 69 | ) |
| 70 | |
| 71 | func BenchmarkWriteReadFile(b *testing.B) { |
| 72 | // let's not run out of disk space... |
| 73 | if b.N > 10000000 { |
| 74 | b.N = 10000000 //nolint:staticcheck // ignoring "SA3001: should not assign to b.N (staticcheck)" as this should not usually happen. |
| 75 | } |
| 76 | |
| 77 | fname := "bench-tmpfile" |
| 78 | f, err := os.Create(fname) |
| 79 | if err != nil { |
| 80 | b.Fatal(err) |
| 81 | } |
| 82 | defer func(f *os.File, name string) { |
| 83 | f.Close() |
| 84 | os.Remove(name) |
| 85 | }(f, fname) |
| 86 | |
| 87 | data := make(Blobs, b.N) |
| 88 | |
| 89 | for i := range data { |
| 90 | data[i].Name = blobstrings[prand.Intn(len(blobstrings))] |
| 91 | data[i].Float = blobfloats[prand.Intn(len(blobfloats))] |
| 92 | data[i].Amount = blobints[prand.Intn(len(blobints))] |
| 93 | data[i].Bytes = blobbytes[prand.Intn(len(blobbytes))] |
| 94 | } |
| 95 | |
| 96 | b.SetBytes(int64(data.Msgsize() / b.N)) |
| 97 | b.ResetTimer() |
| 98 | err = msgp.WriteFile(data, f) |
| 99 | if err != nil { |
| 100 | b.Fatal(err) |
| 101 | } |
| 102 | err = msgp.ReadFile(&data, f) |
| 103 | if err != nil { |
| 104 | b.Fatal(err) |
| 105 | } |
| 106 | } |