Skip to content

Commit f45463d

Browse files
Mohammad DehghanMohammad Dehghan
authored andcommitted
Change RecordingController design and support multiple processes using Runtime Config file
1 parent 5a94a37 commit f45463d

File tree

8 files changed

+209
-79
lines changed

8 files changed

+209
-79
lines changed

SG.CodeCoverage.Tests.NetFx/InstrumenterTester.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
using SG.CodeCoverage.Common;
44
using SG.CodeCoverage.Coverage;
55
using SG.CodeCoverage.Instrumentation;
6+
using SG.CodeCoverage.Metadata;
67
using System;
78
using System.Collections.Generic;
89
using System.IO;
@@ -14,10 +15,10 @@ namespace SG.CodeCoverage.Tests
1415
public class InstrumenterTester
1516
{
1617
public const int PortNumber = 61238;
17-
public const string MapFileName = "map.json";
1818
public static string DefaultOutputPath { get; }
1919
public string OutputPath { get; }
2020
public string MapFilePath { get; }
21+
public InstrumentationMap Map { get; private set; }
2122
public string InstrumentedAssemblyPath { get; private set; }
2223

2324
static InstrumenterTester()
@@ -28,13 +29,11 @@ static InstrumenterTester()
2829
public InstrumenterTester()
2930
{
3031
OutputPath = DefaultOutputPath;
31-
MapFilePath = Path.Combine(OutputPath, MapFileName);
3232
}
3333

3434
public InstrumenterTester(string existingInstrumentedSampleFolder)
3535
{
3636
OutputPath = existingInstrumentedSampleFolder;
37-
MapFilePath = Path.Combine(OutputPath, MapFileName);
3837
InstrumentedAssemblyPath = Path.Combine(OutputPath, Path.GetFileName(typeof(PrimeCalculator).Assembly.Location));
3938
}
4039

@@ -56,10 +55,10 @@ public void InstrumentSampleProject()
5655
var options = new InstrumentationOptions(
5756
new[] { assemblyFileName },
5857
Array.Empty<string>(), OutputPath, PortNumber);
59-
Instrumenter instrumenter = new Instrumenter(options, MapFilePath, new ConsoleLogger());
58+
Instrumenter instrumenter = new Instrumenter(options, new ConsoleLogger());
6059
instrumenter.BackupFolder = Path.Combine(OutputPath, "backup");
6160
Directory.CreateDirectory(instrumenter.BackupFolder);
62-
instrumenter.Instrument();
61+
Map = instrumenter.Instrument();
6362
InstrumentedAssemblyPath = assemblyFileName;
6463
}
6564

@@ -75,7 +74,7 @@ public void RunSomeCode()
7574
{
7675
if (InstrumentedAssemblyPath == null)
7776
throw new InvalidOperationException("Sample assembly is not instrumented.");
78-
var client = new Collection.RecordingController(PortNumber);
77+
var client = RecordingController.ForEndPoint("localhost", PortNumber, Map);
7978
Assembly.LoadFrom(Path.Combine(OutputPath, "SG.CodeCoverage.Recorder.dll"));
8079
var assembly = Assembly.LoadFrom(InstrumentedAssemblyPath);
8180
var calc = assembly.DefinedTypes.Where(x => x.Name == nameof(PrimeCalculator)).FirstOrDefault();
@@ -86,10 +85,8 @@ public List<string> GetVisitedFiles()
8685
{
8786
if (InstrumentedAssemblyPath == null)
8887
throw new InvalidOperationException("Sample assembly is not instrumented.");
89-
var client = new RecordingController(PortNumber);
90-
var hitsFile = Path.Combine(OutputPath, "hits.bin");
91-
client.SaveHitsAndReset(hitsFile);
92-
return new CoverageResult(MapFilePath, hitsFile).GetVisitedSources().ToList();
88+
var client = RecordingController.ForEndPoint("localhost", PortNumber, Map);
89+
return client.CollectResultAndReset().GetVisitedSources().ToList(); ;
9390
}
9491
}
9592
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using SG.CodeCoverage.Coverage;
2+
3+
namespace SG.CodeCoverage.Collection
4+
{
5+
public interface IRecordingController
6+
{
7+
void ResetHits();
8+
CoverageResult CollectResultAndReset();
9+
}
10+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using SG.CodeCoverage.Coverage;
2+
using SG.CodeCoverage.Metadata;
3+
using SG.CodeCoverage.Recorder.RecordingController;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
8+
namespace SG.CodeCoverage.Collection
9+
{
10+
internal class MultiRecordingController : IRecordingController
11+
{
12+
private readonly string _recorderRuntimeConfigFilePath;
13+
private readonly InstrumentationMap _map;
14+
private DateTime _currentRuntimeConfigFileLastUpdate;
15+
private RuntimeConfig _currentRuntimeConfig;
16+
private const string _host = "localhost";
17+
18+
public MultiRecordingController(string recorderRuntimeConfigFilePath, InstrumentationMap map)
19+
{
20+
_recorderRuntimeConfigFilePath = recorderRuntimeConfigFilePath;
21+
_map = map;
22+
}
23+
24+
public void ResetHits()
25+
{
26+
if (!LoadRuntimeConfig())
27+
return;
28+
foreach(var process in _currentRuntimeConfig.Processes)
29+
{
30+
RecordingControllerClient.ResetHits(_host, process.ListeningPort);
31+
}
32+
}
33+
34+
public CoverageResult CollectResultAndReset()
35+
{
36+
if (!LoadRuntimeConfig())
37+
return new CoverageResult(_map, Array.Empty<int[]>());
38+
CoverageResult result = null;
39+
foreach(var process in _currentRuntimeConfig.Processes)
40+
{
41+
var procResult = RecordingControllerClient.CollectResultAndReset(_host, process.ListeningPort, _map);
42+
if (result == null)
43+
result = procResult;
44+
else
45+
result = result.MergeWith(procResult);
46+
}
47+
return result;
48+
}
49+
50+
private bool LoadRuntimeConfig()
51+
{
52+
if(!File.Exists(_recorderRuntimeConfigFilePath))
53+
{
54+
_currentRuntimeConfigFileLastUpdate = default;
55+
_currentRuntimeConfig = null;
56+
return false;
57+
}
58+
59+
var newUpdateDate = File.GetLastWriteTime(_recorderRuntimeConfigFilePath);
60+
if (_currentRuntimeConfig == null ||
61+
newUpdateDate > _currentRuntimeConfigFileLastUpdate)
62+
{
63+
_currentRuntimeConfig = RuntimeConfig.Load(_recorderRuntimeConfigFilePath);
64+
_currentRuntimeConfigFileLastUpdate = newUpdateDate;
65+
}
66+
return true;
67+
}
68+
}
69+
}
Lines changed: 6 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -1,78 +1,17 @@
1-
using SG.CodeCoverage.Recorder;
2-
using System;
3-
using System.IO;
4-
using System.Net.Sockets;
1+
using SG.CodeCoverage.Metadata;
52

63
namespace SG.CodeCoverage.Collection
74
{
8-
public sealed class RecordingController : IDisposable
5+
public static class RecordingController
96
{
10-
public string Host { get; }
11-
public int PortNumber { get; }
12-
private TcpClient _tcpClient;
13-
public int ConnectionTimeoutSeconds { get; set; }
14-
15-
public RecordingController(int portNumber)
16-
: this("localhost", portNumber)
17-
{
18-
}
19-
20-
public RecordingController(string host, int portNumber, int connectionTimeoutSeconds = 10)
21-
{
22-
Host = host;
23-
PortNumber = portNumber;
24-
ConnectionTimeoutSeconds = connectionTimeoutSeconds;
25-
}
26-
27-
public void SaveHitsAndReset(string outputHitsFilePath)
28-
{
29-
var response = SendCommand(Constants.SaveCommand, outputHitsFilePath);
30-
ValidateResponse(response, "saving hits file");
31-
}
32-
33-
public void ResetHits()
7+
public static IRecordingController ForRuntimeConfigFile(string runtimeConfigFilePath, InstrumentationMap map)
348
{
35-
var response = SendCommand(Constants.ResetCommand, null);
36-
ValidateResponse(response, "resettings hits");
37-
}
38-
39-
private void ValidateResponse((bool successful, string result) response, string operationString)
40-
{
41-
if (!response.successful)
42-
throw new Exception(
43-
$"An error occurred in the recorder while {operationString}. The error was:\r\n" +
44-
response.result);
45-
}
46-
47-
private (bool successful, string result) SendCommand(string command, string param)
48-
{
49-
if (_tcpClient == null)
50-
_tcpClient = new TcpClient();
51-
if (!_tcpClient.Connected)
52-
if (!_tcpClient.ConnectAsync(Host, PortNumber).Wait(ConnectionTimeoutSeconds * 1000))
53-
throw new TimeoutException($"Cannot connect to '{Host}:{PortNumber}'. Connection timed out.");
54-
55-
string result;
56-
using (var nstream = _tcpClient.GetStream())
57-
{
58-
BinaryWriter writer = new BinaryWriter(nstream);
59-
writer.Write(command + (string.IsNullOrEmpty(param) ? string.Empty : " " + param));
60-
writer.Flush();
61-
result = new BinaryReader(nstream).ReadString().Trim();
62-
}
63-
if (result.Equals(Constants.CommandOkResponse, StringComparison.OrdinalIgnoreCase))
64-
return (successful: true, result: result.Substring(Constants.CommandOkResponse.Length).Trim());
65-
else if (result.StartsWith(Constants.CommandErrorResponse, StringComparison.OrdinalIgnoreCase))
66-
return (successful: false, result: result.Substring(Constants.CommandErrorResponse.Length).Trim());
67-
else
68-
throw new Exception("Unknown response:\r\n" + result);
69-
9+
return new MultiRecordingController(runtimeConfigFilePath, map);
7010
}
7111

72-
public void Dispose()
12+
public static IRecordingController ForEndPoint(string host, int port, InstrumentationMap map)
7313
{
74-
if (_tcpClient != null)
75-
_tcpClient.Dispose();
14+
return new SingleRecordingController(host, port, map);
7615
}
7716
}
7817
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
using SG.CodeCoverage.Coverage;
2+
using SG.CodeCoverage.Metadata;
3+
using SG.CodeCoverage.Recorder;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.IO;
7+
using System.Linq;
8+
using System.Net.Sockets;
9+
using System.Text;
10+
using System.Threading.Tasks;
11+
12+
namespace SG.CodeCoverage.Collection
13+
{
14+
public static class RecordingControllerClient
15+
{
16+
public const int ConnectionTimeoutSeconds = 10;
17+
18+
public static void SaveHitsAndReset(string host, int port, string outputHitsFilePath)
19+
{
20+
var response = SendCommand(host, port, Constants.SaveCommand, outputHitsFilePath);
21+
ValidateResponse(response, "saving hits file");
22+
}
23+
24+
public static CoverageResult CollectResultAndReset(string host, int port, InstrumentationMap map)
25+
{
26+
var fileName = GetTempFileName();
27+
try
28+
{
29+
SaveHitsAndReset(host, port, fileName);
30+
return new CoverageResult(map, fileName);
31+
}
32+
finally
33+
{
34+
if (File.Exists(fileName))
35+
File.Delete(fileName);
36+
}
37+
}
38+
39+
40+
public static void ResetHits(string host, int port)
41+
{
42+
var response = SendCommand(host, port, Constants.ResetCommand, null);
43+
ValidateResponse(response, "resettings hits");
44+
}
45+
46+
private static (bool successful, string result) SendCommand(string host, int port, string command, string param)
47+
{
48+
var tcpClient = new TcpClient();
49+
if (!tcpClient.ConnectAsync(host, port).Wait(ConnectionTimeoutSeconds * 1000))
50+
throw new TimeoutException($"Cannot connect to '{host}:{port}'. Connection timed out.");
51+
52+
string result;
53+
using (var nstream = tcpClient.GetStream())
54+
{
55+
BinaryWriter writer = new BinaryWriter(nstream);
56+
writer.Write(command + (string.IsNullOrEmpty(param) ? string.Empty : " " + param));
57+
writer.Flush();
58+
result = new BinaryReader(nstream).ReadString().Trim();
59+
}
60+
if (result.Equals(Constants.CommandOkResponse, StringComparison.OrdinalIgnoreCase))
61+
return (successful: true, result: result.Substring(Constants.CommandOkResponse.Length).Trim());
62+
else if (result.StartsWith(Constants.CommandErrorResponse, StringComparison.OrdinalIgnoreCase))
63+
return (successful: false, result: result.Substring(Constants.CommandErrorResponse.Length).Trim());
64+
else
65+
throw new Exception("Unknown response:\r\n" + result);
66+
}
67+
68+
private static string GetTempFileName()
69+
{
70+
return Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString());
71+
}
72+
73+
private static void ValidateResponse((bool successful, string result) response, string operationString)
74+
{
75+
if (!response.successful)
76+
throw new Exception(
77+
$"An error occurred in the recorder while {operationString}. The error was:\r\n" +
78+
response.result);
79+
}
80+
81+
}
82+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
using SG.CodeCoverage.Coverage;
2+
using SG.CodeCoverage.Metadata;
3+
4+
namespace SG.CodeCoverage.Collection
5+
{
6+
internal class SingleRecordingController : IRecordingController
7+
{
8+
private readonly string _host;
9+
private readonly int _port;
10+
private readonly InstrumentationMap _map;
11+
12+
public SingleRecordingController(string host, int port, InstrumentationMap map)
13+
{
14+
_host = host;
15+
_port = port;
16+
_map = map;
17+
}
18+
19+
public CoverageResult CollectResultAndReset()
20+
{
21+
return RecordingControllerClient.CollectResultAndReset(_host, _port, _map);
22+
}
23+
24+
public void ResetHits()
25+
{
26+
RecordingControllerClient.ResetHits(_host, _port);
27+
}
28+
}
29+
}

SG.CodeCoverage/Coverage/CoverageResult.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@ public CoverageResult(InstrumentationMap map, string hitsPath)
3030
Init(map, uniqueId, hits);
3131
}
3232

33-
public CoverageResult(InstrumentationMap map, int[][] hits, Guid hitsUniqueId)
33+
public CoverageResult(InstrumentationMap map, int[][] hits)
3434
{
35-
Init(map, hitsUniqueId, hits);
35+
Init(map, map.UniqueId, hits);
3636
}
3737

3838
public CoverageResult(VersionInfo instrumenterVersion, Guid instrumentUniqueId, IReadOnlyCollection<CoverageAssemblyResult> assemblies)

SG.CodeCoverage/SG.CodeCoverage.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,11 @@
6161
<Reference Include="System.Xml" />
6262
</ItemGroup>
6363
<ItemGroup>
64+
<Compile Include="Collection\IRecordingController.cs" />
65+
<Compile Include="Collection\MultiRecordingController.cs" />
66+
<Compile Include="Collection\RecordingControllerClient.cs" />
6467
<Compile Include="Collection\RecordingController.cs" />
68+
<Compile Include="Collection\SingleRecordingController.cs" />
6569
<Compile Include="Common\ConsoleLogger.cs" />
6670
<Compile Include="Common\ILogger.cs" />
6771
<Compile Include="Common\VersionInfo.cs" />

0 commit comments

Comments
 (0)