Skip to content
This repository has been archived by the owner on Nov 16, 2023. It is now read-only.

Commit

Permalink
Log simple performance data from expect rule (#227)
Browse files Browse the repository at this point in the history
* Log simple performance data from expect rule

1. This is not the right thing for a tslint rule to do.
2. The log isn't written to a very good place.

I think (1) is unavoidable; (2) should be fixed before shipping this
change.

* Use same dir as installs

* Remove console.log

Instead, consumers should read the output files and print results
themselves.
  • Loading branch information
sandersn authored Apr 11, 2019
1 parent 068244f commit e2fbd6d
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/rules/expectRule.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { existsSync, readFileSync } from "fs";
import { dirname, resolve as resolvePath } from "path";
import { existsSync, readFileSync, writeFileSync, mkdirSync } from "fs";
import { dirname, basename, resolve as resolvePath, join } from "path";
import os = require("os");
import * as Lint from "tslint";
import * as TsType from "typescript";
import { last } from "../util";
Expand All @@ -9,6 +10,8 @@ type SourceFile = TsType.SourceFile;

// Based on https://github.com/danvk/typings-checker

const perfDir = join(os.homedir(), ".dts", "perf");

export class Rule extends Lint.Rules.TypedRule {
/* tslint:disable:object-literal-sort-keys */
static metadata: Lint.IRuleMetadata = {
Expand Down Expand Up @@ -39,28 +42,39 @@ export class Rule extends Lint.Rules.TypedRule {

const { tsconfigPath, versionsToTest } = options;

const getFailures = ({ versionName, path }: VersionToTest, nextHigherVersion: string | undefined) => {
const getFailures = ({ versionName, path }: VersionToTest, nextHigherVersion: string | undefined, writeOutput: boolean) => {
const ts = require(path);
const program = getProgram(tsconfigPath, ts, versionName, lintProgram);
return this.applyWithFunction(sourceFile, ctx => walk(ctx, program, ts, versionName, nextHigherVersion));
const failures = this.applyWithFunction(sourceFile, ctx => walk(ctx, program, ts, versionName, nextHigherVersion));
if (writeOutput) {
const packageName = basename(dirname(tsconfigPath));
if (!packageName.match(/v\d+/) && !packageName.match(/ts\d\.\d/)) {
const d = { [packageName]: { typeCount: (program as any).getTypeCount(), memory: ts.sys.getMemoryUsage ? ts.sys.getMemoryUsage() : 0 } };
if (!existsSync(perfDir)) {
mkdirSync(perfDir);
}
writeFileSync(join(perfDir, `${packageName}.json`), JSON.stringify(d));
}
}
return failures;
};

const maxFailures = getFailures(last(versionsToTest), undefined);
const maxFailures = getFailures(last(versionsToTest), undefined, /*writeOutput*/ true);
if (maxFailures.length) {
return maxFailures;
}

// As an optimization, check the earliest version for errors;
// assume that if it works on min and max, it works for everything in between.
const minFailures = getFailures(versionsToTest[0], undefined);
const minFailures = getFailures(versionsToTest[0], undefined, /*writeOutput*/ false);
if (!minFailures.length) {
return [];
}

// There are no failures in the max version, but there are failures in the min version.
// Work backward to find the newest version with failures.
for (let i = versionsToTest.length - 2; i >= 0; i--) {
const failures = getFailures(versionsToTest[i], options.versionsToTest[i + 1].versionName);
const failures = getFailures(versionsToTest[i], options.versionsToTest[i + 1].versionName, /*writeOutput*/ false);
if (failures.length) {
return failures;
}
Expand Down Expand Up @@ -129,7 +143,6 @@ function walk(
const checker = program.getTypeChecker();
// Don't care about emit errors.
const diagnostics = ts.getPreEmitDiagnostics(program, sourceFile);

if (sourceFile.isDeclarationFile || !/\$Expect(Type|Error)/.test(sourceFile.text)) {
// Normal file.
for (const diagnostic of diagnostics) {
Expand Down

0 comments on commit e2fbd6d

Please sign in to comment.