From 218e1012a2e671ca70be3c1b9edf0db092e4c5c2 Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Fri, 24 Apr 2015 11:00:14 -0700 Subject: [PATCH] Add script to create a benchmark test using the compiler --- scripts/createBenchmark.ts | 124 +++++++++++++++++++++++++++++++++++++ 1 file changed, 124 insertions(+) create mode 100644 scripts/createBenchmark.ts diff --git a/scripts/createBenchmark.ts b/scripts/createBenchmark.ts new file mode 100644 index 0000000000000..659df53c9156a --- /dev/null +++ b/scripts/createBenchmark.ts @@ -0,0 +1,124 @@ +/// +/// + +import * as fs from "fs"; +import * as path from "path"; +import * as typescript from "typescript"; +declare var ts: typeof typescript; + +var tsSourceDir = "../src"; +var tsBuildDir = "../built/local"; +var testOutputDir = "../built/benchmark"; +var sourceFiles = [ + "compiler/types.ts", + "compiler/core.ts", + "compiler/sys.ts", + "compiler/diagnosticInformationMap.generated.ts", + "compiler/scanner.ts", + "compiler/binder.ts", + "compiler/utilities.ts", + "compiler/parser.ts", + "compiler/checker.ts", + "compiler/declarationEmitter.ts", + "compiler/emitter.ts", + "compiler/program.ts", + "compiler/commandLineParser.ts", + "compiler/tsc.ts"]; + +// .ts sources for the compiler, used as a test input +var rawCompilerSources = ""; +sourceFiles.forEach(f=> { + rawCompilerSources += "\r\n" + fs.readFileSync(path.join(tsSourceDir, f)).toString(); +}); +var compilerSoruces = `var compilerSources = ${JSON.stringify(rawCompilerSources) };`; + +// .js code for the compiler, what we are actuallty testing +var rawCompilerJavaScript = fs.readFileSync(path.join(tsBuildDir, "tsc.js")).toString(); +rawCompilerJavaScript = rawCompilerJavaScript.replace("ts.executeCommandLine(ts.sys.args);", ""); + +// lib.d.ts sources +var rawLibSources = fs.readFileSync(path.join(tsBuildDir, "lib.d.ts")).toString(); +var libSoruces = `var libSources = ${JSON.stringify(rawLibSources) };`; + +// write test output +if (!fs.existsSync(testOutputDir)) { + fs.mkdirSync(testOutputDir); +} + +// 1. compiler ts sources, used to test +fs.writeFileSync( + path.join(testOutputDir, "compilerSources.js"), + `${ compilerSoruces } \r\n ${ libSoruces }`); + +// 2. the compiler js sources + a call the compiler +fs.writeFileSync( + path.join(testOutputDir, "benchmarktsc.js"), + `${ rawCompilerJavaScript }\r\n${ compile.toString() }\r\ncompile(compilerSources, libSources);`); + +// 3. test html file to drive the test +fs.writeFileSync( + path.join(testOutputDir, "benchmarktsc.html"), + ` + + + + Typescript 1.1 Compiler + + + +
Status: Running
+
End-to-End Time: N/A
+ + + + + +`); + +function compile(compilerSources, librarySources) { + var program = ts.createProgram( + ["lib.d.ts", "compiler.ts"], + { + noResolve: true, + out: "compiler.js", + removeComments: true, + target: ts.ScriptTarget.ES3 + }, { + getDefaultLibFileName: () => "lib.d.ts", + getSourceFile: (filename, languageVersion) => { + var source: string; + if (filename === "lib.d.ts") source = librarySources; + else if (filename === "compiler.ts") source = compilerSources; + else console.error("Unexpected read file request: " + filename); + + return ts.createSourceFile(filename, source, languageVersion); + }, + writeFile: (filename, data, writeByteOrderMark) => { + if (filename !== "compiler.js") + console.error("Unexpected write file request: " + filename); + // console.log(data); + }, + getCurrentDirectory: () => "", + getCanonicalFileName: (filename) => filename, + useCaseSensitiveFileNames: () => false, + getNewLine: () => "\r\n" + }); + + var emitOutput = program.emit(); + + var errors = program.getSyntacticDiagnostics() + .concat(program.getSemanticDiagnostics()) + .concat(program.getGlobalDiagnostics()) + .concat(emitOutput.diagnostics); + + if (errors.length) { + console.error("Unexpected errors."); + errors.forEach(e=> console.log(`${e.code}: ${e.messageText}`)) + } +}