Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 353a9d9

Browse files
vsavkinFrozenPandaz
authored andcommittedAug 13, 2020
feat(core): add perf logging
1 parent 2871bd1 commit 353a9d9

File tree

6 files changed

+52
-3
lines changed

6 files changed

+52
-3
lines changed
 

Diff for: ‎packages/cli/lib/init-local.ts

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { parseRunOneOptions } from './parse-run-one-options';
1010
*/
1111
process.env.NX_CLI_SET = 'true';
1212
export function initLocal(workspace: Workspace) {
13+
require('@nrwl/workspace/' + 'src/utils/perf-logging');
1314
const supportedNxCommands = require('@nrwl/workspace/' +
1415
'src/command-line/supported-nx-commands').supportedNxCommands;
1516
const runOpts = runOneOptions(workspace);

Diff for: ‎packages/workspace/src/core/file-utils.ts

+16-2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import { jsonDiff } from '../utils/json-diff';
1111
import { ProjectGraphNode } from './project-graph';
1212
import { Environment, NxJson } from './shared-interfaces';
1313
import { defaultFileHasher } from './hasher/file-hasher';
14+
import { performance } from 'perf_hooks';
1415

1516
const ignore = require('ignore');
1617

@@ -206,11 +207,19 @@ export function rootWorkspaceFileData(): FileData[] {
206207

207208
export function readWorkspaceFiles(): FileData[] {
208209
const workspaceJson = readWorkspaceJson();
210+
performance.mark('read workspace files:start');
209211

210212
if (defaultFileHasher.usesGitForHashing) {
211-
return defaultFileHasher
213+
const r = defaultFileHasher
212214
.allFiles()
213215
.map((f) => getFileData(`${appRootPath}/${f}`));
216+
performance.mark('read workspace files:end');
217+
performance.measure(
218+
'read workspace files',
219+
'read workspace files:start',
220+
'read workspace files:end'
221+
);
222+
return r;
214223
} else {
215224
const files = [];
216225
files.push(...rootWorkspaceFileData());
@@ -224,7 +233,12 @@ export function readWorkspaceFiles(): FileData[] {
224233
const project = workspaceJson.projects[projectName];
225234
files.push(...allFilesInDir(`${appRootPath}/${project.root}`));
226235
});
227-
236+
performance.mark('read workspace files:end');
237+
performance.measure(
238+
'read workspace files',
239+
'read workspace files:start',
240+
'read workspace files:end'
241+
);
228242
return files;
229243
}
230244
}

Diff for: ‎packages/workspace/src/core/hasher/file-hasher.ts

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { getFileHashes } from './git-hasher';
22
import { readFileSync } from 'fs';
33
import { defaultHashing, HashingImp } from './hashing-impl';
44
import { appRootPath } from '../../utils/app-root';
5+
import { performance } from 'perf_hooks';
56

67
type PathAndTransformer = {
78
path: string;
@@ -26,9 +27,16 @@ export class FileHasher {
2627
}
2728

2829
init() {
30+
performance.mark('init hashing:start');
2931
this.fileHashes = {};
3032
this.getHashesFromGit();
3133
this.usesGitForHashing = Object.keys(this.fileHashes).length > 0;
34+
performance.mark('init hashing:end');
35+
performance.measure(
36+
'init hashing',
37+
'init hashing:start',
38+
'init hashing:end'
39+
);
3240
}
3341

3442
hashFile(path: string, transformer: (x: string) => string | null = null) {

Diff for: ‎packages/workspace/src/core/nx-deps/nx-deps-cache.ts

+8
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import {
1515
writeJsonFile,
1616
} from '../../utils/fileutils';
1717
import { FileMap } from '@nrwl/workspace/src/core/file-graph';
18+
import { performance } from 'perf_hooks';
1819

1920
export interface ProjectGraphCache {
2021
version: string;
@@ -26,6 +27,7 @@ export interface ProjectGraphCache {
2627
const nxDepsDir = join(appRootPath, 'node_modules', '.cache', 'nx');
2728
const nxDepsPath = join(nxDepsDir, 'nxdeps.json');
2829
export function readCache(): false | ProjectGraphCache {
30+
performance.mark('read cache:start');
2931
try {
3032
if (!existsSync(nxDepsDir)) {
3133
fsExtra.ensureDirSync(nxDepsDir);
@@ -47,19 +49,25 @@ export function readCache(): false | ProjectGraphCache {
4749
}
4850

4951
const data = fileExists(nxDepsPath) ? readJsonFile(nxDepsPath) : null;
52+
53+
performance.mark('read cache:end');
54+
performance.measure('read cache', 'read cache:start', 'read cache:end');
5055
return data ? data : false;
5156
}
5257

5358
export function writeCache(
5459
rootFiles: FileData[],
5560
projectGraph: ProjectGraph
5661
): void {
62+
performance.mark('write cache:start');
5763
writeJsonFile(nxDepsPath, {
5864
version: '2.0',
5965
rootFiles,
6066
nodes: projectGraph.nodes,
6167
dependencies: projectGraph.dependencies,
6268
});
69+
performance.mark('write cache:end');
70+
performance.measure('write cache', 'write cache:start', 'write cache:end');
6371
}
6472

6573
export function differentFromCache(

Diff for: ‎packages/workspace/src/core/project-graph/project-graph.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import {
2929
writeCache,
3030
} from '../nx-deps/nx-deps-cache';
3131
import { NxJson } from '../shared-interfaces';
32+
import { performance } from 'perf_hooks';
3233

3334
export function createProjectGraph(
3435
workspaceJson = readWorkspaceJson(),
@@ -83,6 +84,7 @@ function buildProjectGraph(
8384
fileRead: (s: string) => string,
8485
projectGraph: ProjectGraph
8586
) {
87+
performance.mark('build project graph:start');
8688
const builder = new ProjectGraphBuilder(projectGraph);
8789
const buildNodesFns: BuildNodes[] = [
8890
buildWorkspaceProjectNodes,
@@ -97,5 +99,12 @@ function buildProjectGraph(
9799
buildDependenciesFns.forEach((f) =>
98100
f(ctx, builder.nodes, builder.addDependency.bind(builder), fileRead)
99101
);
100-
return builder.build();
102+
const r = builder.build();
103+
performance.mark('build project graph:end');
104+
performance.measure(
105+
'build project graph',
106+
'build project graph:start',
107+
'build project graph:end'
108+
);
109+
return r;
101110
}

Diff for: ‎packages/workspace/src/utils/perf-logging.ts

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { PerformanceObserver } from 'perf_hooks';
2+
3+
if (process.env.NX_PERF_LOGGING) {
4+
const obs = new PerformanceObserver((list) => {
5+
const entry = list.getEntries()[0];
6+
console.log(`Time for '${entry.name}'`, entry.duration);
7+
});
8+
obs.observe({ entryTypes: ['measure'], buffered: false });
9+
}

0 commit comments

Comments
 (0)
Please sign in to comment.