@@ -30,6 +30,8 @@ var helpers = require('./helpers');
3030var subroutines = require ( './subroutines' ) ;
3131var editTypes = require ( './edit_types' ) ;
3232
33+ var Profiler = require ( '../lib' ) . Profiler ;
34+
3335var AX_NAME_PATTERN = require ( '../plots/cartesian/constants' ) . AX_NAME_PATTERN ;
3436
3537var numericNameWarningCount = 0 ;
@@ -64,6 +66,9 @@ function _doPlot(gd, data, layout, config) {
6466 // Events.init is idempotent and bails early if gd has already been init'd
6567 Events . init ( gd ) ;
6668
69+ // Start profiler if enabled (returns no-op if disabled)
70+ var profiler = Profiler . start ( gd ) ;
71+
6772 if ( Lib . isPlainObject ( data ) ) {
6873 var obj = data ;
6974 data = obj . data ;
@@ -129,6 +134,7 @@ function _doPlot(gd, data, layout, config) {
129134 }
130135
131136 Plots . supplyDefaults ( gd ) ;
137+ profiler . mark ( 'supplyDefaults' ) ;
132138
133139 var fullLayout = gd . _fullLayout ;
134140 var hasCartesian = fullLayout . _has ( 'cartesian' ) ;
@@ -145,6 +151,7 @@ function _doPlot(gd, data, layout, config) {
145151 delete fullLayout . _shouldCreateBgLayer ;
146152 }
147153 }
154+ profiler . mark ( 'makePlotFramework' ) ;
148155
149156 // clear gradient and pattern defs on each .plot call, because we know we'll loop through all traces
150157 Drawing . initGradients ( gd ) ;
@@ -159,6 +166,7 @@ function _doPlot(gd, data, layout, config) {
159166 // to force redoing calcdata, just delete it before calling _doPlot
160167 var recalc = ! gd . calcdata || gd . calcdata . length !== ( gd . _fullData || [ ] ) . length ;
161168 if ( recalc ) Plots . doCalcdata ( gd ) ;
169+ profiler . mark ( 'doCalcdata' ) ;
162170
163171 // in case it has changed, attach fullData traces to calcdata
164172 for ( var i = 0 ; i < gd . calcdata . length ; i ++ ) {
@@ -351,11 +359,28 @@ function _doPlot(gd, data, layout, config) {
351359 return Axes . draw ( gd , graphWasEmpty ? '' : 'redraw' ) ;
352360 }
353361
354- var seq = [ Plots . previousPromises , addFrames , drawFramework , marginPushers , marginPushersAgain ] ;
362+ var seq = [
363+ Plots . previousPromises ,
364+ addFrames ,
365+ drawFramework ,
366+ function ( ) { profiler . mark ( 'drawFramework' ) ; } ,
367+ marginPushers ,
368+ function ( ) { profiler . mark ( 'marginPushers' ) ; } ,
369+ marginPushersAgain ,
370+ function ( ) { profiler . mark ( 'marginPushersAgain' ) ; }
371+ ] ;
355372
356- if ( hasCartesian ) seq . push ( positionAndAutorange ) ;
373+ if ( hasCartesian ) {
374+ seq . push (
375+ positionAndAutorange ,
376+ function ( ) { profiler . mark ( 'positionAndAutorange' ) ; }
377+ ) ;
378+ }
357379
358- seq . push ( subroutines . layoutStyles ) ;
380+ seq . push (
381+ subroutines . layoutStyles ,
382+ function ( ) { profiler . mark ( 'layoutStyles' ) ; }
383+ ) ;
359384 if ( hasCartesian ) {
360385 seq . push ( drawAxes , function insideTickLabelsAutorange ( gd ) {
361386 var insideTickLabelsUpdaterange = gd . _fullLayout . _insideTickLabelsUpdaterange ;
@@ -367,12 +392,16 @@ function _doPlot(gd, data, layout, config) {
367392 } ) ;
368393 }
369394 } ) ;
395+ seq . push ( function ( ) { profiler . mark ( 'drawAxes' ) ; } ) ;
370396 }
371397
372398 seq . push (
373399 subroutines . drawData ,
400+ function ( ) { profiler . mark ( 'drawData' ) ; } ,
374401 subroutines . finalDraw ,
402+ function ( ) { profiler . mark ( 'finalDraw' ) ; } ,
375403 initInteractions ,
404+ function ( ) { profiler . mark ( 'initInteractions' ) ; } ,
376405 Plots . addLinks ,
377406 Plots . rehover ,
378407 Plots . redrag ,
@@ -391,6 +420,13 @@ function _doPlot(gd, data, layout, config) {
391420 if ( ! plotDone || ! plotDone . then ) plotDone = Promise . resolve ( ) ;
392421
393422 return plotDone . then ( function ( ) {
423+ // Finalize profiling and emit event if profiling is enabled
424+ var profileData = profiler . end ( ) ;
425+ if ( profileData && profileData . total ) {
426+ gd . _profileData = profileData ;
427+ Events . triggerHandler ( gd , 'plotly_profiled' , profileData ) ;
428+ }
429+
394430 emitAfterPlot ( gd ) ;
395431 return gd ;
396432 } ) ;
0 commit comments