github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/net/trace/trace_test.go (about)

     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  
     5  package trace
     6  
     7  import (
     8  	"reflect"
     9  	"testing"
    10  
    11  	http "github.com/hxx258456/ccgo/gmhttp"
    12  )
    13  
    14  type s struct{}
    15  
    16  func (s) String() string { return "lazy string" }
    17  
    18  // TestReset checks whether all the fields are zeroed after reset.
    19  func TestReset(t *testing.T) {
    20  	tr := New("foo", "bar")
    21  	tr.LazyLog(s{}, false)
    22  	tr.LazyPrintf("%d", 1)
    23  	tr.SetRecycler(func(_ interface{}) {})
    24  	tr.SetTraceInfo(3, 4)
    25  	tr.SetMaxEvents(100)
    26  	tr.SetError()
    27  	tr.Finish()
    28  
    29  	tr.(*trace).reset()
    30  
    31  	if !reflect.DeepEqual(tr, new(trace)) {
    32  		t.Errorf("reset didn't clear all fields: %+v", tr)
    33  	}
    34  }
    35  
    36  // TestResetLog checks whether all the fields are zeroed after reset.
    37  func TestResetLog(t *testing.T) {
    38  	el := NewEventLog("foo", "bar")
    39  	el.Printf("message")
    40  	el.Errorf("error")
    41  	el.Finish()
    42  
    43  	el.(*eventLog).reset()
    44  
    45  	if !reflect.DeepEqual(el, new(eventLog)) {
    46  		t.Errorf("reset didn't clear all fields: %+v", el)
    47  	}
    48  }
    49  
    50  func TestAuthRequest(t *testing.T) {
    51  	testCases := []struct {
    52  		host string
    53  		want bool
    54  	}{
    55  		{host: "192.168.23.1", want: false},
    56  		{host: "192.168.23.1:8080", want: false},
    57  		{host: "malformed remote addr", want: false},
    58  		{host: "localhost", want: true},
    59  		{host: "localhost:8080", want: true},
    60  		{host: "127.0.0.1", want: true},
    61  		{host: "127.0.0.1:8080", want: true},
    62  		{host: "::1", want: true},
    63  		{host: "[::1]:8080", want: true},
    64  	}
    65  	for _, tt := range testCases {
    66  		req := &http.Request{RemoteAddr: tt.host}
    67  		any, sensitive := AuthRequest(req)
    68  		if any != tt.want || sensitive != tt.want {
    69  			t.Errorf("AuthRequest(%q) = %t, %t; want %t, %t", tt.host, any, sensitive, tt.want, tt.want)
    70  		}
    71  	}
    72  }
    73  
    74  // TestParseTemplate checks that all templates used by this package are valid
    75  // as they are parsed on first usage
    76  func TestParseTemplate(t *testing.T) {
    77  	if tmpl := distTmpl(); tmpl == nil {
    78  		t.Error("invalid template returned from distTmpl()")
    79  	}
    80  	if tmpl := pageTmpl(); tmpl == nil {
    81  		t.Error("invalid template returned from pageTmpl()")
    82  	}
    83  	if tmpl := eventsTmpl(); tmpl == nil {
    84  		t.Error("invalid template returned from eventsTmpl()")
    85  	}
    86  }
    87  
    88  func benchmarkTrace(b *testing.B, maxEvents, numEvents int) {
    89  	numSpans := (b.N + numEvents + 1) / numEvents
    90  
    91  	for i := 0; i < numSpans; i++ {
    92  		tr := New("test", "test")
    93  		tr.SetMaxEvents(maxEvents)
    94  		for j := 0; j < numEvents; j++ {
    95  			tr.LazyPrintf("%d", j)
    96  		}
    97  		tr.Finish()
    98  	}
    99  }
   100  
   101  func BenchmarkTrace_Default_2(b *testing.B) {
   102  	benchmarkTrace(b, 0, 2)
   103  }
   104  
   105  func BenchmarkTrace_Default_10(b *testing.B) {
   106  	benchmarkTrace(b, 0, 10)
   107  }
   108  
   109  func BenchmarkTrace_Default_100(b *testing.B) {
   110  	benchmarkTrace(b, 0, 100)
   111  }
   112  
   113  func BenchmarkTrace_Default_1000(b *testing.B) {
   114  	benchmarkTrace(b, 0, 1000)
   115  }
   116  
   117  func BenchmarkTrace_Default_10000(b *testing.B) {
   118  	benchmarkTrace(b, 0, 10000)
   119  }
   120  
   121  func BenchmarkTrace_10_2(b *testing.B) {
   122  	benchmarkTrace(b, 10, 2)
   123  }
   124  
   125  func BenchmarkTrace_10_10(b *testing.B) {
   126  	benchmarkTrace(b, 10, 10)
   127  }
   128  
   129  func BenchmarkTrace_10_100(b *testing.B) {
   130  	benchmarkTrace(b, 10, 100)
   131  }
   132  
   133  func BenchmarkTrace_10_1000(b *testing.B) {
   134  	benchmarkTrace(b, 10, 1000)
   135  }
   136  
   137  func BenchmarkTrace_10_10000(b *testing.B) {
   138  	benchmarkTrace(b, 10, 10000)
   139  }
   140  
   141  func BenchmarkTrace_100_2(b *testing.B) {
   142  	benchmarkTrace(b, 100, 2)
   143  }
   144  
   145  func BenchmarkTrace_100_10(b *testing.B) {
   146  	benchmarkTrace(b, 100, 10)
   147  }
   148  
   149  func BenchmarkTrace_100_100(b *testing.B) {
   150  	benchmarkTrace(b, 100, 100)
   151  }
   152  
   153  func BenchmarkTrace_100_1000(b *testing.B) {
   154  	benchmarkTrace(b, 100, 1000)
   155  }
   156  
   157  func BenchmarkTrace_100_10000(b *testing.B) {
   158  	benchmarkTrace(b, 100, 10000)
   159  }
   160  
   161  func BenchmarkTrace_1000_2(b *testing.B) {
   162  	benchmarkTrace(b, 1000, 2)
   163  }
   164  
   165  func BenchmarkTrace_1000_10(b *testing.B) {
   166  	benchmarkTrace(b, 1000, 10)
   167  }
   168  
   169  func BenchmarkTrace_1000_100(b *testing.B) {
   170  	benchmarkTrace(b, 1000, 100)
   171  }
   172  
   173  func BenchmarkTrace_1000_1000(b *testing.B) {
   174  	benchmarkTrace(b, 1000, 1000)
   175  }
   176  
   177  func BenchmarkTrace_1000_10000(b *testing.B) {
   178  	benchmarkTrace(b, 1000, 10000)
   179  }