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

View File

@ -5,7 +5,7 @@ import (
"log"
"time"
"github.com/status-im/simulator/simulation"
"github.com/status-im/simulator/propagation"
)
// 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.
// 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 {
links := s.data.Links()
for i := range links {
@ -72,7 +72,7 @@ func (s *Simulator) LogEntries2PropagationLog(entries []*LogEntry) *simulation.L
tsnodes[entry.Ts] = nnodes
}
var ret = &simulation.Log{
var ret = &propagation.Log{
Timestamps: make([]int, 0, len(tss)),
Indices: make([][]int, 0, len(tss)),
Nodes: make([][]int, 0, len(tss)),

View File

@ -5,7 +5,7 @@ import (
"time"
"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.
@ -46,12 +46,13 @@ func NewSimulator(data *graph.Graph, N int, delay time.Duration) *Simulator {
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 {
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{
Content: "dummy",
TTL: ttl,
@ -71,7 +72,7 @@ func (s *Simulator) SendMessage(startNodeIdx, ttl int) *simulation.Log {
case val := <-s.reportCh:
ret = append(ret, &val)
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.
type Simulator interface {
// PropagationSimulator defines the simulators for message propagation within the graph.
type PropagationSimulator interface {
SendMessage(idx, ttl int) *Log
Stop() error
}

View File

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

View File

@ -14,11 +14,11 @@ import (
"github.com/ethereum/go-ethereum/p2p/simulations"
"github.com/ethereum/go-ethereum/p2p/simulations/adapters"
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
// given p2p network.
// given p2p network. Implements PropagationSimulator interface.
type Simulator struct {
data *graph.Graph
network *simulations.Network
@ -26,6 +26,7 @@ type Simulator struct {
}
// NewSimulator intializes simulator for the given graph data.
// It uses defaults for PoW settings.
func NewSimulator(data *graph.Graph) *Simulator {
rand.Seed(time.Now().UnixNano())
@ -119,8 +120,8 @@ func (s *Simulator) Stop() error {
return nil
}
// SendMessage sends single message and tracks propagation. Implements simulator.Interface.
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 {
node := s.network.Nodes[startNodeIdx]
// 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
done bool
count int
plog []*LogEntry
plog []*logEntry
)
for subErr == nil && !done {
select {
@ -170,7 +171,7 @@ func (s *Simulator) SendMessage(startNodeIdx, ttl int) *simulation.Log {
if msg.Code == 1 && msg.Protocol == "shh" && msg.Received == false {
from := ncache[msg.One]
to := ncache[msg.Other]
entry := NewLogEntry(start, from, to)
entry := newlogEntry(start, from, to)
plog = append(plog, entry)
count++
}
@ -188,13 +189,13 @@ func (s *Simulator) SendMessage(startNodeIdx, ttl int) *simulation.Log {
if subErr != nil {
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.
// 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()
findLink := func(from, to int) int {
for i := range links {
@ -233,7 +234,7 @@ func (s *Simulator) LogEntries2PropagationLog(entries []*LogEntry) *simulation.L
tsnodes[entry.Ts] = nnodes
}
var ret = &simulation.Log{
var ret = &propagation.Log{
Timestamps: make([]int, 0, len(tss)),
Indices: make([][]int, 0, len(tss)),
Nodes: make([][]int, 0, len(tss)),

View File

@ -4,10 +4,8 @@ import (
"fmt"
"log"
"v.io/x/ref/lib/stats/histogram"
"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.
@ -15,7 +13,7 @@ type Stats struct {
NodeHits map[string]int
NodeCoverage Coverage
LinkCoverage Coverage
Hist *histogram.Histogram
Hist *Histogram
}
// PrintVerbose prints detailed terminal-friendly stats to
@ -24,11 +22,10 @@ func (s *Stats) PrintVerbose() {
fmt.Println("Stats:")
fmt.Println("Nodes coverage:", s.NodeCoverage)
fmt.Println("Links coverage:", s.LinkCoverage)
fmt.Println("Node hits:", s.Hist.Value())
}
// 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)
nodeCoverage := analyzeNodeCoverage(g, nodeHits)
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)
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)
for _, v := range nodeHits {
hist.Add(v)
hist.Add(v, v)
}
return nodeHits, hist
@ -68,7 +65,7 @@ func analyzeNodeCoverage(g *graph.Graph, nodeHits map[string]int) Coverage {
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{})
for _, links := range plog.Indices {
for _, j := range links {