profiler itself might impact performance

This commit is contained in:
fyears 2024-05-18 00:03:21 +08:00
parent fa17ea074b
commit b584f89a95
2 changed files with 64 additions and 63 deletions

View File

@ -62,7 +62,7 @@ import { getClient } from "./fsGetter";
import { FakeFsLocal } from "./fsLocal"; import { FakeFsLocal } from "./fsLocal";
import { DEFAULT_WEBDIS_CONFIG } from "./fsWebdis"; import { DEFAULT_WEBDIS_CONFIG } from "./fsWebdis";
import { changeMobileStatusBar } from "./misc"; import { changeMobileStatusBar } from "./misc";
import { DEFAULT_PROFILER_CONFIG, Profiler } from "./profiler"; import { DEFAULT_PROFILER_CONFIG, type Profiler } from "./profiler";
import { syncer } from "./sync"; import { syncer } from "./sync";
const DEFAULT_SETTINGS: RemotelySavePluginSettings = { const DEFAULT_SETTINGS: RemotelySavePluginSettings = {
@ -152,11 +152,12 @@ export default class RemotelySavePlugin extends Plugin {
appContainerObserver?: MutationObserver; appContainerObserver?: MutationObserver;
async syncRun(triggerSource: SyncTriggerSourceType = "manual") { async syncRun(triggerSource: SyncTriggerSourceType = "manual") {
const profiler = new Profiler( // const profiler = new Profiler(
undefined, // undefined,
this.settings.profiler?.enablePrinting ?? false, // this.settings.profiler?.enablePrinting ?? false,
this.settings.profiler?.recordSize ?? false // this.settings.profiler?.recordSize ?? false
); // );
const profiler: Profiler | undefined = undefined;
const fsLocal = new FakeFsLocal( const fsLocal = new FakeFsLocal(
this.app.vault, this.app.vault,
this.settings.syncConfigDir ?? false, this.settings.syncConfigDir ?? false,
@ -419,7 +420,7 @@ export default class RemotelySavePlugin extends Plugin {
); );
fsEncrypt.closeResources(); fsEncrypt.closeResources();
profiler.clear(); (profiler as Profiler | undefined)?.clear();
this.syncEvent?.trigger("SYNC_DONE"); this.syncEvent?.trigger("SYNC_DONE");
} }

View File

@ -155,13 +155,13 @@ const ensembleMixedEnties = async (
fsEncrypt: FakeFsEncrypt, fsEncrypt: FakeFsEncrypt,
serviceType: SUPPORTED_SERVICES_TYPE, serviceType: SUPPORTED_SERVICES_TYPE,
profiler: Profiler profiler: Profiler | undefined
): Promise<SyncPlanType> => { ): Promise<SyncPlanType> => {
profiler.addIndent(); profiler?.addIndent();
profiler.insert("ensembleMixedEnties: enter"); profiler?.insert("ensembleMixedEnties: enter");
profiler.insertSize("sizeof localEntityList", localEntityList); profiler?.insertSize("sizeof localEntityList", localEntityList);
profiler.insertSize("sizeof prevSyncEntityList", prevSyncEntityList); profiler?.insertSize("sizeof prevSyncEntityList", prevSyncEntityList);
profiler.insertSize("sizeof remoteEntityList", remoteEntityList); profiler?.insertSize("sizeof remoteEntityList", remoteEntityList);
const finalMappings: SyncPlanType = {}; const finalMappings: SyncPlanType = {};
@ -190,8 +190,8 @@ const ensembleMixedEnties = async (
}; };
} }
profiler.insert("ensembleMixedEnties: finish remote"); profiler?.insert("ensembleMixedEnties: finish remote");
profiler.insertSize("sizeof finalMappings", finalMappings); profiler?.insertSize("sizeof finalMappings", finalMappings);
if (Object.keys(finalMappings).length === 0 || localEntityList.length === 0) { if (Object.keys(finalMappings).length === 0 || localEntityList.length === 0) {
// Special checking: // Special checking:
@ -231,8 +231,8 @@ const ensembleMixedEnties = async (
} }
} }
profiler.insert("ensembleMixedEnties: finish prevSync"); profiler?.insert("ensembleMixedEnties: finish prevSync");
profiler.insertSize("sizeof finalMappings", finalMappings); profiler?.insertSize("sizeof finalMappings", finalMappings);
// local has to be last // local has to be last
// because we want to get keyEnc based on the remote // because we want to get keyEnc based on the remote
@ -265,14 +265,14 @@ const ensembleMixedEnties = async (
} }
} }
profiler.insert("ensembleMixedEnties: finish local"); profiler?.insert("ensembleMixedEnties: finish local");
profiler.insertSize("sizeof finalMappings", finalMappings); profiler?.insertSize("sizeof finalMappings", finalMappings);
// console.debug("in the end of ensembleMixedEnties, finalMappings is:"); // console.debug("in the end of ensembleMixedEnties, finalMappings is:");
// console.debug(finalMappings); // console.debug(finalMappings);
profiler.insert("ensembleMixedEnties: exit"); profiler?.insert("ensembleMixedEnties: exit");
profiler.removeIndent(); profiler?.removeIndent();
return finalMappings; return finalMappings;
}; };
@ -287,24 +287,24 @@ const getSyncPlanInplace = async (
skipSizeLargerThan: number, skipSizeLargerThan: number,
conflictAction: ConflictActionType, conflictAction: ConflictActionType,
syncDirection: SyncDirectionType, syncDirection: SyncDirectionType,
profiler: Profiler, profiler: Profiler | undefined,
settings: RemotelySavePluginSettings, settings: RemotelySavePluginSettings,
triggerSource: SyncTriggerSourceType triggerSource: SyncTriggerSourceType
) => { ) => {
profiler.addIndent(); profiler?.addIndent();
profiler.insert("getSyncPlanInplace: enter"); profiler?.insert("getSyncPlanInplace: enter");
// from long(deep) to short(shadow) // from long(deep) to short(shadow)
const sortedKeys = Object.keys(mixedEntityMappings).sort( const sortedKeys = Object.keys(mixedEntityMappings).sort(
(k1, k2) => k2.length - k1.length (k1, k2) => k2.length - k1.length
); );
profiler.insert("getSyncPlanInplace: finish sorting"); profiler?.insert("getSyncPlanInplace: finish sorting");
profiler.insertSize("sizeof sortedKeys", sortedKeys); profiler?.insertSize("sizeof sortedKeys", sortedKeys);
const keptFolder = new Set<string>(); const keptFolder = new Set<string>();
for (let i = 0; i < sortedKeys.length; ++i) { for (let i = 0; i < sortedKeys.length; ++i) {
if (i % 100 === 0) { if (i % 100 === 0) {
profiler.insertSize( profiler?.insertSize(
`sizeof sortedKeys in the beginning of i=${i}`, `sizeof sortedKeys in the beginning of i=${i}`,
mixedEntityMappings mixedEntityMappings
); );
@ -706,7 +706,7 @@ const getSyncPlanInplace = async (
} }
} }
profiler.insert("getSyncPlanInplace: finish looping"); profiler?.insert("getSyncPlanInplace: finish looping");
keptFolder.delete("/"); keptFolder.delete("/");
keptFolder.delete(""); keptFolder.delete("");
@ -736,12 +736,12 @@ const getSyncPlanInplace = async (
}, },
}; };
profiler.insert("getSyncPlanInplace: exit"); profiler?.insert("getSyncPlanInplace: exit");
profiler.insertSize( profiler?.insertSize(
"sizeof mixedEntityMappings in the end of getSyncPlanInplace", "sizeof mixedEntityMappings in the end of getSyncPlanInplace",
mixedEntityMappings mixedEntityMappings
); );
profiler.removeIndent(); profiler?.removeIndent();
return mixedEntityMappings; return mixedEntityMappings;
}; };
@ -1130,11 +1130,11 @@ export const doActualSync = async (
protectModifyPercentage: number, protectModifyPercentage: number,
getProtectModifyPercentageErrorStrFunc: any, getProtectModifyPercentageErrorStrFunc: any,
db: InternalDBs, db: InternalDBs,
profiler: Profiler, profiler: Profiler | undefined,
callbackSyncProcess?: any callbackSyncProcess?: any
) => { ) => {
profiler.addIndent(); profiler?.addIndent();
profiler.insert("doActualSync: enter"); profiler?.insert("doActualSync: enter");
console.debug(`concurrency === ${concurrency}`); console.debug(`concurrency === ${concurrency}`);
const { const {
onlyMarkSyncedOps, onlyMarkSyncedOps,
@ -1152,17 +1152,17 @@ export const doActualSync = async (
console.debug(`allFilesCount: ${allFilesCount}`); console.debug(`allFilesCount: ${allFilesCount}`);
console.debug(`realModifyDeleteCount: ${realModifyDeleteCount}`); console.debug(`realModifyDeleteCount: ${realModifyDeleteCount}`);
console.debug(`realTotalCount: ${realTotalCount}`); console.debug(`realTotalCount: ${realTotalCount}`);
profiler.insert("doActualSync: finish splitting steps"); profiler?.insert("doActualSync: finish splitting steps");
profiler.insertSize( profiler?.insertSize(
"doActualSync: sizeof onlyMarkSyncedOps", "doActualSync: sizeof onlyMarkSyncedOps",
onlyMarkSyncedOps onlyMarkSyncedOps
); );
profiler.insertSize( profiler?.insertSize(
"doActualSync: sizeof folderCreationOps", "doActualSync: sizeof folderCreationOps",
folderCreationOps folderCreationOps
); );
profiler.insertSize("doActualSync: sizeof realTotalCount", deletionOps); profiler?.insertSize("doActualSync: sizeof realTotalCount", deletionOps);
console.debug(`protectModifyPercentage: ${protectModifyPercentage}`); console.debug(`protectModifyPercentage: ${protectModifyPercentage}`);
@ -1187,8 +1187,8 @@ export const doActualSync = async (
allFilesCount allFilesCount
); );
profiler.insert("doActualSync: error branch"); profiler?.insert("doActualSync: error branch");
profiler.removeIndent(); profiler?.removeIndent();
throw Error(errorStr); throw Error(errorStr);
} }
} }
@ -1208,8 +1208,8 @@ export const doActualSync = async (
let realCounter = 0; let realCounter = 0;
for (let i = 0; i < nested.length; ++i) { for (let i = 0; i < nested.length; ++i) {
profiler.addIndent(); profiler?.addIndent();
profiler.insert(`doActualSync: step ${i} start`); profiler?.insert(`doActualSync: step ${i} start`);
console.debug(logTexts[i]); console.debug(logTexts[i]);
const operations = nested[i]; const operations = nested[i];
@ -1282,12 +1282,12 @@ export const doActualSync = async (
} }
} }
profiler.insert(`doActualSync: step ${i} end`); profiler?.insert(`doActualSync: step ${i} end`);
profiler.removeIndent(); profiler?.removeIndent();
} }
profiler.insert(`doActualSync: exit`); profiler?.insert(`doActualSync: exit`);
profiler.removeIndent(); profiler?.removeIndent();
}; };
export type SyncStatusType = export type SyncStatusType =
@ -1309,7 +1309,7 @@ export async function syncer(
fsLocal: FakeFs, fsLocal: FakeFs,
fsRemote: FakeFs, fsRemote: FakeFs,
fsEncrypt: FakeFsEncrypt, fsEncrypt: FakeFsEncrypt,
profiler: Profiler, profiler: Profiler | undefined,
db: InternalDBs, db: InternalDBs,
triggerSource: SyncTriggerSourceType, triggerSource: SyncTriggerSourceType,
profileID: string, profileID: string,
@ -1340,7 +1340,7 @@ export async function syncer(
await notifyFunc?.(triggerSource, step); await notifyFunc?.(triggerSource, step);
await ribboonFunc?.(triggerSource, step); await ribboonFunc?.(triggerSource, step);
await statusBarFunc?.(triggerSource, step, everythingOk); await statusBarFunc?.(triggerSource, step, everythingOk);
profiler.insert("start big sync func"); profiler?.insert("start big sync func");
try { try {
step = 2; step = 2;
@ -1354,7 +1354,7 @@ export async function syncer(
if (!passwordCheckResult.ok) { if (!passwordCheckResult.ok) {
throw Error(passwordCheckResult.reason); throw Error(passwordCheckResult.reason);
} }
profiler.insert( profiler?.insert(
`finish step${step} (list partial remote and check password)` `finish step${step} (list partial remote and check password)`
); );
@ -1365,7 +1365,7 @@ export async function syncer(
const remoteEntityList = await fsEncrypt.walk(); const remoteEntityList = await fsEncrypt.walk();
// console.debug(`remoteEntityList:`); // console.debug(`remoteEntityList:`);
// console.debug(remoteEntityList); // console.debug(remoteEntityList);
profiler.insert(`finish step${step} (list remote)`); profiler?.insert(`finish step${step} (list remote)`);
step = 4; step = 4;
await notifyFunc?.(triggerSource, step); await notifyFunc?.(triggerSource, step);
@ -1374,7 +1374,7 @@ export async function syncer(
const localEntityList = await fsLocal.walk(); const localEntityList = await fsLocal.walk();
// console.debug(`localEntityList:`); // console.debug(`localEntityList:`);
// console.debug(localEntityList); // console.debug(localEntityList);
profiler.insert(`finish step${step} (list local)`); profiler?.insert(`finish step${step} (list local)`);
step = 5; step = 5;
await notifyFunc?.(triggerSource, step); await notifyFunc?.(triggerSource, step);
@ -1387,7 +1387,7 @@ export async function syncer(
); );
// console.debug(`prevSyncEntityList:`); // console.debug(`prevSyncEntityList:`);
// console.debug(prevSyncEntityList); // console.debug(prevSyncEntityList);
profiler.insert(`finish step${step} (prev sync)`); profiler?.insert(`finish step${step} (prev sync)`);
step = 6; step = 6;
await notifyFunc?.(triggerSource, step); await notifyFunc?.(triggerSource, step);
@ -1405,7 +1405,7 @@ export async function syncer(
settings.serviceType, settings.serviceType,
profiler profiler
); );
profiler.insert(`finish step${step} (build partial mixedEntity)`); profiler?.insert(`finish step${step} (build partial mixedEntity)`);
mixedEntityMappings = await getSyncPlanInplace( mixedEntityMappings = await getSyncPlanInplace(
mixedEntityMappings, mixedEntityMappings,
@ -1419,7 +1419,7 @@ export async function syncer(
); );
console.debug(`mixedEntityMappings:`); console.debug(`mixedEntityMappings:`);
console.debug(mixedEntityMappings); // for debugging console.debug(mixedEntityMappings); // for debugging
profiler.insert("finish building full sync plan"); profiler?.insert("finish building full sync plan");
await insertSyncPlanRecordByVault( await insertSyncPlanRecordByVault(
db, db,
@ -1427,8 +1427,8 @@ export async function syncer(
vaultRandomID, vaultRandomID,
settings.serviceType settings.serviceType
); );
profiler.insert("finish writing sync plan"); profiler?.insert("finish writing sync plan");
profiler.insert(`finish step${step} (make plan)`); profiler?.insert(`finish step${step} (make plan)`);
// The operations above are almost read only and kind of safe. // The operations above are almost read only and kind of safe.
// The operations below begins to write or delete (!!!) something. // The operations below begins to write or delete (!!!) something.
@ -1451,27 +1451,27 @@ export async function syncer(
profiler, profiler,
callbackSyncProcess callbackSyncProcess
); );
profiler.insert(`finish step${step} (actual sync)`); profiler?.insert(`finish step${step} (actual sync)`);
} else { } else {
await notifyFunc?.(triggerSource, step); await notifyFunc?.(triggerSource, step);
await ribboonFunc?.(triggerSource, step); await ribboonFunc?.(triggerSource, step);
await statusBarFunc?.(triggerSource, step, everythingOk); await statusBarFunc?.(triggerSource, step, everythingOk);
profiler.insert( profiler?.insert(
`finish step${step} (skip actual sync because of dry run)` `finish step${step} (skip actual sync because of dry run)`
); );
} }
} catch (error: any) { } catch (error: any) {
profiler.insert("start error branch"); profiler?.insert("start error branch");
everythingOk = false; everythingOk = false;
await errNotifyFunc?.(triggerSource, error as Error); await errNotifyFunc?.(triggerSource, error as Error);
profiler.insert("finish error branch"); profiler?.insert("finish error branch");
} finally { } finally {
} }
profiler.insert("finish syncRun"); profiler?.insert("finish syncRun");
// console.debug(profiler.toString()); // console.debug(profiler?.toString());
await profiler.save(db, vaultRandomID, settings.serviceType); await profiler?.save(db, vaultRandomID, settings.serviceType);
step = 8; step = 8;
await notifyFunc?.(triggerSource, step); await notifyFunc?.(triggerSource, step);