Skip to content

Commit 35e0525

Browse files
committed
Added a heavy version of JS execution benchmark (rendering of Handlebars templates)
1 parent dcd18da commit 35e0525

19 files changed

+5811
-6
lines changed

test/JavaScriptEngineSwitcher.Benchmarks/Assert.cs

Lines changed: 82 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
using System;
2+
using System.Runtime.CompilerServices;
23
using System.Text;
34

45
namespace JavaScriptEngineSwitcher.Benchmarks
56
{
67
internal static class Assert
78
{
8-
public static void Equal(string expected, string actual)
9+
private static readonly char[] _lineBreakChars = new[] { '\r', '\n' };
10+
11+
12+
public static void Equal(string expected, string actual, bool ignoreLineBreaks = false)
913
{
10-
if (actual != expected)
14+
if (!EqualInternal(expected, actual, ignoreLineBreaks))
1115
{
1216
var messageBuilder = new StringBuilder();
1317
messageBuilder.AppendLine("Assert.Equal() Failure");
@@ -20,5 +24,81 @@ public static void Equal(string expected, string actual)
2024
throw new InvalidOperationException(errorMessage);
2125
}
2226
}
27+
28+
private static bool EqualInternal(string a, string b, bool ignoreLineBreaks)
29+
{
30+
if (!ignoreLineBreaks)
31+
{
32+
return a.Equals(b);
33+
}
34+
35+
if (ReferenceEquals(a, b))
36+
{
37+
return true;
38+
}
39+
40+
if (a == null || b == null)
41+
{
42+
return false;
43+
}
44+
45+
if (a.IndexOfAny(_lineBreakChars) == -1 && b.IndexOfAny(_lineBreakChars) == -1)
46+
{
47+
return a.Equals(b);
48+
}
49+
50+
int aIndex = 0;
51+
int aLength = a.Length;
52+
int bIndex = 0;
53+
int bLength = b.Length;
54+
55+
while (true)
56+
{
57+
if (aIndex >= aLength)
58+
{
59+
return bIndex >= bLength;
60+
}
61+
62+
if (bIndex >= bLength)
63+
{
64+
return false;
65+
}
66+
67+
char aChar = a[aIndex];
68+
char bChar = b[bIndex];
69+
70+
if (aChar != bChar)
71+
{
72+
if (Array.IndexOf(_lineBreakChars, aChar) != -1 && Array.IndexOf(_lineBreakChars, bChar) != -1)
73+
{
74+
ProcessLineBreaks(a, aChar, ref aIndex, aLength);
75+
ProcessLineBreaks(b, bChar, ref bIndex, bLength);
76+
77+
continue;
78+
}
79+
else
80+
{
81+
return false;
82+
}
83+
}
84+
85+
aIndex++;
86+
bIndex++;
87+
}
88+
}
89+
90+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
91+
private static void ProcessLineBreaks(string value, char charValue, ref int charIndex, int charCount)
92+
{
93+
if (charValue == '\r')
94+
{
95+
int nextCharIndex = charIndex + 1;
96+
if (nextCharIndex < charCount && value[nextCharIndex] == '\n')
97+
{
98+
charIndex++;
99+
}
100+
}
101+
charIndex++;
102+
}
23103
}
24104
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"lastName": "Пупкин",
3+
"firstName": "Василий",
4+
"patronymic": "Иванович",
5+
"address": {
6+
"postalCode": 392032,
7+
"country": "Россия",
8+
"state": "Тамбовская область",
9+
"city": "Тамбов",
10+
"street": "Магистральная",
11+
"houseNumber": "41к7",
12+
"floor": 8,
13+
"apartmentNumber": 115
14+
},
15+
"homePhone": "+7 (4752) 555-55-55",
16+
"mobilePhone": "+7 (999) 689-99-99",
17+
"email": "[email protected]",
18+
"website": "http:\/\/pupkin.narod.ru",
19+
"icq": 698426795
20+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<address>
2+
<strong><abbr title="Фамилия Имя Отчество">Ф.И.О.</abbr>:</strong> Пупкин Василий Иванович<br />
3+
<strong>Адрес:</strong>
4+
392032, Россия, Тамбовская область, Тамбов, ул. Магистральная, д. 41к7, кв. 115
5+
<br />
6+
<strong>Дом. тел.:</strong> +7 (4752) 555-55-55<br />
7+
<strong>Моб. тел.:</strong> +7 (999) 689-99-99<br />
8+
<strong>Email:</strong> <a href="mailto:[email protected]">[email protected]</a><br />
9+
<strong>Сайт:</strong> <a href="http://pupkin.narod.ru">http://pupkin.narod.ru</a><br />
10+
<strong>ICQ:</strong> <img src="http://status.icq.com/online.gif?icq=698426795&img=26" alt="ICQ статус">698426795
11+
</address>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<address>
2+
<strong><abbr title="Фамилия Имя Отчество">Ф.И.О.</abbr>:</strong> {{lastName}} {{firstName}} {{patronymic}}<br />
3+
<strong>Адрес:</strong>
4+
{{#with address}}
5+
{{postalCode}}, {{country}}, {{state}}, {{city}}, ул. {{street}}, д. {{houseNumber}}, кв. {{apartmentNumber}}
6+
{{/with}}
7+
<br />
8+
<strong>Дом. тел.:</strong> {{homePhone}}<br />
9+
<strong>Моб. тел.:</strong> {{mobilePhone}}<br />
10+
<strong>Email:</strong> <a href="mailto:{{email}}">{{email}}</a><br />
11+
<strong>Сайт:</strong> {{{link website website false}}}<br />
12+
<strong>ICQ:</strong> <img src="http://status.icq.com/online.gif?icq={{icq}}&img=26" alt="ICQ статус">{{icq}}
13+
</address>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"lastName": "Pupkin",
3+
"firstName": "Vasilii",
4+
"patronymic": "Ivanovich",
5+
"occupation": "Enikeyschik"
6+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<div class="greeting">
2+
Hello, Vasilii Ivanovich!
3+
</div>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<div class="greeting">
2+
{{!-- Welcome by first name and patronymic --}}
3+
Hello, {{firstName}} {{patronymic}}!
4+
</div>

0 commit comments

Comments
 (0)