Skip to content

Commit 218e101

Browse files
committed
Add script to create a benchmark test using the compiler
1 parent f53e6a8 commit 218e101

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed

scripts/createBenchmark.ts

+124
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
/// <reference path="../src/harness/external/node.d.ts" />
2+
/// <reference path="../built/local/typescript.d.ts" />
3+
4+
import * as fs from "fs";
5+
import * as path from "path";
6+
import * as typescript from "typescript";
7+
declare var ts: typeof typescript;
8+
9+
var tsSourceDir = "../src";
10+
var tsBuildDir = "../built/local";
11+
var testOutputDir = "../built/benchmark";
12+
var sourceFiles = [
13+
"compiler/types.ts",
14+
"compiler/core.ts",
15+
"compiler/sys.ts",
16+
"compiler/diagnosticInformationMap.generated.ts",
17+
"compiler/scanner.ts",
18+
"compiler/binder.ts",
19+
"compiler/utilities.ts",
20+
"compiler/parser.ts",
21+
"compiler/checker.ts",
22+
"compiler/declarationEmitter.ts",
23+
"compiler/emitter.ts",
24+
"compiler/program.ts",
25+
"compiler/commandLineParser.ts",
26+
"compiler/tsc.ts"];
27+
28+
// .ts sources for the compiler, used as a test input
29+
var rawCompilerSources = "";
30+
sourceFiles.forEach(f=> {
31+
rawCompilerSources += "\r\n" + fs.readFileSync(path.join(tsSourceDir, f)).toString();
32+
});
33+
var compilerSoruces = `var compilerSources = ${JSON.stringify(rawCompilerSources) };`;
34+
35+
// .js code for the compiler, what we are actuallty testing
36+
var rawCompilerJavaScript = fs.readFileSync(path.join(tsBuildDir, "tsc.js")).toString();
37+
rawCompilerJavaScript = rawCompilerJavaScript.replace("ts.executeCommandLine(ts.sys.args);", "");
38+
39+
// lib.d.ts sources
40+
var rawLibSources = fs.readFileSync(path.join(tsBuildDir, "lib.d.ts")).toString();
41+
var libSoruces = `var libSources = ${JSON.stringify(rawLibSources) };`;
42+
43+
// write test output
44+
if (!fs.existsSync(testOutputDir)) {
45+
fs.mkdirSync(testOutputDir);
46+
}
47+
48+
// 1. compiler ts sources, used to test
49+
fs.writeFileSync(
50+
path.join(testOutputDir, "compilerSources.js"),
51+
`${ compilerSoruces } \r\n ${ libSoruces }`);
52+
53+
// 2. the compiler js sources + a call the compiler
54+
fs.writeFileSync(
55+
path.join(testOutputDir, "benchmarktsc.js"),
56+
`${ rawCompilerJavaScript }\r\n${ compile.toString() }\r\ncompile(compilerSources, libSources);`);
57+
58+
// 3. test html file to drive the test
59+
fs.writeFileSync(
60+
path.join(testOutputDir, "benchmarktsc.html"),
61+
`<!DOCTYPE HTML>
62+
<html>
63+
<head>
64+
<meta content="IE=Edge" http-equiv="X-UA-Compatible">
65+
<title>Typescript 1.1 Compiler</title>
66+
<meta content="text/html; charset=ISO-8859-1" http-equiv="content-type">
67+
</head>
68+
<body>
69+
<div><span>Status: </span><span id="status">Running</span></div>
70+
<div id="main"><span>End-to-End Time: </span><span id="totalTime">N/A</span></div>
71+
<script>
72+
var startTime = performance.now();
73+
</script>
74+
<script src="compilerSources.js"></script>
75+
<script src="benchmarktsc.js"></script>
76+
<script>
77+
var endTime = performance.now();
78+
document.getElementById("totalTime").innerHTML = parseInt(endTime - startTime, 10);
79+
document.getElementById("status").innerHTML = "DONE";
80+
</script>
81+
</body>
82+
</html>`);
83+
84+
function compile(compilerSources, librarySources) {
85+
var program = ts.createProgram(
86+
["lib.d.ts", "compiler.ts"],
87+
{
88+
noResolve: true,
89+
out: "compiler.js",
90+
removeComments: true,
91+
target: ts.ScriptTarget.ES3
92+
}, {
93+
getDefaultLibFileName: () => "lib.d.ts",
94+
getSourceFile: (filename, languageVersion) => {
95+
var source: string;
96+
if (filename === "lib.d.ts") source = librarySources;
97+
else if (filename === "compiler.ts") source = compilerSources;
98+
else console.error("Unexpected read file request: " + filename);
99+
100+
return ts.createSourceFile(filename, source, languageVersion);
101+
},
102+
writeFile: (filename, data, writeByteOrderMark) => {
103+
if (filename !== "compiler.js")
104+
console.error("Unexpected write file request: " + filename);
105+
// console.log(data);
106+
},
107+
getCurrentDirectory: () => "",
108+
getCanonicalFileName: (filename) => filename,
109+
useCaseSensitiveFileNames: () => false,
110+
getNewLine: () => "\r\n"
111+
});
112+
113+
var emitOutput = program.emit();
114+
115+
var errors = program.getSyntacticDiagnostics()
116+
.concat(program.getSemanticDiagnostics())
117+
.concat(program.getGlobalDiagnostics())
118+
.concat(emitOutput.diagnostics);
119+
120+
if (errors.length) {
121+
console.error("Unexpected errors.");
122+
errors.forEach(e=> console.log(`${e.code}: ${e.messageText}`))
123+
}
124+
}

0 commit comments

Comments
 (0)