Skip to content

Commit b72b4a8

Browse files
committed
writes csv
1 parent 02ca9db commit b72b4a8

File tree

6 files changed

+177
-69
lines changed

6 files changed

+177
-69
lines changed

Tools/TranscriptAnalysis/CsvWriter.cs

Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
namespace TranscriptAnalysis
2+
{
3+
public class CsvWriter
4+
{
5+
public ICsv CreateNew()
6+
{
7+
return new Csv();
8+
}
9+
10+
public void Write(ICsv csv, string filename)
11+
{
12+
var c = (Csv)csv;
13+
14+
using var file = File.OpenWrite(filename);
15+
using var writer = new StreamWriter(file);
16+
c.CreateLines(writer.WriteLine);
17+
}
18+
}
19+
20+
public interface ICsv
21+
{
22+
ICsvColumn GetColumn(string title, float defaultValue);
23+
ICsvColumn GetColumn(string title, string defaultValue);
24+
void AddRow(params CsvCell[] cells);
25+
}
26+
27+
public class Csv : ICsv
28+
{
29+
private readonly string Sep = ",";
30+
private readonly List<CsvColumn> columns = new List<CsvColumn>();
31+
private readonly List<CsvRow> rows = new List<CsvRow>();
32+
33+
public ICsvColumn GetColumn(string title, float defaultValue)
34+
{
35+
return GetColumn(title, defaultValue.ToString());
36+
}
37+
38+
public ICsvColumn GetColumn(string title, string defaultValue)
39+
{
40+
var column = columns.SingleOrDefault(c => c.Title == title);
41+
if (column == null)
42+
{
43+
column = new CsvColumn(title, defaultValue);
44+
columns.Add(column);
45+
}
46+
return column;
47+
}
48+
49+
public void AddRow(params CsvCell[] cells)
50+
{
51+
rows.Add(new CsvRow(cells));
52+
}
53+
54+
public void CreateLines(Action<string> onLine)
55+
{
56+
CreateHeaderLine(onLine);
57+
foreach (var row in rows)
58+
{
59+
CreateRowLine(row, onLine);
60+
}
61+
}
62+
63+
private void CreateHeaderLine(Action<string> onLine)
64+
{
65+
onLine(string.Join(Sep, columns.Select(c => c.Title).ToArray()));
66+
}
67+
68+
private void CreateRowLine(CsvRow row, Action<string> onLine)
69+
{
70+
onLine(string.Join(Sep, columns.Select(c => GetRowCellValue(row, c)).ToArray()));
71+
}
72+
73+
private string GetRowCellValue(CsvRow row, CsvColumn column)
74+
{
75+
var cell = row.Cells.SingleOrDefault(c => c.Column == column);
76+
if (cell == null) return column.DefaultValue;
77+
return cell.Value;
78+
}
79+
}
80+
81+
public class CsvCell
82+
{
83+
public CsvCell(ICsvColumn column, float value)
84+
: this(column, value.ToString())
85+
{
86+
}
87+
88+
public CsvCell(ICsvColumn column, string value)
89+
{
90+
Column = column;
91+
Value = value;
92+
}
93+
94+
public ICsvColumn Column { get; }
95+
public string Value { get; }
96+
}
97+
98+
public interface ICsvColumn
99+
{
100+
string Title { get; }
101+
string DefaultValue { get; }
102+
}
103+
104+
public class CsvColumn : ICsvColumn
105+
{
106+
public CsvColumn(string title, string defaultValue)
107+
{
108+
Title = title;
109+
DefaultValue = defaultValue;
110+
}
111+
112+
public string Title { get; }
113+
public string DefaultValue { get; }
114+
}
115+
116+
public class CsvRow
117+
{
118+
public CsvRow(CsvCell[] cells)
119+
{
120+
Cells = cells;
121+
}
122+
123+
public CsvCell[] Cells { get; }
124+
}
125+
}

Tools/TranscriptAnalysis/Program.cs

Lines changed: 16 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using Logging;
33
using OverwatchTranscript;
44
using TranscriptAnalysis;
5-
using TranscriptAnalysis.Receivers;
65

76
public static class Program
87
{
@@ -11,77 +10,28 @@ public static class Program
1110
public static void Main(string[] args)
1211
{
1312
Log("Transcript Analysis");
14-
15-
var path1 = "d:\\Projects\\cs-codex-dist-tests\\Tests\\CodexTests\\bin\\Debug\\net8.0\\CodexTestLogs\\2024-10\\11\\08-31-52Z_SwarmTests\\";
16-
var path2 = "d:\\Projects\\cs-codex-dist-tests\\Tests\\CodexTests\\bin\\Debug\\net8.0\\CodexTestLogs\\2024-10\\11\\09-28-29Z_SwarmTests\\";
17-
var files1 = new[]
18-
{
19-
(1, 3, "DetectBlockRetransmits[1,3]_swarm_retransmit.owts"),
20-
(1, 5, "DetectBlockRetransmits[1,5]_swarm_retransmit.owts"),
21-
(1, 10, "DetectBlockRetransmits[1,10]_swarm_retransmit.owts"),
22-
(1, 20, "DetectBlockRetransmits[1,20]_swarm_retransmit.owts"),
23-
(5, 3, "DetectBlockRetransmits[5,3]_swarm_retransmit.owts"),
24-
(5, 5, "DetectBlockRetransmits[5,5]_swarm_retransmit.owts"),
25-
(5, 10, "DetectBlockRetransmits[5,10]_swarm_retransmit.owts"),
26-
(5, 20, "DetectBlockRetransmits[5,20]_swarm_retransmit.owts"),
27-
(10, 5, "DetectBlockRetransmits[10,5]_swarm_retransmit.owts"),
28-
(10, 10, "DetectBlockRetransmits[10,10]_swarm_retransmit.owts")
29-
};
30-
var files2 = new[]
31-
{
32-
(10, 20, "DetectBlockRetransmits[10,20]_swarm_retransmit.owts"),
33-
(20, 3, "DetectBlockRetransmits[20,3]_swarm_retransmit.owts"),
34-
(20, 5, "DetectBlockRetransmits[20,5]_swarm_retransmit.owts"),
35-
(20, 10, "DetectBlockRetransmits[20,10]_swarm_retransmit.owts"),
36-
(20, 20, "DetectBlockRetransmits[20,20]_swarm_retransmit.owts")
37-
};
38-
39-
var countLines = new List<int[]>();
40-
41-
foreach (var file in files1)
42-
{
43-
var path = Path.Combine(path1, file.Item3);
44-
DuplicateBlocksReceived.Counts.Clear();
45-
Run(path);
46-
47-
countLines.Add(new[] { file.Item1, file.Item2 }.Concat(DuplicateBlocksReceived.Counts).ToArray());
48-
}
49-
foreach (var file in files2)
13+
if (!args.Any())
5014
{
51-
var path = Path.Combine(path2, file.Item3);
52-
DuplicateBlocksReceived.Counts.Clear();
53-
Run(path);
54-
55-
countLines.Add(new[] { file.Item1, file.Item2 }.Concat(DuplicateBlocksReceived.Counts).ToArray());
15+
Log("Please pass a .owts file");
16+
Console.ReadLine();
17+
return;
5618
}
5719

58-
var numColumns = countLines.Max(l => l.Length);
59-
var header = new List<string>() { "filesize", "numNodes" };
60-
for (var i = 0; i < numColumns - 2; i++) header.Add("recv" + (i + 1) + "x");
61-
62-
var lines = new List<string>() { string.Join(",", header.ToArray()) };
63-
foreach (var count in countLines)
20+
if (!File.Exists(args[0]))
6421
{
65-
var tokens = new List<int>();
66-
for (var i = 0; i < numColumns; i++)
67-
{
68-
if (i < count.Length) tokens.Add(count[i]);
69-
else tokens.Add(0);
70-
}
71-
lines.Add(string.Join(",", tokens.Select(t => t.ToString()).ToArray()));
22+
Log("File doesn't exist: " + args[0]);
23+
Console.ReadLine();
24+
return;
7225
}
7326

74-
File.WriteAllLines("C:\\Users\\Ben\\Desktop\\blockretransmit.csv", lines.ToArray());
75-
76-
Log("Done.");
77-
Console.ReadLine();
78-
}
27+
var reader = OpenReader(args[0]);
28+
AppDomain.CurrentDomain.ProcessExit += (e, s) =>
29+
{
30+
CloseReader(reader);
31+
};
7932

80-
private static void Run(string file)
81-
{
82-
var reader = OpenReader(file);
8333
var header = reader.GetHeader<OverwatchCodexHeader>("cdx_h");
84-
var receivers = new ReceiverSet(log, reader, header);
34+
var receivers = new ReceiverSet(args[0], log, reader, header);
8535
receivers.InitAll();
8636

8737
var processor = new Processor(log, reader);
@@ -90,7 +40,8 @@ private static void Run(string file)
9040
receivers.FinishAll();
9141

9242
CloseReader(reader);
93-
43+
Log("Done.");
44+
Console.ReadLine();
9445
}
9546

9647
private static ITranscriptReader OpenReader(string filepath)

Tools/TranscriptAnalysis/ReceiverSet.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ namespace TranscriptAnalysis
77
{
88
public interface IEventReceiver
99
{
10-
void Init(ILog log, OverwatchCodexHeader header);
10+
void Init(string sourceFilename, ILog log, OverwatchCodexHeader header);
1111
void Finish();
1212
}
1313

@@ -18,13 +18,15 @@ public interface IEventReceiver<T> : IEventReceiver
1818

1919
public class ReceiverSet
2020
{
21+
private readonly string sourceFilename;
2122
private readonly ILog log;
2223
private readonly ITranscriptReader reader;
2324
private readonly OverwatchCodexHeader header;
2425
private readonly List<IEventReceiver> receivers = new List<IEventReceiver>();
2526

26-
public ReceiverSet(ILog log, ITranscriptReader reader, OverwatchCodexHeader header)
27+
public ReceiverSet(string sourceFilename, ILog log, ITranscriptReader reader, OverwatchCodexHeader header)
2728
{
29+
this.sourceFilename = sourceFilename;
2830
this.log = log;
2931
this.reader = reader;
3032
this.header = header;
@@ -53,7 +55,7 @@ private void Add<T>(IEventReceiver<T> receiver)
5355
mux.Add(receiver);
5456

5557
receivers.Add(receiver);
56-
receiver.Init(log, header);
58+
receiver.Init(sourceFilename, log, header);
5759
}
5860

5961
// We use a mux here because, for each time we call reader.AddEventHandler,

Tools/TranscriptAnalysis/Receivers/BaseReceiver.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ public abstract class BaseReceiver<T> : IEventReceiver<T>
88
{
99
protected ILog log { get; private set; } = new NullLog();
1010
protected OverwatchCodexHeader Header { get; private set; } = null!;
11+
protected CsvWriter CsvWriter { get; private set; } = new CsvWriter();
12+
protected string SourceFilename { get; private set; } = string.Empty;
1113

1214
public abstract string Name { get; }
1315
public abstract void Receive(ActivateEvent<T> @event);
1416
public abstract void Finish();
1517

16-
public void Init(ILog log, OverwatchCodexHeader header)
18+
public void Init(string sourceFilename, ILog log, OverwatchCodexHeader header)
1719
{
1820
this.log = new LogPrefixer(log, $"({Name}) ");
1921
Header = header;
22+
SourceFilename = sourceFilename;
2023
}
2124

2225
protected string? GetPeerId(int nodeIndex)

Tools/TranscriptAnalysis/Receivers/DuplicateBlocksReceived.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ namespace TranscriptAnalysis.Receivers
66
public class DuplicateBlocksReceived : BaseReceiver<OverwatchCodexEvent>
77
{
88
public static List<int> Counts = new List<int>();
9+
private long uploadSize;
910

1011
public override string Name => "BlocksReceived";
1112

@@ -15,11 +16,17 @@ public override void Receive(ActivateEvent<OverwatchCodexEvent> @event)
1516
{
1617
Handle(@event.Payload, @event.Payload.BlockReceived);
1718
}
19+
if (@event.Payload.FileUploaded != null)
20+
{
21+
var uploadEvent = @event.Payload.FileUploaded;
22+
uploadSize = uploadEvent.ByteSize;
23+
}
1824
}
1925

2026
public override void Finish()
2127
{
2228
Log("Number of BlockReceived events seen: " + seen);
29+
var csv = CsvWriter.CreateNew();
2330

2431
var totalReceived = peerIdBlockAddrCount.Sum(a => a.Value.Sum(p => p.Value));
2532
var maxRepeats = peerIdBlockAddrCount.Max(a => a.Value.Max(p => p.Value));
@@ -36,14 +43,23 @@ public override void Finish()
3643
if (Counts.Any()) throw new Exception("Should be empty");
3744

3845
float t = totalReceived;
46+
csv.GetColumn("numNodes", Header.Nodes.Length);
47+
csv.GetColumn("filesize", uploadSize.ToString());
48+
var receiveCountColumn = csv.GetColumn("receiveCount", 0.0f);
49+
var occuranceColumn = csv.GetColumn("occurance", 0.0f);
3950
occurances.PrintContinous((i, count) =>
4051
{
4152
float n = count;
4253
float p = 100.0f * (n / t);
4354
Log($"Block received {i} times = {count}x ({p}%)");
4455
Counts.Add(count);
56+
csv.AddRow(
57+
new CsvCell(receiveCountColumn, i),
58+
new CsvCell(occuranceColumn, count)
59+
);
4560
});
4661

62+
CsvWriter.Write(csv, SourceFilename + "_blockduplicates.csv");
4763
}
4864

4965
private int seen = 0;

Tools/TranscriptAnalysis/Receivers/NodesDegree.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public override void Receive(ActivateEvent<OverwatchCodexEvent> @event)
6161

6262
public override void Finish()
6363
{
64+
var csv = CsvWriter.CreateNew();
65+
6466
var numNodes = dialingNodes.Count;
6567
var redialOccurances = new OccuranceMap();
6668
foreach (var dial in dials.Values)
@@ -81,12 +83,21 @@ public override void Finish()
8183
});
8284

8385
float tot = numNodes;
86+
csv.GetColumn("numNodes", Header.Nodes.Length);
87+
var degreeColumn = csv.GetColumn("degree", 0.0f);
88+
var occuranceColumn = csv.GetColumn("occurance", 0.0f);
8489
degreeOccurances.Print((i, count) =>
8590
{
8691
float n = count;
8792
float p = 100.0f * (n / tot);
8893
Log($"Degree: {i} = {count}x ({p}%)");
94+
csv.AddRow(
95+
new CsvCell(degreeColumn, i),
96+
new CsvCell(occuranceColumn, n)
97+
);
8998
});
99+
100+
CsvWriter.Write(csv, SourceFilename + "_nodeDegrees.csv");
90101
}
91102

92103
private void AddDial(string peerId, string targetPeerId)

0 commit comments

Comments
 (0)