Skip to content

Commit

Permalink
feat: performance marks
Browse files Browse the repository at this point in the history
  • Loading branch information
mshanemc committed Sep 27, 2023
1 parent 4adb3c2 commit f7f495f
Show file tree
Hide file tree
Showing 4 changed files with 205 additions and 11 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"node": ">=16.0.0"
},
"dependencies": {
"@oclif/core": "^2.15.0",
"@salesforce/core": "^5.2.0",
"@salesforce/kit": "^3.0.9",
"@salesforce/source-deploy-retrieve": "^9.7.15",
Expand Down
18 changes: 18 additions & 0 deletions src/shared/localShadowRepo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import * as fs from 'graceful-fs';
import { NamedPackageDir, Logger, SfError } from '@salesforce/core';
import { env } from '@salesforce/kit';
import * as git from 'isomorphic-git';
import { Performance } from '@oclif/core';
import { chunkArray, isLwcLocalOnlyTest, pathIsInFolder } from './functions';

/** returns the full path to where we store the shadow repo */
Expand Down Expand Up @@ -105,6 +106,7 @@ export class ShadowRepo {
*
*/
public async gitInit(): Promise<void> {
this.logger.trace(`initializing git repo at ${this.gitDir}`);
await fs.promises.mkdir(this.gitDir, { recursive: true });
try {
await git.init({ fs, dir: this.projectPath, gitdir: this.gitDir, defaultBranch: 'main' });
Expand Down Expand Up @@ -135,7 +137,10 @@ export class ShadowRepo {
* @returns StatusRow[]
*/
public async getStatus(noCache = false): Promise<StatusRow[]> {
this.logger.trace(`start: getStatus (noCache = ${noCache})`);

if (!this.status || noCache) {
const marker = Performance.mark('@salesforce/source-tracking', 'localShadowRepo.getStatus#withoutCache');
// iso-git uses relative, posix paths
// but packageDirs has already resolved / normalized them
// so we need to make them project-relative again and convert if windows
Expand Down Expand Up @@ -168,7 +173,9 @@ export class ShadowRepo {
if (this.isWindows) {
this.status = this.status.map((row) => [path.normalize(row[FILE]), row[HEAD], row[WORKDIR], row[3]]);
}
marker?.stop();
}
this.logger.trace(`done: getStatus (noCache = ${noCache})`);
return this.status;
}

Expand Down Expand Up @@ -247,16 +254,23 @@ export class ShadowRepo {
return 'no files to commit';
}

const marker = Performance.mark('@salesforce/source-tracking', 'localShadowRepo.commitChanges', {
deployedFiles: deployedFiles.length,
deletedFiles: deletedFiles.length,
});
// these are stored in posix/style/path format. We have to convert inbound stuff from windows
if (os.type() === 'Windows_NT') {
this.logger.trace('start: transforming windows paths to posix');
deployedFiles = deployedFiles.map((filepath) => path.normalize(filepath).split(path.sep).join(path.posix.sep));
deletedFiles = deletedFiles.map((filepath) => path.normalize(filepath).split(path.sep).join(path.posix.sep));
this.logger.trace('done: transforming windows paths to posix');
}

if (deployedFiles.length) {
const chunks = chunkArray([...new Set(deployedFiles)], this.maxFileAdd);
for (const chunk of chunks) {
try {
this.logger.debug(`adding ${chunk.length} files of ${deployedFiles.length} deployedFiles to git`);
// these need to be done sequentially (it's already batched) because isogit manages file locking
// eslint-disable-next-line no-await-in-loop
await git.add({
Expand Down Expand Up @@ -296,6 +310,8 @@ export class ShadowRepo {
}

try {
this.logger.trace('start: commitChanges git.commit');

const sha = await git.commit({
fs,
dir: this.projectPath,
Expand All @@ -307,9 +323,11 @@ export class ShadowRepo {
if (needsUpdatedStatus) {
await this.getStatus(true);
}
this.logger.trace('done: commitChanges git.commit');
return sha;
} catch (e) {
redirectToCliRepoError(e as Error);
}
marker?.stop();
}
}
16 changes: 15 additions & 1 deletion src/sourceTracking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import {
} from '@salesforce/source-deploy-retrieve';
// this is not exported by SDR (see the comments in SDR regarding its limitations)
import { filePathsFromMetadataComponent } from '@salesforce/source-deploy-retrieve/lib/src/utils/filePathGenerator';
import { Performance } from '@oclif/core';
import { RemoteSourceTrackingService, remoteChangeElementToChangeResult } from './shared/remoteSourceTrackingService';
import { ShadowRepo } from './shared/localShadowRepo';
import { throwIfConflicts, findConflictsInComponentSet, getDedupedConflictsFromChanges } from './shared/conflicts';
Expand Down Expand Up @@ -409,6 +410,9 @@ export class SourceTracking extends AsyncCreatable {
* @param options the files to update
*/
public async updateLocalTracking(options: LocalUpdateOptions): Promise<void> {
this.logger.trace('start: updateLocalTracking', options);
const marker = Performance.mark('@salesforce/source-tracking', 'SourceTracking.updateLocalTracking');
marker?.addDetails({ nonDeletes: options.files?.length ?? 0, deletes: options.deletedFiles?.length ?? 0 });
await this.ensureLocalTracking();

// relative paths make smaller trees AND isogit wants them relative
Expand Down Expand Up @@ -444,20 +448,25 @@ export class SourceTracking extends AsyncCreatable {
)
),
});
marker?.stop();
this.logger.trace('done: updateLocalTracking', options);
}

/**
* Mark remote source tracking files so say that we have received the latest version from the server
* Optional skip polling for the SourceMembers to exist on the server and be updated in local files
*/
public async updateRemoteTracking(fileResponses: RemoteSyncInput[], skipPolling = false): Promise<void> {
const marker = Performance.mark('@salesforce/source-tracking', 'SourceTracking.updateRemoteTracking');
marker?.addDetails({ fileResponseCount: fileResponses.length, skipPolling });
// false to explicitly NOT query until we do the polling
await this.ensureRemoteTracking(false);
if (!skipPolling) {
// poll to make sure we have the updates before syncing the ones from metadataKeys
await this.remoteSourceTrackingService.pollForSourceTracking(fileResponses);
}
await this.remoteSourceTrackingService.syncSpecifiedElements(fileResponses);
marker?.stop();
}

public async reReadLocalTrackingCache(): Promise<void> {
Expand Down Expand Up @@ -550,6 +559,8 @@ export class SourceTracking extends AsyncCreatable {
* Compares local and remote changes to detect conflicts
*/
public async getConflicts(): Promise<ChangeResult[]> {
const marker = Performance.mark('@salesforce/source-tracking', 'SourceTracking.getConflicts');

// we're going to need have both initialized
await Promise.all([this.ensureRemoteTracking(), this.ensureLocalTracking()]);

Expand All @@ -574,12 +585,15 @@ export class SourceTracking extends AsyncCreatable {
}
this.forceIgnore ??= ForceIgnore.findAndCreate(this.project.getDefaultPackage().path);

return getDedupedConflictsFromChanges({
const result = getDedupedConflictsFromChanges({
localChanges,
remoteChanges,
projectPath: this.projectPath,
forceIgnore: this.forceIgnore,
});

marker?.stop();
return result;
}

/**
Expand Down
Loading

0 comments on commit f7f495f

Please sign in to comment.