-
Notifications
You must be signed in to change notification settings - Fork 208
/
MeasureTileLoadTime.ts
77 lines (62 loc) · 2.76 KB
/
MeasureTileLoadTime.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*---------------------------------------------------------------------------------------------
* Copyright (c) Bentley Systems, Incorporated. All rights reserved.
* See LICENSE.md in the project root for license terms and full copyright notice.
*--------------------------------------------------------------------------------------------*/
/** @packageDocumentation
* @module Tools
*/
import { StopWatch } from "@itwin/core-bentley";
import { IModelApp, NotifyMessageDetails, OutputMessagePriority, Tool, Viewport } from "@itwin/core-frontend";
class TileLoadTimer {
private readonly _vp: Viewport;
private readonly _stopwatch: StopWatch;
private _cleanup?: () => void;
public constructor(vp: Viewport) {
this._vp = vp;
this._stopwatch = new StopWatch();
// Purge tile trees for all models.
IModelApp.viewManager.refreshForModifiedModels(undefined);
const removeOnRender = vp.onRender.addListener(() => this.onRender());
const removeOnClose = vp.iModel.onClose.addOnce(() => this.cancel());
this._cleanup = () => {
removeOnRender();
removeOnClose();
};
IModelApp.notifications.outputMessage(new NotifyMessageDetails(OutputMessagePriority.Info, "Tile loading timer started."));
this._stopwatch.start();
}
private cancel(): void {
IModelApp.notifications.outputMessage(new NotifyMessageDetails(OutputMessagePriority.Info, "Tile loading timer canceled."));
this.stop();
}
private stop(): void {
if (undefined !== this._cleanup) {
this._cleanup();
this._cleanup = undefined;
}
}
private onRender(): void {
// ###TODO: May be intermediate frames during which children props have been asynchronously requested but no outstanding tile requests.
if (!this._vp.areAllTileTreesLoaded || 0 < this._vp.numRequestedTiles)
return;
this._stopwatch.stop();
IModelApp.notifications.outputMessage(new NotifyMessageDetails(OutputMessagePriority.Info, `Tiles loaded in ${this._stopwatch.elapsedSeconds.toFixed(4)} seconds.`));
this.stop();
}
}
/** Unloads all tile trees, then starts a timer that stops when all tile trees and tiles required for the view are ready.
* Outputs the elapsed time to notifications manager.
* @beta
*/
export class MeasureTileLoadTimeTool extends Tool {
public static override toolId = "MeasureTileLoadTime";
/** This method runs the tool, unloading all tile trees, then starts a timer that stops when all tile trees and tiles required for the view are ready. It will then output the elapsed time to notifications manager.
* @param _args this parameter is unused
*/
public override async run(_args: any[]): Promise<boolean> {
const vp = IModelApp.viewManager.selectedView;
if (undefined !== vp)
new TileLoadTimer(vp);
return true;
}
}