Rename propagation interface

This commit is contained in:
Ivan Danyliuk 2018-04-30 22:03:33 +02:00
parent c587dce06c
commit 842ad42fc2
No known key found for this signature in database
GPG Key ID: 97ED33CE024E1DBF
7 changed files with 40 additions and 41 deletions

View File

@ -9,9 +9,9 @@ import (
"github.com/divan/graph-experiments/graph" "github.com/divan/graph-experiments/graph"
gethlog "github.com/ethereum/go-ethereum/log" gethlog "github.com/ethereum/go-ethereum/log"
"github.com/status-im/simulator/simulation" "github.com/status-im/simulator/propagation"
"github.com/status-im/simulator/simulation/naivep2p" "github.com/status-im/simulator/propagation/naivep2p"
"github.com/status-im/simulator/simulation/whisperv6" "github.com/status-im/simulator/propagation/whisperv6"
"github.com/status-im/simulator/stats" "github.com/status-im/simulator/stats"
) )
@ -38,7 +38,7 @@ func main() {
} }
defer fd.Close() defer fd.Close()
var sim simulation.Simulator var sim propagation.PropagationSimulator
switch *simType { switch *simType {
case "naivep2p": case "naivep2p":
sim = naivep2p.NewSimulator(data, *naiveP2PN, *naiveP2PDelay) sim = naivep2p.NewSimulator(data, *naiveP2PN, *naiveP2PDelay)

View File

@ -5,7 +5,7 @@ import (
"log" "log"
"time" "time"
"github.com/status-im/simulator/simulation" "github.com/status-im/simulator/propagation"
) )
// LogEntry defines the reporting log entry for one // LogEntry defines the reporting log entry for one
@ -30,10 +30,10 @@ func NewLogEntry(start time.Time, from, to int) LogEntry {
} }
} }
// LogEntries2PropagationLog converts raw slice of LogEntries to PropagationLog, // logEntries2PropagationLog converts raw slice of LogEntries to PropagationLog,
// aggregating by timestamps and converting nodes indices to link indices. // aggregating by timestamps and converting nodes indices to link indices.
// We expect that timestamps already bucketed into Nms groups. // We expect that timestamps already bucketed into Nms groups.
func (s *Simulator) LogEntries2PropagationLog(entries []*LogEntry) *simulation.Log { func (s *Simulator) logEntries2PropagationLog(entries []*LogEntry) *propagation.Log {
findLink := func(from, to int) int { findLink := func(from, to int) int {
links := s.data.Links() links := s.data.Links()
for i := range links { for i := range links {
@ -72,7 +72,7 @@ func (s *Simulator) LogEntries2PropagationLog(entries []*LogEntry) *simulation.L
tsnodes[entry.Ts] = nnodes tsnodes[entry.Ts] = nnodes
} }
var ret = &simulation.Log{ var ret = &propagation.Log{
Timestamps: make([]int, 0, len(tss)), Timestamps: make([]int, 0, len(tss)),
Indices: make([][]int, 0, len(tss)), Indices: make([][]int, 0, len(tss)),
Nodes: make([][]int, 0, len(tss)), Nodes: make([][]int, 0, len(tss)),

View File

@ -5,7 +5,7 @@ import (
"time" "time"
"github.com/divan/graph-experiments/graph" "github.com/divan/graph-experiments/graph"
"github.com/status-im/simulator/simulation" "github.com/status-im/simulator/propagation"
) )
// Simulator is responsible for running propagation simulation. // Simulator is responsible for running propagation simulation.
@ -46,12 +46,13 @@ func NewSimulator(data *graph.Graph, N int, delay time.Duration) *Simulator {
return sim return sim
} }
// Stop stops simulator and frees all resources if any. // Stop stops simulator and frees all resources if any. Implements propagation.PropagationSimulator.
func (s *Simulator) Stop() error { func (s *Simulator) Stop() error {
return nil return nil
} }
func (s *Simulator) SendMessage(startNodeIdx, ttl int) *simulation.Log { // SendMessage sends single message and tracks propagation. Implements propagation.PropagationSimulator.
func (s *Simulator) SendMessage(startNodeIdx, ttl int) *propagation.Log {
message := Message{ message := Message{
Content: "dummy", Content: "dummy",
TTL: ttl, TTL: ttl,
@ -71,7 +72,7 @@ func (s *Simulator) SendMessage(startNodeIdx, ttl int) *simulation.Log {
case val := <-s.reportCh: case val := <-s.reportCh:
ret = append(ret, &val) ret = append(ret, &val)
case <-done: case <-done:
return s.LogEntries2PropagationLog(ret) return s.logEntries2PropagationLog(ret)
} }
} }
} }

View File

@ -1,7 +1,7 @@
package simulation package propagation
// Simulator defines the simulators for message propagation within the graph. // PropagationSimulator defines the simulators for message propagation within the graph.
type Simulator interface { type PropagationSimulator interface {
SendMessage(idx, ttl int) *Log SendMessage(idx, ttl int) *Log
Stop() error Stop() error
} }

View File

@ -5,22 +5,22 @@ import (
"time" "time"
) )
// LogEntry defines the reporting log entry for one // logEntry defines the reporting log entry for one
// p2p message sending. // p2p message sending.
type LogEntry struct { type logEntry struct {
From int From int
To int To int
Ts time.Duration Ts time.Duration
} }
// String implements Stringer interface for LogEntry. // String implements Stringer interface for logEntry.
func (l LogEntry) String() string { func (l logEntry) String() string {
return fmt.Sprintf("%s: %d -> %d", l.Ts.String(), l.From, l.To) return fmt.Sprintf("%s: %d -> %d", l.Ts.String(), l.From, l.To)
} }
// NewLogEntry creates new log entry. // newlogEntry creates new log entry.
func NewLogEntry(start time.Time, from, to int) *LogEntry { func newlogEntry(start time.Time, from, to int) *logEntry {
return &LogEntry{ return &logEntry{
Ts: time.Since(start) / time.Millisecond, Ts: time.Since(start) / time.Millisecond,
From: from, From: from,
To: to, To: to,

View File

@ -14,11 +14,11 @@ import (
"github.com/ethereum/go-ethereum/p2p/simulations" "github.com/ethereum/go-ethereum/p2p/simulations"
"github.com/ethereum/go-ethereum/p2p/simulations/adapters" "github.com/ethereum/go-ethereum/p2p/simulations/adapters"
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6" whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
"github.com/status-im/simulator/simulation" "github.com/status-im/simulator/propagation"
) )
// Simulator simulates WhisperV6 message propagation through the // Simulator simulates WhisperV6 message propagation through the
// given p2p network. // given p2p network. Implements PropagationSimulator interface.
type Simulator struct { type Simulator struct {
data *graph.Graph data *graph.Graph
network *simulations.Network network *simulations.Network
@ -26,6 +26,7 @@ type Simulator struct {
} }
// NewSimulator intializes simulator for the given graph data. // NewSimulator intializes simulator for the given graph data.
// It uses defaults for PoW settings.
func NewSimulator(data *graph.Graph) *Simulator { func NewSimulator(data *graph.Graph) *Simulator {
rand.Seed(time.Now().UnixNano()) rand.Seed(time.Now().UnixNano())
@ -119,8 +120,8 @@ func (s *Simulator) Stop() error {
return nil return nil
} }
// SendMessage sends single message and tracks propagation. Implements simulator.Interface. // SendMessage sends single message and tracks propagation. Implements propagation.PropagationSimulator.
func (s *Simulator) SendMessage(startNodeIdx, ttl int) *simulation.Log { func (s *Simulator) SendMessage(startNodeIdx, ttl int) *propagation.Log {
node := s.network.Nodes[startNodeIdx] node := s.network.Nodes[startNodeIdx]
// the easiest way to send a message through the node is // the easiest way to send a message through the node is
@ -160,7 +161,7 @@ func (s *Simulator) SendMessage(startNodeIdx, ttl int) *simulation.Log {
subErr error subErr error
done bool done bool
count int count int
plog []*LogEntry plog []*logEntry
) )
for subErr == nil && !done { for subErr == nil && !done {
select { select {
@ -170,7 +171,7 @@ func (s *Simulator) SendMessage(startNodeIdx, ttl int) *simulation.Log {
if msg.Code == 1 && msg.Protocol == "shh" && msg.Received == false { if msg.Code == 1 && msg.Protocol == "shh" && msg.Received == false {
from := ncache[msg.One] from := ncache[msg.One]
to := ncache[msg.Other] to := ncache[msg.Other]
entry := NewLogEntry(start, from, to) entry := newlogEntry(start, from, to)
plog = append(plog, entry) plog = append(plog, entry)
count++ count++
} }
@ -188,13 +189,13 @@ func (s *Simulator) SendMessage(startNodeIdx, ttl int) *simulation.Log {
if subErr != nil { if subErr != nil {
log.Fatal("Failed to collect propagation info", subErr) log.Fatal("Failed to collect propagation info", subErr)
} }
return s.LogEntries2PropagationLog(plog) return s.logEntries2PropagationLog(plog)
} }
// LogEntries2PropagationLog converts raw slice of LogEntries to PropagationLog, // logEntries2PropagationLog converts raw slice of LogEntries to PropagationLog,
// aggregating by timestamps and converting nodes indices to link indices. // aggregating by timestamps and converting nodes indices to link indices.
// We expect that timestamps already bucketed into Nms groups. // We expect that timestamps already bucketed into Nms groups.
func (s *Simulator) LogEntries2PropagationLog(entries []*LogEntry) *simulation.Log { func (s *Simulator) logEntries2PropagationLog(entries []*logEntry) *propagation.Log {
links := s.data.Links() links := s.data.Links()
findLink := func(from, to int) int { findLink := func(from, to int) int {
for i := range links { for i := range links {
@ -233,7 +234,7 @@ func (s *Simulator) LogEntries2PropagationLog(entries []*LogEntry) *simulation.L
tsnodes[entry.Ts] = nnodes tsnodes[entry.Ts] = nnodes
} }
var ret = &simulation.Log{ var ret = &propagation.Log{
Timestamps: make([]int, 0, len(tss)), Timestamps: make([]int, 0, len(tss)),
Indices: make([][]int, 0, len(tss)), Indices: make([][]int, 0, len(tss)),
Nodes: make([][]int, 0, len(tss)), Nodes: make([][]int, 0, len(tss)),

View File

@ -4,10 +4,8 @@ import (
"fmt" "fmt"
"log" "log"
"v.io/x/ref/lib/stats/histogram"
"github.com/divan/graph-experiments/graph" "github.com/divan/graph-experiments/graph"
"github.com/status-im/simulator/simulation" "github.com/status-im/simulator/propagation"
) )
// Stats represents stats data for given simulation log. // Stats represents stats data for given simulation log.
@ -15,7 +13,7 @@ type Stats struct {
NodeHits map[string]int NodeHits map[string]int
NodeCoverage Coverage NodeCoverage Coverage
LinkCoverage Coverage LinkCoverage Coverage
Hist *histogram.Histogram Hist *Histogram
} }
// PrintVerbose prints detailed terminal-friendly stats to // PrintVerbose prints detailed terminal-friendly stats to
@ -24,11 +22,10 @@ func (s *Stats) PrintVerbose() {
fmt.Println("Stats:") fmt.Println("Stats:")
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("Node hits:", s.Hist.Value())
} }
// 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 *simulation.Log) *Stats { func Analyze(g *graph.Graph, plog *propagation.Log) *Stats {
nodeHits, hist := analyzeNodeHits(g, plog) nodeHits, hist := analyzeNodeHits(g, plog)
nodeCoverage := analyzeNodeCoverage(g, nodeHits) nodeCoverage := analyzeNodeCoverage(g, nodeHits)
linkCoverage := analyzeLinkCoverage(g, plog) linkCoverage := analyzeLinkCoverage(g, plog)
@ -41,7 +38,7 @@ func Analyze(g *graph.Graph, plog *simulation.Log) *Stats {
} }
} }
func analyzeNodeHits(g *graph.Graph, plog *simulation.Log) (map[string]int, *histogram.Histogram) { func analyzeNodeHits(g *graph.Graph, plog *propagation.Log) (map[string]int, *Histogram) {
nodeHits := make(map[string]int) nodeHits := make(map[string]int)
for _, nodes := range plog.Nodes { for _, nodes := range plog.Nodes {
@ -56,7 +53,7 @@ func analyzeNodeHits(g *graph.Graph, plog *simulation.Log) (map[string]int, *his
hist := NewHistogram(1, 1, 1) hist := NewHistogram(1, 1, 1)
for _, v := range nodeHits { for _, v := range nodeHits {
hist.Add(v) hist.Add(v, v)
} }
return nodeHits, hist return nodeHits, hist
@ -68,7 +65,7 @@ func analyzeNodeCoverage(g *graph.Graph, nodeHits map[string]int) Coverage {
return NewCoverage(actual, total) return NewCoverage(actual, total)
} }
func analyzeLinkCoverage(g *graph.Graph, plog *simulation.Log) Coverage { func analyzeLinkCoverage(g *graph.Graph, plog *propagation.Log) 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 {