|
1 | 1 | package p2p_test |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "math" |
4 | 7 | "math/rand" |
| 8 | + "os" |
| 9 | + "sync" |
5 | 10 | "testing" |
6 | 11 | "time" |
7 | 12 |
|
| 13 | + "github.com/elecbug/netkit/network-graph/algorithm" |
8 | 14 | "github.com/elecbug/netkit/network-graph/graph/standard_graph" |
9 | 15 | "github.com/elecbug/netkit/p2p" |
10 | 16 | ) |
11 | 17 |
|
12 | 18 | func TestGenerateNetwork(t *testing.T) { |
13 | | - g := standard_graph.ErdosRenyiGraph(1000, 0.005, true) |
| 19 | + g := standard_graph.ErdosRenyiGraph(1000, 0.05, true) |
14 | 20 | t.Logf("Generated graph with %d nodes and %d edges\n", len(g.Nodes()), g.EdgeCount()) |
15 | 21 | src := rand.NewSource(time.Now().UnixNano()) |
16 | 22 |
|
17 | 23 | nodeLatency := func() float64 { return p2p.LogNormalRand(5.704, 0.5, src) } |
18 | 24 | edgeLatency := func() float64 { return p2p.LogNormalRand(5.704, 0.3, src) } |
| 25 | + queuingLatency := func() float64 { return p2p.LogNormalRand(5.0, 0.2, src) } |
19 | 26 |
|
20 | | - nw, _ := p2p.GenerateNetwork(g, nodeLatency, edgeLatency) |
| 27 | + nw, _ := p2p.GenerateNetwork(g, nodeLatency, edgeLatency, queuingLatency) |
21 | 28 | t.Logf("Generated network with %d nodes\n", len(nw)) |
22 | 29 | for id, node := range nw { |
23 | | - t.Logf("Node %d: latency=%.2fms, edges=%v\n", id, node.Latency, node.Edges) |
| 30 | + t.Logf("Node %d: validation_latency=%.2fms, queuing_latency=%.2fms, edges=%v\n", id, node.ValidationLatency, node.QueuingLatency, node.Edges) |
24 | 31 | } |
25 | 32 |
|
26 | | - p2p.RunNetworkSimulation(nw) |
27 | | - p2p.Publish(nw[0], "Hello, P2P Network!") |
| 33 | + msg := "Hello, P2P World!" |
28 | 34 |
|
29 | | - time.Sleep(5 * time.Second) |
| 35 | + p2p.RunNetworkSimulation(nw) |
| 36 | + p2p.Publish(nw[0], msg) |
| 37 | + time.Sleep(1 * time.Second) |
30 | 38 |
|
31 | 39 | count := 0 |
| 40 | + result := make(map[string]map[string]any) |
| 41 | + |
32 | 42 | for id, node := range nw { |
33 | | - c := len(node.SentTo["Hello, P2P Network!"]) |
| 43 | + c := len(node.SentTo[msg]) |
34 | 44 | t.Logf("Node %d sent %d/%d\n", id, c, len(node.Edges)) |
35 | | - t.Logf("Node %d data: recv: %v, sent: %v, seen: %v\n", |
36 | | - id, |
37 | | - node.RecvFrom["Hello, P2P Network!"], |
38 | | - node.SentTo["Hello, P2P Network!"], |
39 | | - node.SeenAt["Hello, P2P Network!"], |
40 | | - ) |
| 45 | + |
| 46 | + result[fmt.Sprintf("node_%d", id)] = map[string]any{} |
| 47 | + result[fmt.Sprintf("node_%d", id)]["recv"] = node.RecvFrom[msg] |
| 48 | + result[fmt.Sprintf("node_%d", id)]["sent"] = node.SentTo[msg] |
| 49 | + result[fmt.Sprintf("node_%d", id)]["seen"] = node.SeenAt[msg] |
| 50 | + |
41 | 51 | count += c |
42 | 52 | } |
43 | 53 |
|
44 | 54 | t.Logf("Total received count: %d\n", count) |
| 55 | + |
| 56 | + data, _ := json.Marshal(result) |
| 57 | + |
| 58 | + os.WriteFile("p2p_result.log", data, 0644) |
| 59 | +} |
| 60 | + |
| 61 | +func TestExpCase(t *testing.T) { |
| 62 | + run := false |
| 63 | + |
| 64 | + if run { |
| 65 | + for i := 4; i <= 11; i++ { |
| 66 | + wg := &sync.WaitGroup{} |
| 67 | + |
| 68 | + for j := 0; j < 60; j++ { |
| 69 | + wg.Add(1) |
| 70 | + go func(j int) { |
| 71 | + defer wg.Done() |
| 72 | + filename := fmt.Sprintf("temp/p2p_result-%02d-%03d.log", i, j) |
| 73 | + |
| 74 | + if _, err := os.Stat(filename); err == nil { |
| 75 | + t.Logf("File %s already exists, skipping...\n", filename) |
| 76 | + return |
| 77 | + } |
| 78 | + |
| 79 | + t.Logf("Experiment case: %02d-%03d\n", i, j) |
| 80 | + r := rand.New(rand.NewSource(time.Now().UnixNano())) |
| 81 | + |
| 82 | + n := r.Int()%20 + 170 |
| 83 | + g := standard_graph.ErdosRenyiGraph(n, float64(i)/float64(n), true) |
| 84 | + |
| 85 | + nodeLatency := func() float64 { return p2p.LogNormalRand(math.Log(0), 0.01, rand.NewSource(time.Now().UnixNano())) } |
| 86 | + edgeLatency := func() float64 { return p2p.LogNormalRand(math.Log(500), 0.01, rand.NewSource(time.Now().UnixNano())) } |
| 87 | + queuingLatency := func() float64 { return p2p.LogNormalRand(math.Log(0), 0.01, rand.NewSource(time.Now().UnixNano())) } |
| 88 | + |
| 89 | + nw, _ := p2p.GenerateNetwork(g, nodeLatency, edgeLatency, queuingLatency) |
| 90 | + msg := "Hello, P2P World!" |
| 91 | + |
| 92 | + t.Logf("Generated graph with %d nodes and %d edges\n", len(g.Nodes()), g.EdgeCount()) |
| 93 | + |
| 94 | + p2p.RunNetworkSimulation(nw) |
| 95 | + p2p.Publish(nw[0], msg) |
| 96 | + time.Sleep(5 * time.Second) |
| 97 | + |
| 98 | + result := make(map[string]map[string]any) |
| 99 | + |
| 100 | + for id, node := range nw { |
| 101 | + result[fmt.Sprintf("node_%d", id)] = map[string]any{} |
| 102 | + result[fmt.Sprintf("node_%d", id)]["recv"] = node.RecvFrom[msg] |
| 103 | + result[fmt.Sprintf("node_%d", id)]["sent"] = node.SentTo[msg] |
| 104 | + result[fmt.Sprintf("node_%d", id)]["seen"] = node.SeenAt[msg] |
| 105 | + } |
| 106 | + |
| 107 | + data, _ := json.Marshal(result) |
| 108 | + |
| 109 | + os.WriteFile(filename, data, 0644) |
| 110 | + |
| 111 | + algorithm.CacheClear() |
| 112 | + }(j) |
| 113 | + } |
| 114 | + |
| 115 | + wg.Wait() |
| 116 | + } |
| 117 | + } |
45 | 118 | } |
0 commit comments