Simplified stats a bit
This commit is contained in:
parent
ec29a2d57e
commit
732b64efb8
|
@ -2,19 +2,16 @@ package stats
|
|||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/divan/graphx/graph"
|
||||
"github.com/status-im/simulation/propagation"
|
||||
)
|
||||
|
||||
// Stats represents stats data for given simulation log.
|
||||
type Stats struct {
|
||||
NodeHits map[string]int
|
||||
NodeHits map[int]int
|
||||
NodeCoverage Coverage
|
||||
LinkCoverage Coverage
|
||||
Hist *Histogram
|
||||
Time time.Duration
|
||||
}
|
||||
|
||||
|
@ -25,62 +22,41 @@ func (s *Stats) PrintVerbose() {
|
|||
fmt.Println("Time elapsed:", s.Time)
|
||||
fmt.Println("Nodes coverage:", s.NodeCoverage)
|
||||
fmt.Println("Links coverage:", s.LinkCoverage)
|
||||
fmt.Println("Histogram:")
|
||||
s.Hist.Print()
|
||||
}
|
||||
|
||||
// Analyze analyzes given propagation log and returns filled Stats object.
|
||||
func Analyze(g *graph.Graph, plog *propagation.Log) *Stats {
|
||||
func Analyze(plog *propagation.Log, nodeCount, linkCount int) *Stats {
|
||||
t := analyzeTiming(plog)
|
||||
nodeHits, hist := analyzeNodeHits(g, plog)
|
||||
nodeCoverage := analyzeNodeCoverage(g, nodeHits)
|
||||
linkCoverage := analyzeLinkCoverage(g, plog)
|
||||
nodeHits := analyzeNodeHits(plog)
|
||||
nodeCoverage := analyzeNodeCoverage(nodeHits, nodeCount)
|
||||
linkCoverage := analyzeLinkCoverage(plog, linkCount)
|
||||
|
||||
return &Stats{
|
||||
NodeHits: nodeHits,
|
||||
NodeCoverage: nodeCoverage,
|
||||
LinkCoverage: linkCoverage,
|
||||
Hist: hist,
|
||||
Time: t,
|
||||
}
|
||||
}
|
||||
|
||||
func analyzeNodeHits(g *graph.Graph, plog *propagation.Log) (map[string]int, *Histogram) {
|
||||
nodeHits := make(map[string]int)
|
||||
func analyzeNodeHits(plog *propagation.Log) map[int]int {
|
||||
nodeHits := make(map[int]int)
|
||||
|
||||
for _, nodes := range plog.Nodes {
|
||||
for _, j := range nodes {
|
||||
id, err := g.NodeIDByIdx(j)
|
||||
if err != nil {
|
||||
log.Fatal("Stats:", err)
|
||||
}
|
||||
nodeHits[id]++
|
||||
nodeHits[j]++
|
||||
}
|
||||
}
|
||||
|
||||
hist := NewHistogram(HistogramOptions{
|
||||
NumBuckets: 16,
|
||||
GrowthFactor: 0.2,
|
||||
MinValue: 1,
|
||||
})
|
||||
for key, v := range nodeHits {
|
||||
fmt.Println("Node:", key, v)
|
||||
err := hist.Add(int64(v))
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
}
|
||||
}
|
||||
|
||||
return nodeHits, hist
|
||||
return nodeHits
|
||||
}
|
||||
|
||||
func analyzeNodeCoverage(g *graph.Graph, nodeHits map[string]int) Coverage {
|
||||
func analyzeNodeCoverage(nodeHits map[int]int, total int) Coverage {
|
||||
actual := len(nodeHits)
|
||||
total := len(g.Nodes())
|
||||
return NewCoverage(actual, total)
|
||||
}
|
||||
|
||||
func analyzeLinkCoverage(g *graph.Graph, plog *propagation.Log) Coverage {
|
||||
func analyzeLinkCoverage(plog *propagation.Log, total int) Coverage {
|
||||
linkHits := make(map[int]struct{})
|
||||
for _, links := range plog.Indices {
|
||||
for _, j := range links {
|
||||
|
@ -89,7 +65,6 @@ func analyzeLinkCoverage(g *graph.Graph, plog *propagation.Log) Coverage {
|
|||
}
|
||||
|
||||
actual := len(linkHits)
|
||||
total := len(g.Links())
|
||||
return NewCoverage(actual, total)
|
||||
}
|
||||
|
||||
|
|
|
@ -12,7 +12,7 @@ type node string
|
|||
|
||||
func (n node) ID() string { return string(n) }
|
||||
|
||||
func TestAnalyze(t *testing.T) {
|
||||
func testGraph() *graph.Graph {
|
||||
g := graph.NewGraph()
|
||||
g.AddNode(node("0"))
|
||||
g.AddNode(node("1"))
|
||||
|
@ -23,6 +23,11 @@ func TestAnalyze(t *testing.T) {
|
|||
g.AddLink("1", "2")
|
||||
g.AddLink("2", "0")
|
||||
g.AddLink("0", "3")
|
||||
return g
|
||||
}
|
||||
|
||||
func TestAnalyze(t *testing.T) {
|
||||
g := testGraph()
|
||||
|
||||
// example propagation log
|
||||
// three timestamps: 10, 20 and 30 ms
|
||||
|
@ -36,7 +41,7 @@ func TestAnalyze(t *testing.T) {
|
|||
},
|
||||
}
|
||||
|
||||
stats := Analyze(g, plog)
|
||||
stats := Analyze(plog, len(g.Nodes()), len(g.Links()))
|
||||
|
||||
expected := []struct {
|
||||
name string
|
||||
|
@ -48,11 +53,33 @@ func TestAnalyze(t *testing.T) {
|
|||
{"3", 1},
|
||||
}
|
||||
|
||||
for _, node := range expected {
|
||||
got := stats.NodeHits[node.name]
|
||||
for i, node := range expected {
|
||||
got := stats.NodeHits[i]
|
||||
if got != node.hits {
|
||||
t.Fatalf("Expected node '%s' to be hit %d times, but got %d", node.name, node.hits, got)
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkAnalyze(b *testing.B) {
|
||||
g := testGraph()
|
||||
|
||||
// example propagation log
|
||||
// three timestamps: 10, 20 and 30 ms
|
||||
// with first node hit 1 time, second and third - 3 times
|
||||
plog := &propagation.Log{
|
||||
Timestamps: []int{10, 20, 30},
|
||||
Nodes: [][]int{
|
||||
[]int{0, 1, 2},
|
||||
[]int{1, 2},
|
||||
[]int{2, 1, 3},
|
||||
},
|
||||
}
|
||||
|
||||
nodeCount := len(g.Nodes())
|
||||
linksCount := len(g.Links())
|
||||
for i := 0; i < b.N; i++ {
|
||||
Analyze(plog, nodeCount, linksCount)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue