// 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 = "";
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