buffer_test.go 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (c) 2016 Uber Technologies, Inc.
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy
  4. // of this software and associated documentation files (the "Software"), to deal
  5. // in the Software without restriction, including without limitation the rights
  6. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. // copies of the Software, and to permit persons to whom the Software is
  8. // furnished to do so, subject to the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be included in
  11. // all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. // THE SOFTWARE.
  20. package core
  21. import (
  22. "bytes"
  23. "strings"
  24. "testing"
  25. "github.com/stretchr/testify/assert"
  26. )
  27. func TestBufferWrites(t *testing.T) {
  28. buf := NewPool(0).Get()
  29. tests := []struct {
  30. desc string
  31. f func()
  32. want string
  33. }{
  34. {"AppendByte", func() { buf.AppendByte('v') }, "v"},
  35. {"AppendString", func() { buf.AppendString("foo") }, "foo"},
  36. {"AppendIntPositive", func() { buf.AppendInt(42) }, "42"},
  37. {"AppendIntNegative", func() { buf.AppendInt(-42) }, "-42"},
  38. {"AppendUint", func() { buf.AppendUint(42) }, "42"},
  39. {"AppendBool", func() { buf.AppendBool(true) }, "true"},
  40. {"AppendFloat64", func() { buf.AppendFloat(3.14, 64) }, "3.14"},
  41. // Intenationally introduce some floating-point error.
  42. {"AppendFloat32", func() { buf.AppendFloat(float64(float32(3.14)), 32) }, "3.14"},
  43. {"AppendWrite", func() { buf.Write([]byte("foo")) }, "foo"},
  44. }
  45. for _, tt := range tests {
  46. t.Run(tt.desc, func(t *testing.T) {
  47. buf.Reset()
  48. tt.f()
  49. assert.Equal(t, tt.want, buf.String(), "Unexpected buffer.String().")
  50. assert.Equal(t, tt.want, string(buf.Bytes()), "Unexpected string(buffer.Bytes()).")
  51. assert.Equal(t, len(tt.want), buf.Len(), "Unexpected buffer length.")
  52. // We're not writing more than a kibibyte in tests.
  53. assert.Equal(t, _size, buf.Cap(), "Expected buffer capacity to remain constant.")
  54. })
  55. }
  56. }
  57. func BenchmarkBuffers(b *testing.B) {
  58. // Because we use the strconv.AppendFoo functions so liberally, we can't
  59. // use the standard library's bytes.Buffer anyways (without incurring a
  60. // bunch of extra allocations). Nevertheless, let's make sure that we're
  61. // not losing any precious nanoseconds.
  62. str := strings.Repeat("a", 1024)
  63. slice := make([]byte, 1024)
  64. buf := bytes.NewBuffer(slice)
  65. custom := NewPool(0).Get()
  66. b.Run("ByteSlice", func(b *testing.B) {
  67. for i := 0; i < b.N; i++ {
  68. slice = append(slice, str...)
  69. slice = slice[:0]
  70. }
  71. })
  72. b.Run("BytesBuffer", func(b *testing.B) {
  73. for i := 0; i < b.N; i++ {
  74. buf.WriteString(str)
  75. buf.Reset()
  76. }
  77. })
  78. b.Run("CustomBuffer", func(b *testing.B) {
  79. for i := 0; i < b.N; i++ {
  80. custom.AppendString(str)
  81. custom.Reset()
  82. }
  83. })
  84. }