github.com/XiaoMi/Gaea@v1.2.5/stats/timings_test.go (about)

     1  /*
     2  Copyright 2017 Google Inc.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package stats
    18  
    19  import (
    20  	"expvar"
    21  	"strings"
    22  	"testing"
    23  	"time"
    24  )
    25  
    26  func TestTimings(t *testing.T) {
    27  	clear()
    28  	tm := NewTimings("timings1", "help", "category")
    29  	tm.Add("tag1", 500*time.Microsecond)
    30  	tm.Add("tag1", 1*time.Millisecond)
    31  	tm.Add("tag2", 1*time.Millisecond)
    32  	want := `{"TotalCount":3,"TotalTime":2500000,"Histograms":{"tag1":{"500000":1,"1000000":2,"5000000":2,"10000000":2,"50000000":2,"100000000":2,"500000000":2,"1000000000":2,"5000000000":2,"10000000000":2,"inf":2,"Count":2,"Time":1500000},"tag2":{"500000":0,"1000000":1,"5000000":1,"10000000":1,"50000000":1,"100000000":1,"500000000":1,"1000000000":1,"5000000000":1,"10000000000":1,"inf":1,"Count":1,"Time":1000000}}}`
    33  	if got := tm.String(); got != want {
    34  		t.Errorf("got %s, want %s", got, want)
    35  	}
    36  }
    37  
    38  func TestMultiTimings(t *testing.T) {
    39  	clear()
    40  	mtm := NewMultiTimings("maptimings1", "help", []string{"dim1", "dim2"})
    41  	mtm.Add([]string{"tag1a", "tag1b"}, 500*time.Microsecond)
    42  	mtm.Add([]string{"tag1a", "tag1b"}, 1*time.Millisecond)
    43  	mtm.Add([]string{"tag2a", "tag2b"}, 1*time.Millisecond)
    44  	want := `{"TotalCount":3,"TotalTime":2500000,"Histograms":{"tag1a.tag1b":{"500000":1,"1000000":2,"5000000":2,"10000000":2,"50000000":2,"100000000":2,"500000000":2,"1000000000":2,"5000000000":2,"10000000000":2,"inf":2,"Count":2,"Time":1500000},"tag2a.tag2b":{"500000":0,"1000000":1,"5000000":1,"10000000":1,"50000000":1,"100000000":1,"500000000":1,"1000000000":1,"5000000000":1,"10000000000":1,"inf":1,"Count":1,"Time":1000000}}}`
    45  	if got := mtm.String(); got != want {
    46  		t.Errorf("got %s, want %s", got, want)
    47  	}
    48  }
    49  
    50  func TestMultiTimingsDot(t *testing.T) {
    51  	clear()
    52  	mtm := NewMultiTimings("maptimings2", "help", []string{"label"})
    53  	mtm.Add([]string{"value.dot"}, 500*time.Microsecond)
    54  	safe := safeLabel("value.dot")
    55  	safeJSON := strings.Replace(safe, "\\", "\\\\", -1)
    56  	want := `{"TotalCount":1,"TotalTime":500000,"Histograms":{"` + safeJSON + `":{"500000":1,"1000000":1,"5000000":1,"10000000":1,"50000000":1,"100000000":1,"500000000":1,"1000000000":1,"5000000000":1,"10000000000":1,"inf":1,"Count":1,"Time":500000}}}`
    57  	if got := mtm.String(); got != want {
    58  		t.Errorf("got %s, want %s", got, want)
    59  	}
    60  }
    61  
    62  func TestTimingsHook(t *testing.T) {
    63  	var gotname string
    64  	var gotv *Timings
    65  	clear()
    66  	Register(func(name string, v expvar.Var) {
    67  		gotname = name
    68  		gotv = v.(*Timings)
    69  	})
    70  
    71  	name := "timings2"
    72  	v := NewTimings(name, "help", "")
    73  	if gotname != name {
    74  		t.Errorf("got %q, want %q", gotname, name)
    75  	}
    76  	if gotv != v {
    77  		t.Errorf("got %#v, want %#v", gotv, v)
    78  	}
    79  }