github.com/rohankumardubey/syslog-redirector-golang@v0.0.0-20140320174030-4859f03d829a/src/pkg/runtime/append_test.go (about) 1 // Copyright 2011 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 package runtime_test 5 6 import "testing" 7 8 const N = 20 9 10 func BenchmarkAppend(b *testing.B) { 11 b.StopTimer() 12 x := make([]int, 0, N) 13 b.StartTimer() 14 for i := 0; i < b.N; i++ { 15 x = x[0:0] 16 for j := 0; j < N; j++ { 17 x = append(x, j) 18 } 19 } 20 } 21 22 func benchmarkAppendBytes(b *testing.B, length int) { 23 b.StopTimer() 24 x := make([]byte, 0, N) 25 y := make([]byte, length) 26 b.StartTimer() 27 for i := 0; i < b.N; i++ { 28 x = x[0:0] 29 x = append(x, y...) 30 } 31 } 32 33 func BenchmarkAppend1Byte(b *testing.B) { 34 benchmarkAppendBytes(b, 1) 35 } 36 37 func BenchmarkAppend4Bytes(b *testing.B) { 38 benchmarkAppendBytes(b, 4) 39 } 40 41 func BenchmarkAppend7Bytes(b *testing.B) { 42 benchmarkAppendBytes(b, 7) 43 } 44 45 func BenchmarkAppend8Bytes(b *testing.B) { 46 benchmarkAppendBytes(b, 8) 47 } 48 49 func BenchmarkAppend15Bytes(b *testing.B) { 50 benchmarkAppendBytes(b, 15) 51 } 52 53 func BenchmarkAppend16Bytes(b *testing.B) { 54 benchmarkAppendBytes(b, 16) 55 } 56 57 func BenchmarkAppend32Bytes(b *testing.B) { 58 benchmarkAppendBytes(b, 32) 59 } 60 61 func benchmarkAppendStr(b *testing.B, str string) { 62 b.StopTimer() 63 x := make([]byte, 0, N) 64 b.StartTimer() 65 for i := 0; i < b.N; i++ { 66 x = x[0:0] 67 x = append(x, str...) 68 } 69 } 70 71 func BenchmarkAppendStr1Byte(b *testing.B) { 72 benchmarkAppendStr(b, "1") 73 } 74 75 func BenchmarkAppendStr4Bytes(b *testing.B) { 76 benchmarkAppendStr(b, "1234") 77 } 78 79 func BenchmarkAppendStr8Bytes(b *testing.B) { 80 benchmarkAppendStr(b, "12345678") 81 } 82 83 func BenchmarkAppendStr16Bytes(b *testing.B) { 84 benchmarkAppendStr(b, "1234567890123456") 85 } 86 87 func BenchmarkAppendStr32Bytes(b *testing.B) { 88 benchmarkAppendStr(b, "12345678901234567890123456789012") 89 } 90 91 func BenchmarkAppendSpecialCase(b *testing.B) { 92 b.StopTimer() 93 x := make([]int, 0, N) 94 b.StartTimer() 95 for i := 0; i < b.N; i++ { 96 x = x[0:0] 97 for j := 0; j < N; j++ { 98 if len(x) < cap(x) { 99 x = x[:len(x)+1] 100 x[len(x)-1] = j 101 } else { 102 x = append(x, j) 103 } 104 } 105 } 106 } 107 108 var x []int 109 110 func f() int { 111 x[:1][0] = 3 112 return 2 113 } 114 115 func TestSideEffectOrder(t *testing.T) { 116 x = make([]int, 0, 10) 117 x = append(x, 1, f()) 118 if x[0] != 1 || x[1] != 2 { 119 t.Error("append failed: ", x[0], x[1]) 120 } 121 } 122 123 func TestAppendOverlap(t *testing.T) { 124 x := []byte("1234") 125 x = append(x[1:], x...) // p > q in runtimeĀ·appendslice. 126 got := string(x) 127 want := "2341234" 128 if got != want { 129 t.Errorf("overlap failed: got %q want %q", got, want) 130 } 131 } 132 133 func benchmarkCopySlice(b *testing.B, l int) { 134 s := make([]byte, l) 135 buf := make([]byte, 4096) 136 var n int 137 for i := 0; i < b.N; i++ { 138 n = copy(buf, s) 139 } 140 b.SetBytes(int64(n)) 141 } 142 143 func benchmarkCopyStr(b *testing.B, l int) { 144 s := string(make([]byte, l)) 145 buf := make([]byte, 4096) 146 var n int 147 for i := 0; i < b.N; i++ { 148 n = copy(buf, s) 149 } 150 b.SetBytes(int64(n)) 151 } 152 153 func BenchmarkCopy1Byte(b *testing.B) { benchmarkCopySlice(b, 1) } 154 func BenchmarkCopy2Byte(b *testing.B) { benchmarkCopySlice(b, 2) } 155 func BenchmarkCopy4Byte(b *testing.B) { benchmarkCopySlice(b, 4) } 156 func BenchmarkCopy8Byte(b *testing.B) { benchmarkCopySlice(b, 8) } 157 func BenchmarkCopy12Byte(b *testing.B) { benchmarkCopySlice(b, 12) } 158 func BenchmarkCopy16Byte(b *testing.B) { benchmarkCopySlice(b, 16) } 159 func BenchmarkCopy32Byte(b *testing.B) { benchmarkCopySlice(b, 32) } 160 func BenchmarkCopy128Byte(b *testing.B) { benchmarkCopySlice(b, 128) } 161 func BenchmarkCopy1024Byte(b *testing.B) { benchmarkCopySlice(b, 1024) } 162 163 func BenchmarkCopy1String(b *testing.B) { benchmarkCopyStr(b, 1) } 164 func BenchmarkCopy2String(b *testing.B) { benchmarkCopyStr(b, 2) } 165 func BenchmarkCopy4String(b *testing.B) { benchmarkCopyStr(b, 4) } 166 func BenchmarkCopy8String(b *testing.B) { benchmarkCopyStr(b, 8) } 167 func BenchmarkCopy12String(b *testing.B) { benchmarkCopyStr(b, 12) } 168 func BenchmarkCopy16String(b *testing.B) { benchmarkCopyStr(b, 16) } 169 func BenchmarkCopy32String(b *testing.B) { benchmarkCopyStr(b, 32) } 170 func BenchmarkCopy128String(b *testing.B) { benchmarkCopyStr(b, 128) } 171 func BenchmarkCopy1024String(b *testing.B) { benchmarkCopyStr(b, 1024) }