rsPerformance.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. IBM Confidential
  3. OCO Source Materials
  4. IBM Cognos Products: authoring
  5. (C) Copyright IBM Corp. 2015, 2020
  6. The source code for this program is not published or otherwise divested
  7. of its trade secrets, irrespective of what has been deposited with the
  8. U.S. Copyright Office.
  9. */
  10. define(['q'], function(Q){
  11. 'use strict';
  12. var v_oRsPerformance = {
  13. M_sPerformanceLogServerHost: undefined,
  14. M_sKitVersion: 'no kit version',
  15. M_bKitVersionIsCached: false,
  16. summarizeInteractiveViewerRun: function( v_oView) {
  17. this._commonSummarize( v_oView,
  18. [
  19. { start:'rsContextMenuActions.onSelectItem', end: 'rsContentView.render'},
  20. { start:'rsContentView.render', end: 'rsContentView._onRSApplicationReady'},
  21. { start:'rsContentView._onRSApplicationReady', end: 'rsContentView.onViewBeforeDraw'},
  22. { start:'rsContentView.onViewBeforeDraw', end: 'rsContentView.onViewAfterDraw'},
  23. { start:'rsContextMenuActions.onSelectItem', end: 'rsContentView.onViewAfterDraw'},
  24. { start:'authoring-selectItemAndDraw-start', end: 'authoring-selectItemAndDraw-stop'}
  25. ]);
  26. },
  27. summarizeClassicViewerRun: function ( v_oView ) {
  28. this._commonSummarize(v_oView,
  29. [
  30. { start:'rsContextMenuActions.onSelectItem', end: 'cvContentView.render'},
  31. { start:'cvContentView.render', end: 'cvContentView.getViewerConfiguration'},
  32. { start:'cvContentView.getViewerConfiguration', end: 'cvContentView.onReportStatusComplete'},
  33. { start:'cvContentView.onReportStatusComplete', end: 'cvContentView._onClassicViewerLoaded'},
  34. { start:'rsContextMenuActions.onSelectItem', end: 'cvContentView._onClassicViewerLoaded'},
  35. { start:'authoring-selectItemAndDraw-start', end: 'authoring-selectItemAndDraw-stop'}
  36. ]);
  37. },
  38. _commonSummarize: function ( v_oView, v_aStartEndNames ) {
  39. // We do the non-essential work inside a try-catch because the window.performance methods
  40. // can throw exceptions when markers don't exist or have been renamed.
  41. try
  42. {
  43. if ( this.getEntriesByName('rsContextMenuActions.onSelectItem','mark').length < 1 )
  44. {
  45. return;
  46. }
  47. v_aStartEndNames.forEach( function ( v_o ) {
  48. var v_sName = v_o.start + '->' + v_o.end;
  49. try
  50. {
  51. this.measure( v_sName, v_o.start, v_o.end );
  52. }
  53. catch(e)
  54. {
  55. // sometimes we don't have the start or end and will get an exception
  56. // skip it
  57. }
  58. }, this);
  59. var v_aPerformanceFields= [
  60. "navigationStart",
  61. "unloadEventStart",
  62. "unloadEventEnd",
  63. "redirectStart",
  64. "redirectEnd",
  65. "fetchStart",
  66. "domainLookupStart",
  67. "domainLookupEnd",
  68. "connectStart",
  69. "connectEnd",
  70. "requestStart",
  71. "responseStart",
  72. "responseEnd",
  73. "domLoading",
  74. "domInteractive",
  75. "domContentLoadedEventStart",
  76. "domContentLoadedEventEnd",
  77. "domComplete",
  78. "loadEventStart",
  79. "loadEventEnd"];
  80. var v_oPerformanceOut = {timing:{}};
  81. var iframe = v_oView.m_iframeAuthoring || v_oView.m_iframeClassicViewer;
  82. var v_oPerformanceIn = iframe.contentWindow.performance;
  83. for(var iPField=0; iPField < v_aPerformanceFields.length; ++iPField)
  84. {
  85. var v_sField = v_aPerformanceFields[iPField];
  86. if (v_oPerformanceIn.timing[v_sField] !== undefined)
  87. {
  88. v_oPerformanceOut.timing[v_sField] = v_oPerformanceIn.timing[v_sField];
  89. }
  90. }
  91. var v_oPerformanceSummary = {
  92. userAgent: window.navigator.userAgent,
  93. urlPath: window.location.protocol + '//' + window.location.hostname + (window.location.port ? ":" + window.location.port : "") + window.location.pathname,
  94. reportName: v_oView.cmProperties.defaultName,
  95. date: Date.now(),
  96. measures:[],
  97. rsWindowPerformance: v_oPerformanceOut
  98. };
  99. this.getEntriesByType('measure').forEach( function( v_oEntry ) {
  100. console.info( "rsperf: performance.measure %s %s ms", v_oEntry.name, v_oEntry.duration);
  101. v_oPerformanceSummary.measures.push({name: v_oEntry.name, duration: v_oEntry.duration} );
  102. });
  103. this.clearMeasures();
  104. this.clearMarks();
  105. if (this.M_sPerformanceLogServerHost)
  106. {
  107. // Calling Q(blah) is tagged as an error because Q() looks like
  108. // a ctor function which should be new Q(blah)
  109. var qPromiseFromValueForLint = Q;
  110. Q.all([
  111. this.getKitVersion(v_oView.glassContext),
  112. this.getPerformanceUUID(v_oView.glassContext),
  113. qPromiseFromValueForLint(v_oPerformanceSummary),
  114. qPromiseFromValueForLint(v_oView.glassContext)])
  115. .then( function (v_aResults) {
  116. this.sendPerformanceRecordToHost.apply(this, v_aResults);
  117. }.bind(this))
  118. .catch( function () {
  119. console.info('rsperf: failed to get kit and uuid for logging');
  120. })
  121. .done( function (v_oResult) {
  122. console.info('rsperf: _commonSummarized finished');
  123. });
  124. }
  125. else
  126. {
  127. this.getKitVersion(v_oView.glassContext)
  128. .catch( function(err){
  129. console.info('rsperf: error while getting kit version');
  130. })
  131. .done( function(version){
  132. console.info('rsperf: kit version %s (cached: %s)', version, this.M_bKitVersionIsCached.toString());
  133. }.bind(this));
  134. }
  135. }
  136. catch(err)
  137. {
  138. console.warn('_commonSummarize failed with an exception:\n%s', err && err.toString() || "unknown err");
  139. this.clearMeasures();
  140. this.clearMarks();
  141. }
  142. },
  143. getKitVersion: function( v_oGlassContext ) {
  144. var deferred = Q.defer();
  145. if ( this.M_sKitVersion )
  146. {
  147. this.M_bKitVersionIsCached = true;
  148. deferred.resolve(this.M_sKitVersion);
  149. return deferred.promise;
  150. }
  151. v_oGlassContext.services.ajax.ajax({
  152. url: window.location.pathname + 'cmplst.txt',
  153. type: 'GET',
  154. dataType: 'text'
  155. })
  156. .fail( function(err){
  157. console.warn('getKitVersion() failed ');
  158. this.M_sKitVersion = 'error cannot get kit version';
  159. deferred.reject(this.M_sKitVersion);
  160. }.bind(this))
  161. .done( function(resp){
  162. var v_aMatches = resp.match(/^kit_version=\s*((?:\d+.)+\d+)\s*$/m);
  163. if (v_aMatches)
  164. {
  165. this.M_sKitVersion = v_aMatches[1];
  166. }
  167. else
  168. {
  169. this.M_sKitVersion = 'could not find kit version';
  170. }
  171. deferred.resolve(this.M_sKitVersion);
  172. }.bind(this));
  173. return deferred.promise;
  174. },
  175. getPerformanceUUID: function(v_oGlassContext) {
  176. var deferred = Q.defer();
  177. v_oGlassContext.services.ajax.ajax({
  178. url: this.M_sPerformanceLogServerHost + "/_uuids",
  179. type: 'GET',
  180. dataType: 'json'
  181. })
  182. .fail(function(jqXHR, err){
  183. deferred.reject('uuid_failed');
  184. })
  185. .done(function(resp){
  186. deferred.resolve(resp.uuids[0]);
  187. });
  188. return deferred.promise;
  189. },
  190. sendPerformanceRecordToHost: function(v_sKit, v_sUuid, v_oRecord, v_oGlassContext) {
  191. var deferred = Q.defer();
  192. v_oRecord.kitVersion = v_sKit;
  193. v_oRecord.kitVersionCached = this.M_bKitVersionIsCached;
  194. v_oGlassContext.services.ajax.ajax({
  195. headers: {
  196. 'Accept': 'application/json',
  197. 'Content-Type': 'application/json'
  198. },
  199. url: this.M_sPerformanceLogServerHost + "/toddtest/" + v_sUuid,
  200. type: 'PUT',
  201. data: JSON.stringify(v_oRecord)
  202. })
  203. .fail( function (err) {
  204. console.log('error sendPerformanceRecordToHost: %s', err);
  205. deferred.reject(err);
  206. })
  207. .done( function (result) {
  208. console.log('succeed sendPerformanceRecordToHost: %s', result);
  209. deferred.resolve(result);
  210. });
  211. return deferred.promise;
  212. },
  213. };
  214. ['mark',
  215. 'measure',
  216. 'getEntriesByName',
  217. 'getEntriesByType',
  218. 'clearMeasures',
  219. 'clearMarks'
  220. ].forEach(function(fname){
  221. if (window.performance[fname])
  222. {
  223. this[fname] = window.performance[fname].bind(window.performance);
  224. }
  225. else if (['getEntriesByName', 'getEntriesByType'].indexOf(fname) != -1)
  226. {
  227. this[fname] = function(){ return []; };
  228. }
  229. else
  230. {
  231. this[fname] = function(){};
  232. }
  233. },v_oRsPerformance);
  234. return v_oRsPerformance;
  235. });