From 58cebd43ce0642d69d0856ec853b206766577728 Mon Sep 17 00:00:00 2001 From: benbierens Date: Thu, 8 Aug 2024 09:39:20 +0200 Subject: [PATCH] Identified issue with nodesDegree overview --- Tools/TranscriptAnalysis/OccuranceMap.cs | 47 ++++++++ Tools/TranscriptAnalysis/Program.cs | 2 +- .../Receivers/DuplicateBlocksReceived.cs | 14 +-- .../Receivers/NodesDegree.cs | 107 +++++++++++++++--- 4 files changed, 145 insertions(+), 25 deletions(-) create mode 100644 Tools/TranscriptAnalysis/OccuranceMap.cs diff --git a/Tools/TranscriptAnalysis/OccuranceMap.cs b/Tools/TranscriptAnalysis/OccuranceMap.cs new file mode 100644 index 0000000..8474f4a --- /dev/null +++ b/Tools/TranscriptAnalysis/OccuranceMap.cs @@ -0,0 +1,47 @@ +namespace TranscriptAnalysis +{ + public class OccuranceMap + { + private readonly Dictionary map = new Dictionary(); + + public void Add(int point) + { + if (map.ContainsKey(point)) + { + map[point]++; + } + else + { + map.Add(point, 1); + } + } + + public void Print(Action action) + { + Print(false, action); + } + + public void PrintContinous(Action action) + { + Print(true, action); + } + + private void Print(bool continuous, Action action) + { + var min = map.Keys.Min(); + var max = map.Keys.Max(); + + for (var i = min; i <= max; i++) + { + if (map.ContainsKey(i)) + { + action(i, map[i]); + } + else if (continuous) + { + action(i, 0); + } + } + } + } +} diff --git a/Tools/TranscriptAnalysis/Program.cs b/Tools/TranscriptAnalysis/Program.cs index b1141cb..2759c50 100644 --- a/Tools/TranscriptAnalysis/Program.cs +++ b/Tools/TranscriptAnalysis/Program.cs @@ -8,7 +8,7 @@ public static class Program public static void Main(string[] args) { - args = new[] { "D:\\Projects\\cs-codex-dist-tests\\Tests\\CodexTests\\bin\\Debug\\net7.0\\CodexTestLogs\\2024-08\\06\\08-24-45Z_ThreeClientTest\\SwarmTest_SwarmTest.owts" }; + //args = new[] { "D:\\Projects\\cs-codex-dist-tests\\Tests\\CodexTests\\bin\\Debug\\net7.0\\CodexTestLogs\\2024-08\\06\\08-24-45Z_ThreeClientTest\\SwarmTest_SwarmTest.owts" }; Log("Transcript Analysis"); if (!args.Any()) diff --git a/Tools/TranscriptAnalysis/Receivers/DuplicateBlocksReceived.cs b/Tools/TranscriptAnalysis/Receivers/DuplicateBlocksReceived.cs index 4a7fa8c..d0f8f36 100644 --- a/Tools/TranscriptAnalysis/Receivers/DuplicateBlocksReceived.cs +++ b/Tools/TranscriptAnalysis/Receivers/DuplicateBlocksReceived.cs @@ -21,23 +21,23 @@ namespace TranscriptAnalysis.Receivers var totalReceived = peerIdBlockAddrCount.Sum(a => a.Value.Sum(p => p.Value)); var maxRepeats = peerIdBlockAddrCount.Max(a => a.Value.Max(p => p.Value)); - var occurances = new int[maxRepeats + 1]; + var occurances = new OccuranceMap(); foreach (var peerPair in peerIdBlockAddrCount) { - foreach (var pair in peerPair.Value) + foreach (var blockCountPair in peerPair.Value) { - occurances[pair.Value]++; + occurances.Add(blockCountPair.Value); } } float t = totalReceived; - for (var i = 1; i < occurances.Length; i++) + occurances.PrintContinous((i, count) => { - float n = occurances[i]; + float n = count; float p = 100.0f * (n / t); - Log($"Block received {i} times = {occurances[i]}x ({p}%)"); - } + Log($"Block received {i} times = {count}x ({p}%)"); + }); } private int seen = 0; diff --git a/Tools/TranscriptAnalysis/Receivers/NodesDegree.cs b/Tools/TranscriptAnalysis/Receivers/NodesDegree.cs index 6b57dd8..8957235 100644 --- a/Tools/TranscriptAnalysis/Receivers/NodesDegree.cs +++ b/Tools/TranscriptAnalysis/Receivers/NodesDegree.cs @@ -6,7 +6,46 @@ namespace TranscriptAnalysis.Receivers { public class NodesDegree : BaseReceiver { - private readonly Dictionary> dials = new Dictionary>(); + public class Dial + { + public Dial(Node peer, Node target) + { + Id = GetLineId(peer.Id, target.Id); + InitiatedBy.Add(peer); + Peer = peer; + Target = target; + } + + public string Id { get; } + public int RedialCount => InitiatedBy.Count - 1; + public List InitiatedBy { get; } = new List(); + public Node Peer { get; } + public Node Target { get; } + + private string GetLineId(string a, string b) + { + if (string.Compare(a, b) > 0) + { + return a + b; + } + return b + a; + } + } + + public class Node + { + public Node(string peerId) + { + Id = peerId; + } + + public string Id { get; } + public List Dials { get; } = new List(); + public int Degree => Dials.Count; + } + + private readonly Dictionary dialingNodes = new Dictionary(); + private readonly Dictionary dials = new Dictionary(); public override string Name => "NodesDegree"; @@ -18,34 +57,68 @@ namespace TranscriptAnalysis.Receivers } } - private void AddDial(string peerId, string targetPeerId) + public override void Finish() { - if (!dials.ContainsKey(peerId)) + var numNodes = dialingNodes.Count; + var redialOccurances = new OccuranceMap(); + foreach (var dial in dials.Values) { - dials.Add(peerId, new Dictionary()); + redialOccurances.Add(dial.RedialCount); + } + var degreeOccurances = new OccuranceMap(); + foreach (var node in dialingNodes.Values) + { + degreeOccurances.Add(node.Degree); } - var d = dials[peerId]; - if (!d.ContainsKey(targetPeerId)) + Log($"Dialing nodes: {numNodes}"); + Log("Redials:"); + redialOccurances.PrintContinous((i, count) => { - d.Add(targetPeerId, 1); + Log($"{i} redials = {count}x"); + }); + + float tot = numNodes; + degreeOccurances.Print((i, count) => + { + float n = count; + float p = 100.0f * (n / tot); + Log($"Degree: {i} = {count}x ({p}%)"); + }); + } + + private void AddDial(string peerId, string targetPeerId) + { + peerId = CodexUtils.ToShortId(peerId); + targetPeerId = CodexUtils.ToShortId(targetPeerId); + + var peer = GetNode(peerId); + var target = GetNode(targetPeerId); ; + + var dial = new Dial(peer, target); + + if (dials.ContainsKey(dial.Id)) + { + var d = dials[dial.Id]; + d.InitiatedBy.Add(peer); + peer.Dials.Add(d); + target.Dials.Add(d); } else { - d[targetPeerId]++; + dials.Add(dial.Id, dial); + peer.Dials.Add(dial); + target.Dials.Add(dial); } } - public override void Finish() + private Node GetNode(string id) { - var numNodes = dials.Keys.Count; - var redials = dials.Values.Count(t => t.Values.Any(nd => nd > 1)); - - var min = dials.Values.Min(t => t.Count); - var avg = dials.Values.Average(t => t.Count); - var max = dials.Values.Max(t => t.Count); - - Log($"Nodes: {numNodes} - Degrees: min:{min} avg:{avg} max:{max} - Redials: {redials}"); + if (!dialingNodes.ContainsKey(id)) + { + dialingNodes.Add(id, new Node(id)); + } + return dialingNodes[id]; } } }