forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSunSpider.cs
More file actions
151 lines (129 loc) · 5.5 KB
/
SunSpider.cs
File metadata and controls
151 lines (129 loc) · 5.5 KB
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net.Http;
using System.Text;
namespace Microsoft.ClearScript.Test
{
internal static class SunSpider
{
private const string version = "sunspider-1.0.2";
private const string baseUrl = "https://webkit.org/perf/" + version + "/" + version + "/";
private const int repeatCount = 10;
private const string scriptBegin = "<script>";
private const string scriptEnd = "</script>";
private static bool gotCode;
private static string testPrefix;
private static string testContents;
public static void RunSuite(ScriptEngine engine, bool quiet)
{
// download raw test code if necessary
if (!gotCode)
{
if (!quiet) Console.Write("Downloading code... ");
testPrefix = DownloadFileAsString("sunspider-test-prefix.js");
testContents = DownloadFileAsString("sunspider-test-contents.js");
if (!quiet) Console.WriteLine("Done");
gotCode = true;
}
// set up dummy HTML DOM
var mockDOM = new MockDOM();
engine.AccessContext = typeof(SunSpider);
engine.AddHostObject("document", mockDOM);
engine.AddHostObject("window", mockDOM);
engine.AddHostObject("parent", mockDOM);
// load raw test code
engine.Execute(testPrefix);
engine.Execute(testContents);
engine.Execute(@"
function ClearScriptCleanup() {
delete Array.prototype.toJSONString;
delete Boolean.prototype.toJSONString;
delete Date.prototype.toJSONString;
delete Number.prototype.toJSONString;
delete Object.prototype.toJSONString;
}
");
// initialize
var testCount = (int)engine.Script.tests.length;
var testIndices = Enumerable.Range(0, testCount).ToList();
var repeatIndices = Enumerable.Range(0, repeatCount).ToList();
// run warmup cycle
if (!quiet) Console.Write("Warming up... ");
testIndices.ForEach(testIndex => RunTest(engine, mockDOM, testIndex));
if (!quiet) Console.WriteLine("Done");
// run main test
var results = repeatIndices.Select(index => new Dictionary<string, int>()).ToArray();
repeatIndices.ForEach(repeatIndex =>
{
if (!quiet) Console.Write("Running iteration {0}... ", repeatIndex + 1);
testIndices.ForEach(testIndex =>
{
var name = (string)engine.Script.tests[testIndex];
results[repeatIndex][name] = RunTest(engine, mockDOM, testIndex);
});
if (!quiet) Console.WriteLine("Done");
});
// show results
if (!quiet)
{
var resultString = new StringBuilder("{\"v\":\"" + version + "\",");
results[0].Keys.ToList().ForEach(name =>
{
resultString.Append("\"" + name + "\":[");
resultString.Append(string.Join(",", repeatIndices.Select(repeatIndex => results[repeatIndex][name])));
resultString.Append("],");
});
resultString.Length -= 1;
resultString.Append("}");
Process.Start(new ProcessStartInfo((new Uri(baseUrl + "results.html?" + resultString)).AbsoluteUri) {UseShellExecute = true});
}
}
private static int RunTest(ScriptEngine engine, MockDOM mockDOM, int index)
{
// extract test script
var name = (string)engine.Script.tests[index];
var html = (string)engine.Script.testContents[index];
var start = html.IndexOf(scriptBegin, StringComparison.OrdinalIgnoreCase) + scriptBegin.Length;
var end = html.IndexOf(scriptEnd, StringComparison.OrdinalIgnoreCase);
var script = html.Substring(start, end - start);
// execute test
var result = int.MinValue;
mockDOM.RecordAction = value => result = value;
engine.Execute(name, script);
engine.Script.ClearScriptCleanup();
return result;
}
private static string DownloadFileAsString(string name)
{
using (var client = new HttpClient())
{
return client.GetStringAsync(baseUrl + name).Result;
}
}
// ReSharper disable InconsistentNaming
// ReSharper disable UnusedMember.Local
// ReSharper disable UnusedParameter.Local
private sealed class MockDOM
{
public Action<int> RecordAction { private get; set; }
public object getElementById(string id)
{
return this;
}
public string innerHTML { get; set; }
public object parent => this;
public void recordResult(int time)
{
RecordAction(time);
}
public object onerror { get; set; }
}
// ReSharper restore UnusedParameter.Local
// ReSharper restore UnusedMember.Local
// ReSharper restore InconsistentNaming
}
}