github.com/waldiirawan/apm-agent-go/v2@v2.2.2/internal/ringbuffer/buffer_test.go (about) 1 // Licensed to Elasticsearch B.V. under one or more contributor 2 // license agreements. See the NOTICE file distributed with 3 // this work for additional information regarding copyright 4 // ownership. Elasticsearch B.V. licenses this file to you under 5 // the Apache License, Version 2.0 (the "License"); you may 6 // not use this file except in compliance with the License. 7 // You may obtain a copy of the License at 8 // 9 // http://www.apache.org/licenses/LICENSE-2.0 10 // 11 // Unless required by applicable law or agreed to in writing, 12 // software distributed under the License is distributed on an 13 // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 14 // KIND, either express or implied. See the License for the 15 // specific language governing permissions and limitations 16 // under the License. 17 18 package ringbuffer 19 20 import ( 21 "bytes" 22 "encoding/binary" 23 "io" 24 "io/ioutil" 25 "strings" 26 "testing" 27 28 "github.com/stretchr/testify/assert" 29 ) 30 31 func TestBlockHeaderSize(t *testing.T) { 32 size := binary.Size(BlockHeader{}) 33 assert.Equal(t, BlockHeaderSize, size) 34 } 35 36 func TestBuffer(t *testing.T) { 37 b := New(150) 38 assert.Equal(t, 0, b.Len()) 39 assert.Equal(t, 150, b.Cap()) 40 41 const block = `{"transaction":{"duration":0,"id":"00000000-0000-0000-0000-000000000000","name":"","timestamp":"0001-01-01T00:00:00Z","type":""}}` 42 43 for i := 0; i < 10; i++ { 44 b.WriteBlock([]byte(block), 0) 45 blen := b.Len() 46 assert.NotEqual(t, 0, blen) 47 assert.Equal(t, 150, b.Cap()) 48 49 var bb bytes.Buffer 50 _, n, err := b.WriteBlockTo(&bb) 51 assert.NoError(t, err) 52 assert.Equal(t, int64(blen-BlockHeaderSize), n) 53 assert.Equal(t, block, bb.String()) 54 assert.Equal(t, 0, b.Len()) 55 _, n, err = b.WriteBlockTo(&bb) 56 assert.Zero(t, n) 57 assert.Equal(t, io.EOF, err) 58 } 59 } 60 61 func TestBufferEviction(t *testing.T) { 62 const block = `{"transaction":{"duration":0,"id":"00000000-0000-0000-0000-000000000000","name":"","timestamp":"0001-01-01T00:00:00Z","type":""}}` 63 64 var evicted []BlockHeader 65 b := New(300) 66 b.Evicted = func(h BlockHeader) { 67 evicted = append(evicted, h) 68 } 69 for i := 0; i < 100; i++ { 70 b.WriteBlock([]byte(block), BlockTag(i)) 71 } 72 assert.Equal(t, len(block)*2+2*BlockHeaderSize, b.Len()) 73 74 for i := 0; i < 2; i++ { 75 var bb bytes.Buffer 76 b.WriteBlockTo(&bb) 77 assert.Equal(t, block, bb.String()) 78 } 79 assert.Equal(t, 0, b.Len()) 80 assert.Len(t, evicted, 98) 81 for i, h := range evicted { 82 assert.Equal(t, BlockTag(i), h.Tag) 83 assert.Equal(t, uint32(len(block)), h.Size) 84 } 85 } 86 87 func BenchmarkWrite(b *testing.B) { 88 data := []byte(strings.Repeat("*", 1024)) 89 buf := New(10 * 1024 * 1024) 90 b.ReportAllocs() 91 b.ResetTimer() 92 for i := 0; i < b.N; i++ { 93 n, err := buf.WriteBlock(data[:], 0) 94 if err != nil { 95 panic(err) 96 } 97 b.SetBytes(int64(n)) 98 } 99 } 100 101 func BenchmarkWriteBlockTo(b *testing.B) { 102 data := []byte(strings.Repeat("*", 300)) 103 buf := New(b.N * (len(data) + BlockHeaderSize)) 104 for i := 0; i < b.N; i++ { 105 buf.WriteBlock(data[:], 0) 106 } 107 b.ReportAllocs() 108 b.ResetTimer() 109 for i := 0; i < b.N; i++ { 110 _, n, err := buf.WriteBlockTo(ioutil.Discard) 111 if err != nil { 112 panic(err) 113 } 114 b.SetBytes(n) 115 } 116 }