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