buffer_test.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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. // Intenationally 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. }
  47. for _, tt := range tests {
  48. t.Run(tt.desc, func(t *testing.T) {
  49. buf.Reset()
  50. tt.f()
  51. assert.Equal(t, tt.want, buf.String(), "Unexpected buffer.String().")
  52. assert.Equal(t, tt.want, string(buf.Bytes()), "Unexpected string(buffer.Bytes()).")
  53. assert.Equal(t, len(tt.want), buf.Len(), "Unexpected buffer length.")
  54. // We're not writing more than a kibibyte in tests.
  55. assert.Equal(t, _size, buf.Cap(), "Expected buffer capacity to remain constant.")
  56. })
  57. }
  58. }
  59. func BenchmarkBuffers(b *testing.B) {
  60. // Because we use the strconv.AppendFoo functions so liberally, we can't
  61. // use the standard library's bytes.Buffer anyways (without incurring a
  62. // bunch of extra allocations). Nevertheless, let's make sure that we're
  63. // not losing any precious nanoseconds.
  64. str := strings.Repeat("a", 1024)
  65. slice := make([]byte, 1024)
  66. buf := bytes.NewBuffer(slice)
  67. custom := NewPool().Get()
  68. b.Run("ByteSlice", func(b *testing.B) {
  69. for i := 0; i < b.N; i++ {
  70. slice = append(slice, str...)
  71. slice = slice[:0]
  72. }
  73. })
  74. b.Run("BytesBuffer", func(b *testing.B) {
  75. for i := 0; i < b.N; i++ {
  76. buf.WriteString(str)
  77. buf.Reset()
  78. }
  79. })
  80. b.Run("CustomBuffer", func(b *testing.B) {
  81. for i := 0; i < b.N; i++ {
  82. custom.AppendString(str)
  83. custom.Reset()
  84. }
  85. })
  86. }