1- import { Deferred , DisposableStore , IDisposable , randomString } from '@opensumi/ide-core-common' ;
1+ import {
2+ Deferred ,
3+ DisposableStore ,
4+ IDisposable ,
5+ IReporterService ,
6+ IReporterTimer ,
7+ REPORT_NAME ,
8+ randomString ,
9+ } from '@opensumi/ide-core-common' ;
210import { addElement } from '@opensumi/ide-utils/lib/arrays' ;
311
412import { METHOD_NOT_REGISTERED } from '../constants' ;
@@ -12,6 +20,8 @@ import { ProtocolRegistry, ServiceRegistry } from './registry';
1220
1321import type { MessageConnection } from '@opensumi/vscode-jsonrpc' ;
1422
23+ const kDefaultMinimumReportThresholdTime = 200 ;
24+
1525export class RPCServiceCenter implements IDisposable {
1626 private _disposables = new DisposableStore ( ) ;
1727
@@ -30,6 +40,16 @@ export class RPCServiceCenter implements IDisposable {
3040 this . logger = logger || console ;
3141 }
3242
43+ private _reporterService : IReporterService | undefined ;
44+ private _reportThreshold : number = kDefaultMinimumReportThresholdTime ;
45+ setReporter (
46+ reporterService : IReporterService ,
47+ minimumReportThresholdTime : number = kDefaultMinimumReportThresholdTime ,
48+ ) {
49+ this . _reporterService = reporterService ;
50+ this . _reportThreshold = minimumReportThresholdTime ;
51+ }
52+
3353 registerService ( serviceName : string , type : ServiceType ) : void {
3454 if ( type === ServiceType . Service ) {
3555 if ( this . bench ) {
@@ -98,8 +118,13 @@ export class RPCServiceCenter implements IDisposable {
98118
99119 async broadcast ( serviceName : string , _name : string , ...args : any [ ] ) : Promise < any > {
100120 await this . ready ( ) ;
101-
102121 const name = getMethodName ( serviceName , _name ) ;
122+
123+ let timer : IReporterTimer | undefined ;
124+ if ( this . _reporterService ) {
125+ timer = this . _reporterService . time ( REPORT_NAME . RPC_TIMMING_MEASURE ) ;
126+ }
127+
103128 const broadcastResult = await Promise . all ( this . proxies . map ( ( proxy ) => proxy . invoke ( name , ...args ) ) ) ;
104129
105130 const doubtfulResult = [ ] as any [ ] ;
@@ -117,9 +142,33 @@ export class RPCServiceCenter implements IDisposable {
117142 }
118143
119144 if ( result . length === 0 ) {
145+ if ( timer ) {
146+ timer . timeEnd (
147+ name ,
148+ {
149+ success : false ,
150+ } ,
151+ {
152+ minimumReportThresholdTime : this . _reportThreshold ,
153+ } ,
154+ ) ;
155+ }
156+
120157 throw new Error ( `broadcast rpc \`${ name } \` error: no remote service can handle this call` ) ;
121158 }
122159
160+ if ( timer ) {
161+ timer . timeEnd (
162+ name ,
163+ {
164+ success : true ,
165+ } ,
166+ {
167+ minimumReportThresholdTime : this . _reportThreshold ,
168+ } ,
169+ ) ;
170+ }
171+
123172 // FIXME: this is an unreasonable design, if remote service only returned doubtful result, we will return an empty array.
124173 // but actually we should throw an error to tell user that no remote service can handle this call.
125174 // or just return `undefined`.
0 commit comments