Skip to content

Commit 46a07ef

Browse files
authored
Merge pull request #88 from hunterjm/feature/gzip
GZip Compression on hits files
2 parents d2effb3 + 3bc81ad commit 46a07ef

File tree

4 files changed

+67
-51
lines changed

4 files changed

+67
-51
lines changed

src/coverlet.core/Coverage.cs

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.IO.Compression;
45
using System.Linq;
56

67
using Coverlet.Core.Helpers;
@@ -152,43 +153,55 @@ private void CalculateCoverage()
152153
{
153154
foreach (var result in _results)
154155
{
155-
if (!File.Exists(result.HitsFilePath)) { continue; }
156-
var lines = InstrumentationHelper.ReadHitsFile(result.HitsFilePath);
157-
foreach (var row in lines)
156+
var i = 0;
157+
while (true)
158158
{
159-
var info = row.Split(',');
160-
// Ignore malformed lines
161-
if (info.Length != 4)
162-
continue;
159+
var file = $"{result.HitsFilePath}_compressed_{i}";
160+
if(!File.Exists(file)) break;
161+
162+
using (var fs = new FileStream(file, FileMode.Open))
163+
using (var gz = new GZipStream(fs, CompressionMode.Decompress))
164+
using (var sr = new StreamReader(gz))
165+
{
166+
string row;
167+
while ((row = sr.ReadLine()) != null)
168+
{
169+
var info = row.Split(',');
170+
// Ignore malformed lines
171+
if (info.Length != 4)
172+
continue;
163173

164-
bool isBranch = info[0] == "B";
174+
bool isBranch = info[0] == "B";
165175

166-
var document = result.Documents.FirstOrDefault(d => d.Path == info[1]);
167-
if (document == null)
168-
continue;
176+
var document = result.Documents.FirstOrDefault(d => d.Path == info[1]);
177+
if (document == null)
178+
continue;
169179

170-
int start = int.Parse(info[2]);
180+
int start = int.Parse(info[2]);
171181

172-
if (isBranch)
173-
{
174-
uint ordinal = uint.Parse(info[3]);
175-
var branch = document.Branches.First(b => b.Number == start && b.Ordinal == ordinal);
176-
if (branch.Hits != int.MaxValue)
177-
branch.Hits += branch.Hits + 1;
178-
}
179-
else
180-
{
181-
int end = int.Parse(info[3]);
182-
for (int j = start; j <= end; j++)
183-
{
184-
var line = document.Lines.First(l => l.Number == j);
185-
if (line.Hits != int.MaxValue)
186-
line.Hits = line.Hits + 1;
182+
if (isBranch)
183+
{
184+
uint ordinal = uint.Parse(info[3]);
185+
var branch = document.Branches.First(b => b.Number == start && b.Ordinal == ordinal);
186+
if (branch.Hits != int.MaxValue)
187+
branch.Hits += branch.Hits + 1;
188+
}
189+
else
190+
{
191+
int end = int.Parse(info[3]);
192+
for (int j = start; j <= end; j++)
193+
{
194+
var line = document.Lines.First(l => l.Number == j);
195+
if (line.Hits != int.MaxValue)
196+
line.Hits = line.Hits + 1;
197+
}
198+
}
187199
}
188200
}
189-
}
190201

191-
InstrumentationHelper.DeleteHitsFile(result.HitsFilePath);
202+
InstrumentationHelper.DeleteHitsFile(file);
203+
i++;
204+
}
192205
}
193206
}
194207
}

src/coverlet.core/CoverageTracker.cs

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4-
4+
using System.IO.Compression;
55
using Coverlet.Core.Attributes;
66
using Coverlet.Core.Extensions;
77

@@ -10,11 +10,13 @@ namespace Coverlet.Core
1010
public static class CoverageTracker
1111
{
1212
private static Dictionary<string, List<string>> _markers;
13+
private static Dictionary<string, int> _markerFileCount;
1314

1415
[ExcludeFromCoverage]
1516
static CoverageTracker()
1617
{
1718
_markers = new Dictionary<string, List<string>>();
19+
_markerFileCount = new Dictionary<string, int>();
1820
AppDomain.CurrentDomain.ProcessExit += new EventHandler(CurrentDomain_ProcessExit);
1921
}
2022

@@ -25,10 +27,20 @@ public static void MarkExecuted(string path, string marker)
2527
{
2628
_markers.TryAdd(path, new List<string>());
2729
_markers[path].Add(marker);
30+
_markerFileCount.TryAdd(path, 0);
2831
if (_markers[path].Count >= 100000)
2932
{
30-
File.AppendAllLines(path, _markers[path]);
33+
using (var fs = new FileStream($"{path}_compressed_{_markerFileCount[path]}", FileMode.OpenOrCreate))
34+
using (var gz = new GZipStream(fs, CompressionMode.Compress))
35+
using (var sw = new StreamWriter(gz))
36+
{
37+
foreach(var line in _markers[path])
38+
{
39+
sw.WriteLine(line);
40+
}
41+
}
3142
_markers[path].Clear();
43+
_markerFileCount[path] = _markerFileCount[path] + 1;
3244
}
3345
}
3446
}
@@ -37,7 +49,17 @@ public static void MarkExecuted(string path, string marker)
3749
public static void CurrentDomain_ProcessExit(object sender, EventArgs e)
3850
{
3951
foreach (var kvp in _markers)
40-
File.AppendAllLines(kvp.Key, kvp.Value);
52+
{
53+
using (var fs = new FileStream($"{kvp.Key}_compressed_{_markerFileCount[kvp.Key]}", FileMode.OpenOrCreate))
54+
using (var gz = new GZipStream(fs, CompressionMode.Compress))
55+
using (var sw = new StreamWriter(gz))
56+
{
57+
foreach(var line in kvp.Value)
58+
{
59+
sw.WriteLine(line);
60+
}
61+
}
62+
}
4163
}
4264
}
4365
}

src/coverlet.core/Helpers/InstrumentationHelper.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -71,15 +71,6 @@ public static void RestoreOriginalModule(string module, string identifier)
7171
}, retryStrategy, 10);
7272
}
7373

74-
public static IEnumerable<string> ReadHitsFile(string path)
75-
{
76-
// Retry hitting the hits file - retry up to 10 times, since the file could be locked
77-
// See: https://github.com/tonerdo/coverlet/issues/25
78-
var retryStrategy = CreateRetryStrategy();
79-
80-
return RetryHelper.Do(() => File.ReadLines(path), retryStrategy, 10);
81-
}
82-
8374
public static void DeleteHitsFile(string path)
8475
{
8576
// Retry hitting the hits file - retry up to 10 times, since the file could be locked

test/coverlet.core.tests/Helpers/InstrumentationHelperTests.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,6 @@ public void TestDontCopyCoverletDependency()
6161
Directory.Delete(directory.FullName, true);
6262
}
6363

64-
[Fact]
65-
public void TestReadHitsFile()
66-
{
67-
var tempFile = Path.GetTempFileName();
68-
Assert.True(File.Exists(tempFile));
69-
70-
var lines = InstrumentationHelper.ReadHitsFile(tempFile);
71-
Assert.NotNull(lines);
72-
}
73-
7464
[Fact]
7565
public void TestDeleteHitsFile()
7666
{

0 commit comments

Comments
 (0)