github.com/kaituanwang/hyperledger@v2.0.1+incompatible/common/metrics/statsd/goruntime/collector_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package goruntime_test
     8  
     9  import (
    10  	"strings"
    11  	"time"
    12  
    13  	"github.com/hyperledger/fabric/common/metrics"
    14  	"github.com/hyperledger/fabric/common/metrics/metricsfakes"
    15  	"github.com/hyperledger/fabric/common/metrics/statsd/goruntime"
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  )
    19  
    20  var _ = Describe("Collector", func() {
    21  	var (
    22  		fakeProvider *metricsfakes.Provider
    23  		fakeGauges   map[string]*metricsfakes.Gauge
    24  
    25  		collector *goruntime.Collector
    26  	)
    27  
    28  	BeforeEach(func() {
    29  		fakeGauges = map[string]*metricsfakes.Gauge{}
    30  		fakeProvider = &metricsfakes.Provider{}
    31  		fakeProvider.NewGaugeStub = func(o metrics.GaugeOpts) metrics.Gauge {
    32  			name := strings.Join([]string{o.Namespace, o.Subsystem, o.Name}, ".")
    33  			_, ok := fakeGauges[name]
    34  			if !ok {
    35  				fakeGauges[name] = &metricsfakes.Gauge{}
    36  			}
    37  			return fakeGauges[name]
    38  		}
    39  
    40  		collector = goruntime.NewCollector(fakeProvider)
    41  	})
    42  
    43  	It("constructs a collector with the approriate gauges", func() {
    44  		Expect(fakeProvider.NewGaugeCallCount()).To(Equal(27))
    45  	})
    46  
    47  	It("acquires runtime statistics", func() {
    48  		// TODO: statistics are a bit difficult to test
    49  		stats := goruntime.CollectStats()
    50  		Expect(stats).NotTo(BeZero())
    51  	})
    52  
    53  	It("collects and publishes statistics", func() {
    54  		ticks := make(chan time.Time, 3)
    55  		ticks <- time.Now()
    56  		ticks <- time.Now().Add(time.Second)
    57  		ticks <- time.Now().Add(2 * time.Second)
    58  		close(ticks)
    59  
    60  		collector.CollectAndPublish(ticks)
    61  		for _, gauge := range fakeGauges {
    62  			Expect(gauge.SetCallCount()).To(Equal(3))
    63  		}
    64  	})
    65  
    66  	Describe("Publish", func() {
    67  		var stats goruntime.Stats
    68  
    69  		BeforeEach(func() {
    70  			stats = goruntime.Stats{}
    71  		})
    72  
    73  		It("publishes CgoCalls as go.cgo_calls", func() {
    74  			stats.CgoCalls = 1
    75  			collector.Publish(stats)
    76  
    77  			Expect(fakeGauges["go..cgo_calls"]).NotTo(BeNil())
    78  			Expect(fakeGauges["go..cgo_calls"].SetCallCount()).To(Equal(1))
    79  			Expect(fakeGauges["go..cgo_calls"].SetArgsForCall(0)).To(Equal(float64(1)))
    80  		})
    81  
    82  		It("publishes GoRoutines as go.goroutine_count", func() {
    83  			stats.GoRoutines = 2
    84  			collector.Publish(stats)
    85  
    86  			Expect(fakeGauges["go..goroutine_count"]).NotTo(BeNil())
    87  			Expect(fakeGauges["go..goroutine_count"].SetCallCount()).To(Equal(1))
    88  			Expect(fakeGauges["go..goroutine_count"].SetArgsForCall(0)).To(Equal(float64(2)))
    89  		})
    90  
    91  		It("publishes ThreadsCreated as go.threads_created", func() {
    92  			stats.ThreadsCreated = 3
    93  			collector.Publish(stats)
    94  
    95  			Expect(fakeGauges["go..threads_created"]).NotTo(BeNil())
    96  			Expect(fakeGauges["go..threads_created"].SetCallCount()).To(Equal(1))
    97  			Expect(fakeGauges["go..threads_created"].SetArgsForCall(0)).To(Equal(float64(3)))
    98  		})
    99  
   100  		It("publishes HeapAlloc as go.mem.heap_alloc_bytes", func() {
   101  			stats.MemStats.HeapAlloc = 4
   102  			collector.Publish(stats)
   103  
   104  			Expect(fakeGauges["go.mem.heap_alloc_bytes"]).NotTo(BeNil())
   105  			Expect(fakeGauges["go.mem.heap_alloc_bytes"].SetCallCount()).To(Equal(1))
   106  			Expect(fakeGauges["go.mem.heap_alloc_bytes"].SetArgsForCall(0)).To(Equal(float64(4)))
   107  		})
   108  
   109  		It("publishes TotalAlloc as go.mem.heap.total_alloc_bytes", func() {
   110  			stats.MemStats.TotalAlloc = 5
   111  			collector.Publish(stats)
   112  
   113  			Expect(fakeGauges["go.mem.heap_total_alloc_bytes"]).NotTo(BeNil())
   114  			Expect(fakeGauges["go.mem.heap_total_alloc_bytes"].SetCallCount()).To(Equal(1))
   115  			Expect(fakeGauges["go.mem.heap_total_alloc_bytes"].SetArgsForCall(0)).To(Equal(float64(5)))
   116  		})
   117  
   118  		It("publishes Mallocs as go.mem.heap.heap_malloc_count", func() {
   119  			stats.MemStats.Mallocs = 6
   120  			collector.Publish(stats)
   121  
   122  			Expect(fakeGauges["go.mem.heap_malloc_count"]).NotTo(BeNil())
   123  			Expect(fakeGauges["go.mem.heap_malloc_count"].SetCallCount()).To(Equal(1))
   124  			Expect(fakeGauges["go.mem.heap_malloc_count"].SetArgsForCall(0)).To(Equal(float64(6)))
   125  		})
   126  
   127  		It("publishes Frees as go.mem.heap_free_count", func() {
   128  			stats.MemStats.Frees = 7
   129  			collector.Publish(stats)
   130  
   131  			Expect(fakeGauges["go.mem.heap_free_count"]).NotTo(BeNil())
   132  			Expect(fakeGauges["go.mem.heap_free_count"].SetCallCount()).To(Equal(1))
   133  			Expect(fakeGauges["go.mem.heap_free_count"].SetArgsForCall(0)).To(Equal(float64(7)))
   134  		})
   135  
   136  		It("publishes HeapSys as go.mem.heap_sys_bytes", func() {
   137  			stats.MemStats.HeapSys = 8
   138  			collector.Publish(stats)
   139  
   140  			Expect(fakeGauges["go.mem.heap_sys_bytes"]).NotTo(BeNil())
   141  			Expect(fakeGauges["go.mem.heap_sys_bytes"].SetCallCount()).To(Equal(1))
   142  			Expect(fakeGauges["go.mem.heap_sys_bytes"].SetArgsForCall(0)).To(Equal(float64(8)))
   143  		})
   144  
   145  		It("publishes HeapSys as go.mem.heap_idle_bytes", func() {
   146  			stats.MemStats.HeapIdle = 9
   147  			collector.Publish(stats)
   148  
   149  			Expect(fakeGauges["go.mem.heap_idle_bytes"]).NotTo(BeNil())
   150  			Expect(fakeGauges["go.mem.heap_idle_bytes"].SetCallCount()).To(Equal(1))
   151  			Expect(fakeGauges["go.mem.heap_idle_bytes"].SetArgsForCall(0)).To(Equal(float64(9)))
   152  		})
   153  
   154  		It("publishes HeapInuse as go.mem.heap_inuse_bytes", func() {
   155  			stats.MemStats.HeapInuse = 10
   156  			collector.Publish(stats)
   157  
   158  			Expect(fakeGauges["go.mem.heap_inuse_bytes"]).NotTo(BeNil())
   159  			Expect(fakeGauges["go.mem.heap_inuse_bytes"].SetCallCount()).To(Equal(1))
   160  			Expect(fakeGauges["go.mem.heap_inuse_bytes"].SetArgsForCall(0)).To(Equal(float64(10)))
   161  		})
   162  
   163  		It("publishes HeapReleased as go.mem.heap_released_bytes", func() {
   164  			stats.MemStats.HeapReleased = 11
   165  			collector.Publish(stats)
   166  
   167  			Expect(fakeGauges["go.mem.heap_released_bytes"]).NotTo(BeNil())
   168  			Expect(fakeGauges["go.mem.heap_released_bytes"].SetCallCount()).To(Equal(1))
   169  			Expect(fakeGauges["go.mem.heap_released_bytes"].SetArgsForCall(0)).To(Equal(float64(11)))
   170  		})
   171  
   172  		It("publishes HeapObjects as go.mem.heap_objects", func() {
   173  			stats.MemStats.HeapObjects = 12
   174  			collector.Publish(stats)
   175  
   176  			Expect(fakeGauges["go.mem.heap_objects"]).NotTo(BeNil())
   177  			Expect(fakeGauges["go.mem.heap_objects"].SetCallCount()).To(Equal(1))
   178  			Expect(fakeGauges["go.mem.heap_objects"].SetArgsForCall(0)).To(Equal(float64(12)))
   179  		})
   180  
   181  		It("publishes StackInuse as go.mem.stack_inuse_bytes", func() {
   182  			stats.MemStats.StackInuse = 13
   183  			collector.Publish(stats)
   184  
   185  			Expect(fakeGauges["go.mem.stack_inuse_bytes"]).NotTo(BeNil())
   186  			Expect(fakeGauges["go.mem.stack_inuse_bytes"].SetCallCount()).To(Equal(1))
   187  			Expect(fakeGauges["go.mem.stack_inuse_bytes"].SetArgsForCall(0)).To(Equal(float64(13)))
   188  		})
   189  
   190  		It("publishes StackSys as go.mem.stack_sys_bytes", func() {
   191  			stats.MemStats.StackSys = 14
   192  			collector.Publish(stats)
   193  
   194  			Expect(fakeGauges["go.mem.stack_sys_bytes"]).NotTo(BeNil())
   195  			Expect(fakeGauges["go.mem.stack_sys_bytes"].SetCallCount()).To(Equal(1))
   196  			Expect(fakeGauges["go.mem.stack_sys_bytes"].SetArgsForCall(0)).To(Equal(float64(14)))
   197  		})
   198  
   199  		It("publishes MSpanInuse as go.mem.mspan_inuse_bytes", func() {
   200  			stats.MemStats.MSpanInuse = 15
   201  			collector.Publish(stats)
   202  
   203  			Expect(fakeGauges["go.mem.mspan_inuse_bytes"]).NotTo(BeNil())
   204  			Expect(fakeGauges["go.mem.mspan_inuse_bytes"].SetCallCount()).To(Equal(1))
   205  			Expect(fakeGauges["go.mem.mspan_inuse_bytes"].SetArgsForCall(0)).To(Equal(float64(15)))
   206  		})
   207  
   208  		It("publishes MSpanSys as go.mem.mspan_sys_bytes", func() {
   209  			stats.MemStats.MSpanSys = 16
   210  			collector.Publish(stats)
   211  
   212  			Expect(fakeGauges["go.mem.mspan_sys_bytes"]).NotTo(BeNil())
   213  			Expect(fakeGauges["go.mem.mspan_sys_bytes"].SetCallCount()).To(Equal(1))
   214  			Expect(fakeGauges["go.mem.mspan_sys_bytes"].SetArgsForCall(0)).To(Equal(float64(16)))
   215  		})
   216  
   217  		It("publishes MCacheInuse as go.mem.mcache_inuse_bytes", func() {
   218  			stats.MemStats.MCacheInuse = 17
   219  			collector.Publish(stats)
   220  
   221  			Expect(fakeGauges["go.mem.mcache_inuse_bytes"]).NotTo(BeNil())
   222  			Expect(fakeGauges["go.mem.mcache_inuse_bytes"].SetCallCount()).To(Equal(1))
   223  			Expect(fakeGauges["go.mem.mcache_inuse_bytes"].SetArgsForCall(0)).To(Equal(float64(17)))
   224  		})
   225  
   226  		It("publishes MCacheInuse as go.mem.mcache_sys_bytes", func() {
   227  			stats.MemStats.MCacheSys = 18
   228  			collector.Publish(stats)
   229  
   230  			Expect(fakeGauges["go.mem.mcache_sys_bytes"]).NotTo(BeNil())
   231  			Expect(fakeGauges["go.mem.mcache_sys_bytes"].SetCallCount()).To(Equal(1))
   232  			Expect(fakeGauges["go.mem.mcache_sys_bytes"].SetArgsForCall(0)).To(Equal(float64(18)))
   233  		})
   234  
   235  		It("publishes BuckHashSys as go.mem.buckethash_sys_bytes", func() {
   236  			stats.MemStats.BuckHashSys = 19
   237  			collector.Publish(stats)
   238  
   239  			Expect(fakeGauges["go.mem.buckethash_sys_bytes"]).NotTo(BeNil())
   240  			Expect(fakeGauges["go.mem.buckethash_sys_bytes"].SetCallCount()).To(Equal(1))
   241  			Expect(fakeGauges["go.mem.buckethash_sys_bytes"].SetArgsForCall(0)).To(Equal(float64(19)))
   242  		})
   243  
   244  		It("publishes GCSys as go.mem.gc_sys_bytes", func() {
   245  			stats.MemStats.GCSys = 20
   246  			collector.Publish(stats)
   247  
   248  			Expect(fakeGauges["go.mem.gc_sys_bytes"]).NotTo(BeNil())
   249  			Expect(fakeGauges["go.mem.gc_sys_bytes"].SetCallCount()).To(Equal(1))
   250  			Expect(fakeGauges["go.mem.gc_sys_bytes"].SetArgsForCall(0)).To(Equal(float64(20)))
   251  		})
   252  
   253  		It("publishes OtherSys as go.mem.other_sys_bytes", func() {
   254  			stats.MemStats.OtherSys = 21
   255  			collector.Publish(stats)
   256  
   257  			Expect(fakeGauges["go.mem.other_sys_bytes"]).NotTo(BeNil())
   258  			Expect(fakeGauges["go.mem.other_sys_bytes"].SetCallCount()).To(Equal(1))
   259  			Expect(fakeGauges["go.mem.other_sys_bytes"].SetArgsForCall(0)).To(Equal(float64(21)))
   260  		})
   261  
   262  		It("publishes NextGC as go.mem.gc_next_bytes", func() {
   263  			stats.MemStats.NextGC = 22
   264  			collector.Publish(stats)
   265  
   266  			Expect(fakeGauges["go.mem.gc_next_bytes"]).NotTo(BeNil())
   267  			Expect(fakeGauges["go.mem.gc_next_bytes"].SetCallCount()).To(Equal(1))
   268  			Expect(fakeGauges["go.mem.gc_next_bytes"].SetArgsForCall(0)).To(Equal(float64(22)))
   269  		})
   270  
   271  		It("publishes LastGC as go.mem.gc.last_epoch_nanotime", func() {
   272  			stats.MemStats.LastGC = 23
   273  			collector.Publish(stats)
   274  
   275  			Expect(fakeGauges["go.mem.gc_last_epoch_nanotime"]).NotTo(BeNil())
   276  			Expect(fakeGauges["go.mem.gc_last_epoch_nanotime"].SetCallCount()).To(Equal(1))
   277  			Expect(fakeGauges["go.mem.gc_last_epoch_nanotime"].SetArgsForCall(0)).To(Equal(float64(23)))
   278  		})
   279  
   280  		It("publishes PauseTotalNs as go.mem.gc_pause_total_ns", func() {
   281  			stats.MemStats.PauseTotalNs = 24
   282  			collector.Publish(stats)
   283  
   284  			Expect(fakeGauges["go.mem.gc_pause_total_ns"]).NotTo(BeNil())
   285  			Expect(fakeGauges["go.mem.gc_pause_total_ns"].SetCallCount()).To(Equal(1))
   286  			Expect(fakeGauges["go.mem.gc_pause_total_ns"].SetArgsForCall(0)).To(Equal(float64(24)))
   287  		})
   288  
   289  		It("publishes PauseNs as go.mem.gc_pause_last_ns", func() {
   290  			stats.MemStats.NumGC = 3
   291  			stats.MemStats.PauseNs[2] = 25
   292  			collector.Publish(stats)
   293  
   294  			Expect(fakeGauges["go.mem.gc_pause_last_ns"]).NotTo(BeNil())
   295  			Expect(fakeGauges["go.mem.gc_pause_last_ns"].SetCallCount()).To(Equal(1))
   296  			Expect(fakeGauges["go.mem.gc_pause_last_ns"].SetArgsForCall(0)).To(Equal(float64(25)))
   297  		})
   298  
   299  		It("publishes NumGC as go.mem.gc_completed_count", func() {
   300  			stats.MemStats.NumGC = 27
   301  			collector.Publish(stats)
   302  
   303  			Expect(fakeGauges["go.mem.gc_completed_count"]).NotTo(BeNil())
   304  			Expect(fakeGauges["go.mem.gc_completed_count"].SetCallCount()).To(Equal(1))
   305  			Expect(fakeGauges["go.mem.gc_completed_count"].SetArgsForCall(0)).To(Equal(float64(27)))
   306  		})
   307  
   308  		It("publishes NumForcedGC as go.mem.gc_forced_count", func() {
   309  			stats.MemStats.NumForcedGC = 28
   310  			collector.Publish(stats)
   311  
   312  			Expect(fakeGauges["go.mem.gc_forced_count"]).NotTo(BeNil())
   313  			Expect(fakeGauges["go.mem.gc_forced_count"].SetCallCount()).To(Equal(1))
   314  			Expect(fakeGauges["go.mem.gc_forced_count"].SetArgsForCall(0)).To(Equal(float64(28)))
   315  		})
   316  	})
   317  })