-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Amine Ben hammou
committed
Jul 1, 2023
1 parent
78d7ab1
commit 4a6812d
Showing
14 changed files
with
434 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
{ | ||
"semi": false, | ||
"printWidth": 155, | ||
"singleQuote": true, | ||
"trailingComma": "es5", | ||
"bracketSpacing": true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,42 @@ | ||
export {measurePath, measureCode} from './functions' | ||
import * as ts from 'typescript' | ||
import { normalize } from 'path' | ||
|
||
export function measureCode(path: string, code: string) { | ||
const sourceFile = ts.createSourceFile(path, code, ts.ScriptTarget.ES2015, true) | ||
const host = addSourceFileToHost(ts.createCompilerHost({}), path, sourceFile) | ||
const program = ts.createProgram([path], { noEmitOnError: true }, host) | ||
return measure(program, sourceFile) | ||
} | ||
|
||
export function measurePath(filePath: string) { | ||
const program = ts.createProgram([filePath], { noEmitOnError: true }) | ||
const sourceFile = program.getSourceFile(filePath) | ||
if (!sourceFile) { | ||
throw new Error(`No source file found at path: ${filePath}`) | ||
} | ||
return measure(program, sourceFile) | ||
} | ||
|
||
function measure(program: ts.Program, sourceFile: ts.SourceFile) { | ||
const start = performance.now() | ||
const diagnostics = ts.getPreEmitDiagnostics(program, sourceFile) | ||
const duration = performance.now() - start | ||
if (diagnostics.length > 0) { | ||
const error = ts.formatDiagnosticsWithColorAndContext(diagnostics, { | ||
getCanonicalFileName: normalize, | ||
getCurrentDirectory: ts.sys.getCurrentDirectory, | ||
getNewLine: () => ts.sys.newLine, | ||
}) | ||
throw new Error(error) | ||
} | ||
return duration | ||
} | ||
|
||
function addSourceFileToHost(host: ts.CompilerHost, path: string, sourceFile: ts.SourceFile) { | ||
const getSourceFile = host.getSourceFile | ||
host.getSourceFile = (filename, languageVersion) => { | ||
if (filename === path) return sourceFile | ||
return getSourceFile(filename, languageVersion) | ||
} | ||
return host | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { add } from './math' | ||
|
||
console.log(add(1, 2)) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { tuple } from 'just-types' | ||
|
||
const x: tuple.Permutation<[1, 2, 3, 4, 5, 6, 7]> = [1, 4, 7, 2, 6, 3, 5] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export function add(x: number, y: number) { | ||
return x + y | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import assert from 'assert' | ||
import { test } from './utils' | ||
import { measurePath, measureCode } from '../src' | ||
|
||
test('measurePath: empty file', () => { | ||
const duration = measurePath('tests/empty.ts') | ||
console.log(duration) | ||
assert.equal(duration < 500, true) | ||
}) | ||
|
||
test('measurePath: simple file', () => { | ||
const duration = measurePath('tests/math.ts') | ||
console.log(duration) | ||
assert.equal(duration < 500, true) | ||
}) | ||
|
||
test('measurePath: file importing other files', () => { | ||
const duration = measurePath('tests/computation.ts') | ||
console.log(duration) | ||
assert.equal(duration < 500, true) | ||
}) | ||
|
||
test('measurePath: heavy types', () => { | ||
const duration = measurePath('tests/heavy.ts') | ||
console.log(duration) | ||
assert.equal(duration > 300, true) | ||
assert.equal(duration < 2_000, true) | ||
}) | ||
|
||
test('measurePath: file with errors', () => { | ||
assert.throws(() => measurePath('tests/wrong.ts')) | ||
}) | ||
|
||
test('measureCode: empty code', () => { | ||
const duration = measureCode(__filename, '') | ||
console.log(duration) | ||
assert.equal(duration < 500, true) | ||
}) | ||
|
||
test('measureCode: simple code', () => { | ||
const duration = measureCode( | ||
__filename, | ||
/*ts*/ ` | ||
export function add(x: number, y: number) { | ||
return x + y | ||
} | ||
` | ||
) | ||
console.log(duration) | ||
assert.equal(duration < 500, true) | ||
}) | ||
|
||
test('measureCode: file importing other files', () => { | ||
const duration = measureCode( | ||
__filename, | ||
/*ts*/ ` | ||
import { add } from './math' | ||
console.log(add(1, 2)) | ||
` | ||
) | ||
console.log(duration) | ||
assert.equal(duration < 500, true) | ||
}) | ||
|
||
test('measureCode: heavy types', () => { | ||
const duration = measureCode( | ||
__filename, | ||
/*ts*/ ` | ||
import { tuple } from 'just-types' | ||
const x: tuple.Permutation<[1, 2, 3, 4, 5, 6, 7]> = [1, 4, 7, 2, 6, 3, 5] | ||
` | ||
) | ||
console.log(duration) | ||
assert.equal(duration > 300, true) | ||
assert.equal(duration < 2_000, true) | ||
}) | ||
|
||
test('measureCode: file with errors', () => { | ||
assert.throws(() => | ||
measureCode( | ||
__filename, | ||
/*ts*/ ` | ||
import { add } from './math' | ||
console.log(add('foo', 2)) | ||
` | ||
) | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export function test(name: string, fn: () => any) { | ||
try { | ||
fn() | ||
console.log(`✅`, name) | ||
} catch (err) { | ||
console.error(`❌`, name) | ||
console.error(err) | ||
process.exit(1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { add } from './math' | ||
|
||
console.log(add('foo', 2)) |
Oops, something went wrong.