1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
16 var _ = Suite(&writeBufferSuite{})
18 type writeBufferSuite struct {
21 // 1000 / 96.3 ns/op = 10.384 GB/s
22 func (s *writeBufferSuite) Benchmark_1KBWrites(c *C) {
23 wb := newWriteBuffer(io.Discard, 1<<20)
24 in := make([]byte, 1000)
25 for i := 0; i < c.N; i++ {
31 func (s *writeBufferSuite) TestRandomizedSpeedsAndSizes(c *C) {
32 for i := 0; i < 20; i++ {
33 insize := rand.Intn(1 << 26)
34 bufsize := rand.Intn(1 << 26)
36 // make sure to test edge cases
38 } else if insize/bufsize > 1000 {
39 // don't waste too much time testing tiny
40 // buffer / huge content
41 insize = bufsize*1000 + 123
43 c.Logf("%s: insize %d bufsize %d", c.TestName(), insize, bufsize)
45 in := make([]byte, insize)
52 out := &bytes.Buffer{}
53 done := make(chan struct{})
56 n, err := slowCopy(out, pr, rand.Intn(8192)+1)
58 c.Check(n, Equals, int64(insize))
61 wb := newWriteBuffer(pw, bufsize)
62 n, err := slowCopy(wb, bytes.NewBuffer(in), rand.Intn(8192)+1)
64 c.Check(n, Equals, int64(insize))
65 c.Check(wb.Close(), IsNil)
66 c.Check(pw.Close(), IsNil)
68 c.Check(out.Len(), Equals, insize)
69 for i := 0; i < out.Len() && i < len(in); i++ {
70 if out.Bytes()[i] != in[i] {
71 c.Errorf("content mismatch at byte %d", i)
78 func slowCopy(dst io.Writer, src io.Reader, bufsize int) (int64, error) {
80 buf := make([]byte, bufsize)
82 time.Sleep(time.Duration(rand.Intn(100) + 1))
83 n, err := src.Read(buf)
85 n, err := dst.Write(buf[:n])