github.com/decred/dcrlnd@v0.7.6/routing/stats.go (about)

     1  package routing
     2  
     3  import (
     4  	"fmt"
     5  	"sync"
     6  	"time"
     7  )
     8  
     9  // routerStats is a struct that tracks various updates to the graph and
    10  // facilitates aggregate logging of the statistics.
    11  type routerStats struct {
    12  	numChannels uint32
    13  	numUpdates  uint32
    14  	numNodes    uint32
    15  	lastReset   time.Time
    16  
    17  	mu sync.RWMutex
    18  }
    19  
    20  // incNumEdges increments the number of discovered edges.
    21  func (g *routerStats) incNumEdgesDiscovered() {
    22  	g.mu.Lock()
    23  	g.numChannels++
    24  	g.mu.Unlock()
    25  }
    26  
    27  // incNumUpdates increments the number of channel updates processed.
    28  func (g *routerStats) incNumChannelUpdates() {
    29  	g.mu.Lock()
    30  	g.numUpdates++
    31  	g.mu.Unlock()
    32  }
    33  
    34  // incNumNodeUpdates increments the number of node updates processed.
    35  func (g *routerStats) incNumNodeUpdates() {
    36  	g.mu.Lock()
    37  	g.numNodes++
    38  	g.mu.Unlock()
    39  }
    40  
    41  // Empty returns true if all stats are zero.
    42  func (g *routerStats) Empty() bool {
    43  	g.mu.RLock()
    44  	isEmpty := g.numChannels == 0 &&
    45  		g.numUpdates == 0 &&
    46  		g.numNodes == 0
    47  	g.mu.RUnlock()
    48  	return isEmpty
    49  }
    50  
    51  // Reset clears any router stats and sets the lastReset field to now.
    52  func (g *routerStats) Reset() {
    53  	g.mu.Lock()
    54  	g.numChannels = 0
    55  	g.numUpdates = 0
    56  	g.numNodes = 0
    57  	g.lastReset = time.Now()
    58  	g.mu.Unlock()
    59  }
    60  
    61  // String returns a human-readable description of the router stats.
    62  func (g *routerStats) String() string {
    63  	g.mu.RLock()
    64  	str := fmt.Sprintf("Processed channels=%d updates=%d nodes=%d in "+
    65  		"last %v", g.numChannels, g.numUpdates, g.numNodes,
    66  		time.Since(g.lastReset))
    67  	g.mu.RUnlock()
    68  	return str
    69  }