-
-
Notifications
You must be signed in to change notification settings - Fork 188
/
Copy pathSideBySideDiffBuilderBenchmark.cs
106 lines (91 loc) · 3.4 KB
/
SideBySideDiffBuilderBenchmark.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BenchmarkDotNet.Attributes;
using DiffPlex;
using DiffPlex.DiffBuilder;
using DiffPlex.DiffBuilder.Model;
namespace Perf.DiffPlex
{
[MemoryDiagnoser]
public class SideBySideDiffBuilderBenchmark
{
private static readonly Random Random = new Random();
private readonly SideBySideDiffBuilder sideBySideDiffer = new SideBySideDiffBuilder(new Differ());
private string oldText;
private string newText;
private const int MaxLineLength = 150;
private const double DifferenceAmount = 0.2;
private const int MaxLines = 8000;
[GlobalSetup]
public void SetUp()
{
var oldLines = GenerateLines(MaxLines);
var newLines = MakeDifferent(oldLines, DifferenceAmount);
oldText = Implode(oldLines, Environment.NewLine);
newText = Implode(newLines, Environment.NewLine);
}
[Benchmark]
public SideBySideDiffModel BuildDiffModel()
{
return sideBySideDiffer.BuildDiffModel(oldText, newText);
}
[Benchmark]
public SideBySideDiffModel BuildDiffModelIgnoreCase()
{
return sideBySideDiffer.BuildDiffModel(oldText, newText, true, true);
}
private static string Implode<T>(IEnumerable<T> enumerable, string delim)
{
if (enumerable == null) throw new ArgumentNullException(nameof(enumerable));
if (delim == null) throw new ArgumentNullException(nameof(delim));
bool loopedAtLeaseOnce = false;
StringBuilder result = new StringBuilder();
foreach (var item in enumerable)
{
loopedAtLeaseOnce = true;
result.Append(item + delim);
}
if (loopedAtLeaseOnce)
return result.Remove(result.Length - delim.Length, delim.Length).ToString();
return String.Empty;
}
private static IList<string> MakeDifferent(IList<string> lines, double differenceAmount)
{
var newLines = new List<string>();
foreach (var i in Enumerable.Range(0, lines.Count))
{
if(Random.NextDouble() <= differenceAmount)
{
// Either delete line or add different one
if(Random.Next(2) % 2 == 1)
{
newLines.Add(RandomString(MaxLineLength));
}
}
else
{
newLines.Add(lines[i]);
}
}
return newLines;
}
private static IList<string> GenerateLines(int lines)
{
return Enumerable.Range(0, lines).Select(i => RandomString(MaxLineLength)).ToList();
}
private static string RandomString(int maxLength)
{
var builder = new StringBuilder();
foreach (var i in Enumerable.Range(0, Random.Next(0, maxLength)))
{
var ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * Random.NextDouble() + 32 * Random.Next(2) + 65)));
builder.Append(ch);
}
if (Random.Next(2) % 2 == 0)
return builder.ToString().ToLower();
return builder.ToString();
}
}
}