@@ -23,55 +23,63 @@ export class FlamegraphChart {
23
23
y : [ 0 , 0 ] ,
24
24
} ;
25
25
26
- static Empty = new FlamegraphChart ( Rect . Empty ( ) , { unit : 'percent' , values : [ ] } , [
27
- [ 0 , 0 , 0 , 0 ] ,
28
- ] ) ;
26
+ static Empty = new FlamegraphChart ( Rect . Empty ( ) , [ ] , [ [ 0 , 0 , 0 , 0 ] ] ) ;
29
27
30
28
constructor (
31
29
configSpace : Rect ,
32
- measurement : Profiling . Measurement ,
30
+ measurements : Profiling . Measurement [ ] ,
33
31
colors : ColorChannels [ ]
34
32
) {
35
33
this . series = new Array < Series > ( ) ;
36
34
37
- if ( ! measurement || ! measurement . values . length ) {
35
+ if ( ! measurements || ! measurements . length ) {
38
36
this . formatter = makeFormatter ( 'percent' ) ;
39
37
this . configSpace = configSpace . clone ( ) ;
40
38
return ;
41
39
}
42
40
43
- this . series [ 0 ] = {
44
- type : 'area' ,
45
- lineColor : colorComponentsToRGBA ( colors [ 0 ] ) ,
46
- fillColor : colorComponentsToRGBA ( colors [ 0 ] ) ,
47
- points : new Array ( measurement . values . length ) ,
48
- } ;
41
+ const type = measurements . length > 0 ? 'line' : 'area' ;
49
42
50
- for ( let i = 0 ; i < measurement . values . length ; i ++ ) {
51
- const m = measurement . values [ i ] ;
43
+ for ( let j = 0 ; j < measurements . length ; j ++ ) {
44
+ const measurement = measurements [ j ] ;
45
+ this . series [ j ] = {
46
+ type,
47
+ lineColor : colorComponentsToRGBA ( colors [ j ] ) ,
48
+ fillColor : colorComponentsToRGBA ( colors [ j ] ) ,
49
+ points : new Array ( measurement . values . length ) . fill ( 0 ) ,
50
+ } ;
52
51
53
- // Track and update Y max and min
54
- if ( m . value > this . domains . y [ 1 ] ) {
55
- this . domains . y [ 1 ] = m . value ;
56
- }
57
- if ( m . value < this . domains . y [ 0 ] ) {
58
- this . domains . y [ 0 ] = m . value ;
59
- }
52
+ for ( let i = 0 ; i < measurement . values . length ; i ++ ) {
53
+ const m = measurement . values [ i ] ;
60
54
61
- // Track and update X domain max and min
62
- if ( m . elapsed_since_start_ns > this . domains . x [ 1 ] ) {
63
- this . domains . x [ 1 ] = m . elapsed_since_start_ns ;
64
- }
65
- if ( m . elapsed_since_start_ns < this . domains . x [ 0 ] ) {
66
- this . domains . x [ 1 ] = m . elapsed_since_start_ns ;
67
- }
55
+ // Track and update Y max and min
56
+ if ( m . value > this . domains . y [ 1 ] ) {
57
+ this . domains . y [ 1 ] = m . value ;
58
+ }
59
+ if ( m . value < this . domains . y [ 0 ] ) {
60
+ this . domains . y [ 0 ] = m . value ;
61
+ }
68
62
69
- this . series [ 0 ] . points [ i ] = { x : m . elapsed_since_start_ns , y : m . value } ;
63
+ // Track and update X domain max and min
64
+ if ( m . elapsed_since_start_ns > this . domains . x [ 1 ] ) {
65
+ this . domains . x [ 1 ] = m . elapsed_since_start_ns ;
66
+ }
67
+ if ( m . elapsed_since_start_ns < this . domains . x [ 0 ] ) {
68
+ this . domains . x [ 1 ] = m . elapsed_since_start_ns ;
69
+ }
70
+
71
+ this . series [ j ] . points [ i ] = { x : m . elapsed_since_start_ns , y : m . value } ;
72
+ }
70
73
}
71
74
75
+ this . series . sort ( ( a , b ) => {
76
+ const aAvg = a . points . reduce ( ( acc , point ) => acc + point . y , 0 ) / a . points . length ;
77
+ const bAvg = b . points . reduce ( ( acc , point ) => acc + point . y , 0 ) / b . points . length ;
78
+ return bAvg - aAvg ;
79
+ } ) ;
80
+
72
81
this . domains . y [ 1 ] = 100 ;
73
82
this . configSpace = configSpace . withHeight ( this . domains . y [ 1 ] - this . domains . y [ 0 ] ) ;
74
-
75
- this . formatter = makeFormatter ( measurement . unit , 0 ) ;
83
+ this . formatter = makeFormatter ( measurements [ 0 ] . unit , 0 ) ;
76
84
}
77
85
}
0 commit comments