trace_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // Copyright 2015 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 trace
  5. import (
  6. "net/http"
  7. "reflect"
  8. "testing"
  9. )
  10. type s struct{}
  11. func (s) String() string { return "lazy string" }
  12. // TestReset checks whether all the fields are zeroed after reset.
  13. func TestReset(t *testing.T) {
  14. tr := New("foo", "bar")
  15. tr.LazyLog(s{}, false)
  16. tr.LazyPrintf("%d", 1)
  17. tr.SetRecycler(func(_ interface{}) {})
  18. tr.SetTraceInfo(3, 4)
  19. tr.SetMaxEvents(100)
  20. tr.SetError()
  21. tr.Finish()
  22. tr.(*trace).reset()
  23. if !reflect.DeepEqual(tr, new(trace)) {
  24. t.Errorf("reset didn't clear all fields: %+v", tr)
  25. }
  26. }
  27. // TestResetLog checks whether all the fields are zeroed after reset.
  28. func TestResetLog(t *testing.T) {
  29. el := NewEventLog("foo", "bar")
  30. el.Printf("message")
  31. el.Errorf("error")
  32. el.Finish()
  33. el.(*eventLog).reset()
  34. if !reflect.DeepEqual(el, new(eventLog)) {
  35. t.Errorf("reset didn't clear all fields: %+v", el)
  36. }
  37. }
  38. func TestAuthRequest(t *testing.T) {
  39. testCases := []struct {
  40. host string
  41. want bool
  42. }{
  43. {host: "192.168.23.1", want: false},
  44. {host: "192.168.23.1:8080", want: false},
  45. {host: "malformed remote addr", want: false},
  46. {host: "localhost", want: true},
  47. {host: "localhost:8080", want: true},
  48. {host: "127.0.0.1", want: true},
  49. {host: "127.0.0.1:8080", want: true},
  50. {host: "::1", want: true},
  51. {host: "[::1]:8080", want: true},
  52. }
  53. for _, tt := range testCases {
  54. req := &http.Request{RemoteAddr: tt.host}
  55. any, sensitive := AuthRequest(req)
  56. if any != tt.want || sensitive != tt.want {
  57. t.Errorf("AuthRequest(%q) = %t, %t; want %t, %t", tt.host, any, sensitive, tt.want, tt.want)
  58. }
  59. }
  60. }
  61. func benchmarkTrace(b *testing.B, maxEvents, numEvents int) {
  62. numSpans := (b.N + numEvents + 1) / numEvents
  63. for i := 0; i < numSpans; i++ {
  64. tr := New("test", "test")
  65. tr.SetMaxEvents(maxEvents)
  66. for j := 0; j < numEvents; j++ {
  67. tr.LazyPrintf("%d", j)
  68. }
  69. tr.Finish()
  70. }
  71. }
  72. func BenchmarkTrace_Default_2(b *testing.B) {
  73. benchmarkTrace(b, 0, 2)
  74. }
  75. func BenchmarkTrace_Default_10(b *testing.B) {
  76. benchmarkTrace(b, 0, 10)
  77. }
  78. func BenchmarkTrace_Default_100(b *testing.B) {
  79. benchmarkTrace(b, 0, 100)
  80. }
  81. func BenchmarkTrace_Default_1000(b *testing.B) {
  82. benchmarkTrace(b, 0, 1000)
  83. }
  84. func BenchmarkTrace_Default_10000(b *testing.B) {
  85. benchmarkTrace(b, 0, 10000)
  86. }
  87. func BenchmarkTrace_10_2(b *testing.B) {
  88. benchmarkTrace(b, 10, 2)
  89. }
  90. func BenchmarkTrace_10_10(b *testing.B) {
  91. benchmarkTrace(b, 10, 10)
  92. }
  93. func BenchmarkTrace_10_100(b *testing.B) {
  94. benchmarkTrace(b, 10, 100)
  95. }
  96. func BenchmarkTrace_10_1000(b *testing.B) {
  97. benchmarkTrace(b, 10, 1000)
  98. }
  99. func BenchmarkTrace_10_10000(b *testing.B) {
  100. benchmarkTrace(b, 10, 10000)
  101. }
  102. func BenchmarkTrace_100_2(b *testing.B) {
  103. benchmarkTrace(b, 100, 2)
  104. }
  105. func BenchmarkTrace_100_10(b *testing.B) {
  106. benchmarkTrace(b, 100, 10)
  107. }
  108. func BenchmarkTrace_100_100(b *testing.B) {
  109. benchmarkTrace(b, 100, 100)
  110. }
  111. func BenchmarkTrace_100_1000(b *testing.B) {
  112. benchmarkTrace(b, 100, 1000)
  113. }
  114. func BenchmarkTrace_100_10000(b *testing.B) {
  115. benchmarkTrace(b, 100, 10000)
  116. }
  117. func BenchmarkTrace_1000_2(b *testing.B) {
  118. benchmarkTrace(b, 1000, 2)
  119. }
  120. func BenchmarkTrace_1000_10(b *testing.B) {
  121. benchmarkTrace(b, 1000, 10)
  122. }
  123. func BenchmarkTrace_1000_100(b *testing.B) {
  124. benchmarkTrace(b, 1000, 100)
  125. }
  126. func BenchmarkTrace_1000_1000(b *testing.B) {
  127. benchmarkTrace(b, 1000, 1000)
  128. }
  129. func BenchmarkTrace_1000_10000(b *testing.B) {
  130. benchmarkTrace(b, 1000, 10000)
  131. }