forked from linkedin/goavro
-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers_test.go
86 lines (76 loc) · 1.98 KB
/
helpers_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright [2019] LinkedIn Corp. Licensed under the Apache License, Version
// 2.0 (the "License"); you may not use this file except in compliance with the
// License. You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
package goavro
import (
"io"
"runtime"
"sync"
"testing"
)
func benchmarkLowAndHigh(b *testing.B, callback func()) {
b.Helper()
// Run test case in parallel at relative low concurrency
b.Run("Low", func(b *testing.B) {
b.ResetTimer()
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
callback()
}
})
})
// Run test case in parallel at relative high concurrency
b.Run("High", func(b *testing.B) {
concurrency := runtime.NumCPU() * 1000
wg := new(sync.WaitGroup)
wg.Add(concurrency)
b.ResetTimer()
for c := 0; c < concurrency; c++ {
go func() {
defer wg.Done()
for n := 0; n < b.N; n++ {
callback()
}
}()
}
wg.Wait()
})
}
// ShortWriter returns a structure that wraps an io.Writer, but returns
// io.ErrShortWrite when the number of bytes to write exceeds a preset limit.
//
// Copied with author's permission from https://github.com/karrick/gorill.
//
// bb := NopCloseBuffer()
// sw := ShortWriter(bb, 16)
//
// n, err := sw.Write([]byte("short write"))
// // n == 11, err == nil
//
// n, err := sw.Write([]byte("a somewhat longer write"))
// // n == 16, err == io.ErrShortWrite
func ShortWriter(w io.Writer, max int) io.Writer {
return shortWriter{Writer: w, max: max}
}
func (s shortWriter) Write(data []byte) (int, error) {
var short bool
index := len(data)
if index > s.max {
index = s.max
short = true
}
n, err := s.Writer.Write(data[:index])
if short {
return n, io.ErrShortWrite
}
return n, err
}
type shortWriter struct {
io.Writer
max int
}