buffer_test.go 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 buffer
  21. import (
  22. "bytes"
  23. "strings"
  24. "testing"
  25. "time"
  26. "github.com/stretchr/testify/assert"
  27. )
  28. func TestBufferWrites(t *testing.T) {
  29. buf := NewPool().Get()
  30. tests := []struct {
  31. desc string
  32. f func()
  33. want string
  34. }{
  35. {"AppendByte", func() { buf.AppendByte('v') }, "v"},
  36. {"AppendString", func() { buf.AppendString("foo") }, "foo"},
  37. {"AppendIntPositive", func() { buf.AppendInt(42) }, "42"},
  38. {"AppendIntNegative", func() { buf.AppendInt(-42) }, "-42"},
  39. {"AppendUint", func() { buf.AppendUint(42) }, "42"},
  40. {"AppendBool", func() { buf.AppendBool(true) }, "true"},
  41. {"AppendFloat64", func() { buf.AppendFloat(3.14, 64) }, "3.14"},
  42. // Intentionally introduce some floating-point error.
  43. {"AppendFloat32", func() { buf.AppendFloat(float64(float32(3.14)), 32) }, "3.14"},
  44. {"AppendWrite", func() { buf.Write([]byte("foo")) }, "foo"},
  45. {"AppendTime", func() { buf.AppendTime(time.Date(2000, 1, 2, 3, 4, 5, 6, time.UTC), time.RFC3339) }, "2000-01-02T03:04:05Z"},
  46. {"WriteByte", func() { buf.WriteByte('v') }, "v"},
  47. {"WriteString", func() { buf.WriteString("foo") }, "foo"},
  48. }
  49. for _, tt := range tests {
  50. t.Run(tt.desc, func(t *testing.T) {
  51. buf.Reset()
  52. tt.f()
  53. assert.Equal(t, tt.want, buf.String(), "Unexpected buffer.String().")
  54. assert.Equal(t, tt.want, string(buf.Bytes()), "Unexpected string(buffer.Bytes()).")
  55. assert.Equal(t, len(tt.want), buf.Len(), "Unexpected buffer length.")
  56. // We're not writing more than a kibibyte in tests.
  57. assert.Equal(t, _size, buf.Cap(), "Expected buffer capacity to remain constant.")
  58. })
  59. }
  60. }
  61. func BenchmarkBuffers(b *testing.B) {
  62. // Because we use the strconv.AppendFoo functions so liberally, we can't
  63. // use the standard library's bytes.Buffer anyways (without incurring a
  64. // bunch of extra allocations). Nevertheless, let's make sure that we're
  65. // not losing any precious nanoseconds.
  66. str := strings.Repeat("a", 1024)
  67. slice := make([]byte, 1024)
  68. buf := bytes.NewBuffer(slice)
  69. custom := NewPool().Get()
  70. b.Run("ByteSlice", func(b *testing.B) {
  71. for i := 0; i < b.N; i++ {
  72. slice = append(slice, str...)
  73. slice = slice[:0]
  74. }
  75. })
  76. b.Run("BytesBuffer", func(b *testing.B) {
  77. for i := 0; i < b.N; i++ {
  78. buf.WriteString(str)
  79. buf.Reset()
  80. }
  81. })
  82. b.Run("CustomBuffer", func(b *testing.B) {
  83. for i := 0; i < b.N; i++ {
  84. custom.AppendString(str)
  85. custom.Reset()
  86. }
  87. })
  88. }