@shopgate/pwa-benchmark 7.30.0-alpha.7 → 7.30.0-alpha.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.js CHANGED
@@ -1,49 +1,221 @@
1
- function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}function _defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}function _createClass(Constructor,protoProps,staticProps){if(protoProps)_defineProperties(Constructor.prototype,protoProps);if(staticProps)_defineProperties(Constructor,staticProps);return Constructor;}function _defineProperty(obj,key,value){if(key in obj){Object.defineProperty(obj,key,{value:value,enumerable:true,configurable:true,writable:true});}else{obj[key]=value;}return obj;}import KeyFigure,{KEY_FIGURE_MODE_ADD,KEY_FIGURE_METHOD_TIME,KEY_FIGURE_METHOD_COUNT,KEY_FIGURE_METHOD_OBSERVER}from"./keyFigure";var HEADLINE_STYLE='font-size: 24px; margin-top: 32px; margin-bottom: 12px;';/**
1
+ import "core-js/modules/es.array.reduce.js";
2
+ import KeyFigure, { KEY_FIGURE_MODE_ADD, KEY_FIGURE_METHOD_TIME, KEY_FIGURE_METHOD_COUNT, KEY_FIGURE_METHOD_OBSERVER } from "./keyFigure";
3
+ const HEADLINE_STYLE = 'font-size: 24px; margin-top: 32px; margin-bottom: 12px;';
4
+
5
+ /**
2
6
  * Benchmark controller.
3
- */var BenchmarkController=/*#__PURE__*/function(){/**
7
+ */
8
+ class BenchmarkController {
9
+ /**
4
10
  * Init
5
- */function BenchmarkController(){_classCallCheck(this,BenchmarkController);/**
6
- * Sorts measured actions descending.
7
- * @param {Array} actions Measured actions.
8
- * @param {Function} getA Getter to get left side.
9
- * @param {Function} getB Getter to get right side.
10
- * @returns {Array}
11
- */_defineProperty(this,"sortMeasure",function(actions,getA,getB){return actions.sort(function(a,b){var measureA=getA(a);var measureB=getB(b);if(measureA<measureB){return-1;}if(measureA>measureB){return 1;}return 0;});});/**
12
- * Takes measure from all actions / components and adds it to a total.
13
- * @param {Array} measure List of measures.
14
- * @returns {Array}
15
- */_defineProperty(this,"getTotals",function(measure){var result=[];Object.keys(measure).forEach(function(action){return Object.keys(measure[action]).forEach(function(component){var found=result.find(function(r){return r.name===component;});if(found){found.render+=measure[action][component].render;found.renderTime+=measure[action][component].renderTime;return;}result.push({name:component,render:measure[action][component].render,renderTime:measure[action][component].renderTime});});});return result;});this.keyFigures={};}/**
11
+ */
12
+ constructor() {
13
+ /**
14
+ * Sorts measured actions descending.
15
+ * @param {Array} actions Measured actions.
16
+ * @param {Function} getA Getter to get left side.
17
+ * @param {Function} getB Getter to get right side.
18
+ * @returns {Array}
19
+ */
20
+ this.sortMeasure = (actions, getA, getB) => actions.sort((a, b) => {
21
+ const measureA = getA(a);
22
+ const measureB = getB(b);
23
+ if (measureA < measureB) {
24
+ return -1;
25
+ }
26
+ if (measureA > measureB) {
27
+ return 1;
28
+ }
29
+ return 0;
30
+ });
31
+ /**
32
+ * Takes measure from all actions / components and adds it to a total.
33
+ * @param {Array} measure List of measures.
34
+ * @returns {Array}
35
+ */
36
+ this.getTotals = measure => {
37
+ const result = [];
38
+ Object.keys(measure).forEach(action => Object.keys(measure[action]).forEach(component => {
39
+ const found = result.find(r => r.name === component);
40
+ if (found) {
41
+ found.render += measure[action][component].render;
42
+ found.renderTime += measure[action][component].renderTime;
43
+ return;
44
+ }
45
+ result.push({
46
+ name: component,
47
+ render: measure[action][component].render,
48
+ renderTime: measure[action][component].renderTime
49
+ });
50
+ }));
51
+ return result;
52
+ };
53
+ this.keyFigures = {};
54
+ }
55
+
56
+ /**
16
57
  * Startsup the controller
17
- */return _createClass(BenchmarkController,[{key:"startup",value:function startup(){// Redux key figures.
18
- this.addKeyFigure('ActionCount',new KeyFigure(KEY_FIGURE_MODE_ADD,KEY_FIGURE_METHOD_COUNT));this.addKeyFigure('ActionTime',new KeyFigure(KEY_FIGURE_MODE_ADD,KEY_FIGURE_METHOD_TIME));this.addKeyFigure('ActionEvents',new KeyFigure(KEY_FIGURE_MODE_ADD,KEY_FIGURE_METHOD_OBSERVER));this.addKeyFigure('GlobalEvents',new KeyFigure(KEY_FIGURE_MODE_ADD,KEY_FIGURE_METHOD_OBSERVER));// Add global events.
19
- this.getKeyFigure('GlobalEvents').startMeasure('global');}/**
58
+ */
59
+ startup() {
60
+ // Redux key figures.
61
+ this.addKeyFigure('ActionCount', new KeyFigure(KEY_FIGURE_MODE_ADD, KEY_FIGURE_METHOD_COUNT));
62
+ this.addKeyFigure('ActionTime', new KeyFigure(KEY_FIGURE_MODE_ADD, KEY_FIGURE_METHOD_TIME));
63
+ this.addKeyFigure('ActionEvents', new KeyFigure(KEY_FIGURE_MODE_ADD, KEY_FIGURE_METHOD_OBSERVER));
64
+ this.addKeyFigure('GlobalEvents', new KeyFigure(KEY_FIGURE_MODE_ADD, KEY_FIGURE_METHOD_OBSERVER));
65
+
66
+ // Add global events.
67
+ this.getKeyFigure('GlobalEvents').startMeasure('global');
68
+ }
69
+
70
+ /**
20
71
  * Adds a key figure.
21
72
  * @param {string} name Name
22
73
  * @param {KeyFigure} keyFigure KeyFigure
23
- */},{key:"addKeyFigure",value:function addKeyFigure(name,keyFigure){this.keyFigures[name]=keyFigure;}/**
74
+ */
75
+ addKeyFigure(name, keyFigure) {
76
+ this.keyFigures[name] = keyFigure;
77
+ }
78
+
79
+ /**
24
80
  * Gets a key figure.
25
81
  * @param {string} name Name
26
82
  * @param {KeyFigure} keyfigure KeyFigure
27
83
  * @returns {Object}
28
- */},{key:"getKeyFigure",value:function getKeyFigure(name){return this.keyFigures[name];}/**
84
+ */
85
+ getKeyFigure(name) {
86
+ return this.keyFigures[name];
87
+ }
88
+
89
+ /**
29
90
  * Benchmark debug log;
30
- */},{key:"debug",value:function debug(){console.log('=========================');console.log('Benchmark Debug Log');console.log('=========================');console.log(this.keyFigures);}},{key:"printMostCalledActions",value:/**
91
+ */
92
+ debug() {
93
+ console.log('=========================');
94
+ console.log('Benchmark Debug Log');
95
+ console.log('=========================');
96
+ console.log(this.keyFigures);
97
+ }
98
+ /**
31
99
  * Prints most called actions.
32
- */function printMostCalledActions(){// Most called actions.
33
- var _this$getKeyFigure=this.getKeyFigure('ActionCount'),measure=_this$getKeyFigure.measure;var actions=Object.keys(measure.inclusive);var sortedAction=this.sortMeasure(actions,function(a){return measure.inclusive[a];},function(b){return measure.inclusive[b];});console.log('%cMost called actions',HEADLINE_STYLE);console.table(sortedAction.map(function(action){return{Action:action,Count:measure.inclusive[action]};}).reverse().slice(0,10));}/**
100
+ */
101
+ printMostCalledActions() {
102
+ // Most called actions.
103
+ const {
104
+ measure
105
+ } = this.getKeyFigure('ActionCount');
106
+ const actions = Object.keys(measure.inclusive);
107
+ const sortedAction = this.sortMeasure(actions, a => measure.inclusive[a], b => measure.inclusive[b]);
108
+ console.log('%cMost called actions', HEADLINE_STYLE);
109
+ console.table(sortedAction.map(action => ({
110
+ Action: action,
111
+ Count: measure.inclusive[action]
112
+ })).reverse().slice(0, 10));
113
+ }
114
+
115
+ /**
34
116
  * Prints the highest execution time per action on average.
35
117
  * @param {string} type "inclusive" or "exclusive"
36
- */},{key:"printHighestAverageAction",value:function printHighestAverageAction(){var type=arguments.length>0&&arguments[0]!==undefined?arguments[0]:'inclusive';// Highest average execution time
37
- var _this$getKeyFigure2=this.getKeyFigure('ActionTime'),measure=_this$getKeyFigure2.measure;var _this$getKeyFigure3=this.getKeyFigure('ActionCount'),measureCount=_this$getKeyFigure3.measure;var actions=Object.keys(measure[type]);var sortedAction=this.sortMeasure(actions,function(a){return measure[type][a]/measureCount[type][a];},function(b){return measure[type][b]/measureCount[type][b];});console.log("%cHighest average execution time (".concat(type,")"),HEADLINE_STYLE);console.table(sortedAction.map(function(action){return{Action:action,Time:measure[type][action]/measureCount[type][action]};}).reverse().slice(0,10));}/**
118
+ */
119
+ printHighestAverageAction(type = 'inclusive') {
120
+ // Highest average execution time
121
+ const {
122
+ measure
123
+ } = this.getKeyFigure('ActionTime');
124
+ const {
125
+ measure: measureCount
126
+ } = this.getKeyFigure('ActionCount');
127
+ const actions = Object.keys(measure[type]);
128
+ const sortedAction = this.sortMeasure(actions, a => measure[type][a] / measureCount[type][a], b => measure[type][b] / measureCount[type][b]);
129
+ console.log(`%cHighest average execution time (${type})`, HEADLINE_STYLE);
130
+ console.table(sortedAction.map(action => ({
131
+ Action: action,
132
+ Time: measure[type][action] / measureCount[type][action]
133
+ })).reverse().slice(0, 10));
134
+ }
135
+
136
+ /**
38
137
  * Prints most rendering actions.
39
- */},{key:"printsActionsWithMostRenders",value:function printsActionsWithMostRenders(){var measure=this.getKeyFigure('ActionEvents').measure.inclusive;var actions=Object.keys(measure).map(function(action){return Object.keys(measure[action]).reduce(function(acc,current){return{action:action,render:measure[action][current].render+acc.render,renderTime:measure[action][current].renderTime+acc.renderTime};},{action:action,render:0,renderTime:0});});var sortedAction=this.sortMeasure(actions,function(a){return a.render;},function(b){return b.render;});console.log('%cAction that caused most renders',HEADLINE_STYLE);console.table(sortedAction.map(function(action){return{Action:action.action,Renders:action.render,SumRenderTime:action.renderTime,AvgRenderTime:action.render>0?action.renderTime/action.render:0};}).reverse().slice(0,10),['Action','Renders','SumRenderTime','AvgRenderTime']);}},{key:"printsMostRenderedComponents",value:/**
138
+ */
139
+ printsActionsWithMostRenders() {
140
+ const measure = this.getKeyFigure('ActionEvents').measure.inclusive;
141
+ const actions = Object.keys(measure).map(action => Object.keys(measure[action]).reduce((acc, current) => ({
142
+ action,
143
+ render: measure[action][current].render + acc.render,
144
+ renderTime: measure[action][current].renderTime + acc.renderTime
145
+ }), {
146
+ action,
147
+ render: 0,
148
+ renderTime: 0
149
+ }));
150
+ const sortedAction = this.sortMeasure(actions, a => a.render, b => b.render);
151
+ console.log('%cAction that caused most renders', HEADLINE_STYLE);
152
+ console.table(sortedAction.map(action => ({
153
+ Action: action.action,
154
+ Renders: action.render,
155
+ SumRenderTime: action.renderTime,
156
+ AvgRenderTime: action.render > 0 ? action.renderTime / action.render : 0
157
+ })).reverse().slice(0, 10), ['Action', 'Renders', 'SumRenderTime', 'AvgRenderTime']);
158
+ }
159
+ /**
40
160
  * Prints the most rendering components.
41
- */function printsMostRenderedComponents(){var measure=this.getKeyFigure('GlobalEvents').measure.inclusive;var result=this.getTotals(measure);var sortedAction=this.sortMeasure(result,function(a){return a.render;},function(b){return b.render;});console.log('%cMost rendered Components',HEADLINE_STYLE);console.table(sortedAction.map(function(action){return{Component:action.name,Renders:action.render,SumRenderTime:action.renderTime,AvgRenderTime:action.render>0?action.renderTime/action.render:0};}).reverse().slice(0,10),['Component','Renders','SumRenderTime','AvgRenderTime']);}/**
161
+ */
162
+ printsMostRenderedComponents() {
163
+ const measure = this.getKeyFigure('GlobalEvents').measure.inclusive;
164
+ const result = this.getTotals(measure);
165
+ const sortedAction = this.sortMeasure(result, a => a.render, b => b.render);
166
+ console.log('%cMost rendered Components', HEADLINE_STYLE);
167
+ console.table(sortedAction.map(action => ({
168
+ Component: action.name,
169
+ Renders: action.render,
170
+ SumRenderTime: action.renderTime,
171
+ AvgRenderTime: action.render > 0 ? action.renderTime / action.render : 0
172
+ })).reverse().slice(0, 10), ['Component', 'Renders', 'SumRenderTime', 'AvgRenderTime']);
173
+ }
174
+
175
+ /**
42
176
  * Prints summary.
43
- */},{key:"printTotals",value:function printTotals(){var measure=this.getKeyFigure('GlobalEvents').measure.inclusive;var result=this.getTotals(measure);var sumRenders=result.reduce(function(current,acc){return current+acc.render;},0);var sumRenderTime=result.reduce(function(current,acc){return current+acc.renderTime;},0);var avgRenderTime=sumRenderTime/sumRenders;console.log('%cRender total',HEADLINE_STYLE);console.log("%cRenders: ".concat(sumRenders,"; Execution Time: ").concat(sumRenderTime,"ms; Average Execution Time: ").concat(avgRenderTime,"ms"),'font-size: 16px; margin-bottom: 12px;');}/**
177
+ */
178
+ printTotals() {
179
+ const measure = this.getKeyFigure('GlobalEvents').measure.inclusive;
180
+ const result = this.getTotals(measure);
181
+ const sumRenders = result.reduce((current, acc) => current + acc.render, 0);
182
+ const sumRenderTime = result.reduce((current, acc) => current + acc.renderTime, 0);
183
+ const avgRenderTime = sumRenderTime / sumRenders;
184
+ console.log('%cRender total', HEADLINE_STYLE);
185
+ console.log(`%cRenders: ${sumRenders}; Execution Time: ${sumRenderTime}ms; Average Execution Time: ${avgRenderTime}ms`, 'font-size: 16px; margin-bottom: 12px;');
186
+ }
187
+
188
+ /**
44
189
  * Pretty prints result.
45
- */},{key:"print",value:function print(){var globalEvents=this.getKeyFigure('GlobalEvents');if(globalEvents.started){this.getKeyFigure('GlobalEvents').stopMeasure('global');this.getKeyFigure('GlobalEvents').startMeasure('global');}this.printMostCalledActions();this.printHighestAverageAction();this.printsActionsWithMostRenders();this.printsMostRenderedComponents();this.printTotals();}/**
190
+ */
191
+ print() {
192
+ const globalEvents = this.getKeyFigure('GlobalEvents');
193
+ if (globalEvents.started) {
194
+ this.getKeyFigure('GlobalEvents').stopMeasure('global');
195
+ this.getKeyFigure('GlobalEvents').startMeasure('global');
196
+ }
197
+ this.printMostCalledActions();
198
+ this.printHighestAverageAction();
199
+ this.printsActionsWithMostRenders();
200
+ this.printsMostRenderedComponents();
201
+ this.printTotals();
202
+ }
203
+
204
+ /**
46
205
  * Starts benchmarking all key figures.
47
- */},{key:"startAll",value:function startAll(){var _this=this;Object.keys(this.keyFigures).forEach(function(keyFigure){return _this.keyFigures[keyFigure].setStarted(true);});this.getKeyFigure('GlobalEvents').startMeasure('global');}/**
206
+ */
207
+ startAll() {
208
+ Object.keys(this.keyFigures).forEach(keyFigure => this.keyFigures[keyFigure].setStarted(true));
209
+ this.getKeyFigure('GlobalEvents').startMeasure('global');
210
+ }
211
+
212
+ /**
48
213
  * Stops benchmarking all key figures.
49
- */},{key:"stopAll",value:function stopAll(){var _this2=this;this.getKeyFigure('GlobalEvents').stopMeasure('global');Object.keys(this.keyFigures).forEach(function(keyFigure){return _this2.keyFigures[keyFigure].setStarted(false);});}}]);}();window.benchmark=new BenchmarkController();export default window.benchmark;
214
+ */
215
+ stopAll() {
216
+ this.getKeyFigure('GlobalEvents').stopMeasure('global');
217
+ Object.keys(this.keyFigures).forEach(keyFigure => this.keyFigures[keyFigure].setStarted(false));
218
+ }
219
+ }
220
+ window.benchmark = new BenchmarkController();
221
+ export default window.benchmark;
package/keyFigure.js CHANGED
@@ -1,28 +1,100 @@
1
- function _toArray(arr){return _arrayWithHoles(arr)||_iterableToArray(arr)||_nonIterableRest();}function _nonIterableRest(){throw new TypeError("Invalid attempt to destructure non-iterable instance");}function _iterableToArray(iter){if(Symbol.iterator in Object(iter)||Object.prototype.toString.call(iter)==="[object Arguments]")return Array.from(iter);}function _arrayWithHoles(arr){if(Array.isArray(arr))return arr;}function _classCallCheck(instance,Constructor){if(!(instance instanceof Constructor)){throw new TypeError("Cannot call a class as a function");}}function _defineProperties(target,props){for(var i=0;i<props.length;i++){var descriptor=props[i];descriptor.enumerable=descriptor.enumerable||false;descriptor.configurable=true;if("value"in descriptor)descriptor.writable=true;Object.defineProperty(target,descriptor.key,descriptor);}}function _createClass(Constructor,protoProps,staticProps){if(protoProps)_defineProperties(Constructor.prototype,protoProps);if(staticProps)_defineProperties(Constructor,staticProps);return Constructor;}export var KEY_FIGURE_MODE_ADD='add';// Score is genereated by counting the occurence of each key.
2
- export var KEY_FIGURE_METHOD_COUNT='count';// Score is generated by measuring time between each key was started and stopped.
3
- export var KEY_FIGURE_METHOD_TIME='time';// Score is generated by collecting "performance events" during a start and stop. (chrome only)
4
- export var KEY_FIGURE_METHOD_OBSERVER='observer';/**
1
+ import "core-js/modules/es.string.replace.js";
2
+ export const KEY_FIGURE_MODE_ADD = 'add';
3
+
4
+ // Score is genereated by counting the occurence of each key.
5
+ export const KEY_FIGURE_METHOD_COUNT = 'count';
6
+ // Score is generated by measuring time between each key was started and stopped.
7
+ export const KEY_FIGURE_METHOD_TIME = 'time';
8
+ // Score is generated by collecting "performance events" during a start and stop. (chrome only)
9
+ export const KEY_FIGURE_METHOD_OBSERVER = 'observer';
10
+
11
+ /**
5
12
  * Class
6
- */var KeyFigure=/*#__PURE__*/function(){/**
13
+ */
14
+ export default class KeyFigure {
15
+ /**
7
16
  * Initializes the keyfigure and set it as started.
8
17
  * @param {string} mode Append mode.
9
18
  * @param {string} method Method.
10
- */function KeyFigure(mode,method){_classCallCheck(this,KeyFigure);this.mode=mode;this.method=method;this.setStarted(true);}/**
19
+ */
20
+ constructor(mode, method) {
21
+ this.mode = mode;
22
+ this.method = method;
23
+ this.setStarted(true);
24
+ }
25
+
26
+ /**
11
27
  * Toggles the status of the benchmark and also clears the result.
12
- * @param {bool} started Whether benchmark is running or not.
13
- */return _createClass(KeyFigure,[{key:"setStarted",value:function setStarted(started){if(started){this.measure={exclusiveTimers:[],timings:{},observers:{},inclusive:{},exclusive:{}};}this.started=true;}/**
28
+ * @param {boolean} started Whether benchmark is running or not.
29
+ */
30
+ setStarted(started) {
31
+ if (started) {
32
+ this.measure = {
33
+ exclusiveTimers: [],
34
+ timings: {},
35
+ observers: {},
36
+ inclusive: {},
37
+ exclusive: {}
38
+ };
39
+ }
40
+ this.started = true;
41
+ }
42
+
43
+ /**
14
44
  * Starts measuring.
15
45
  * @param {string} key Unique identifier for action.
16
- */},{key:"startMeasure",value:function startMeasure(key){if(!this.started)return;// Prepare storage for key if not existing yet.
17
- if(typeof this.measure.inclusive[key]==='undefined'){this.measure.inclusive[key]=this.method!==KEY_FIGURE_METHOD_OBSERVER?0:{};this.measure.exclusive[key]=this.method!==KEY_FIGURE_METHOD_OBSERVER?0:{};}// Push the current start time into an array (allows to collect the action multiple times)
18
- if(this.method===KEY_FIGURE_METHOD_TIME){this.measure.timings[key]=this.measure.timings[key]||[];this.measure.timings[key].push({start:performance.now(),stop:null,others:{}});}// Push the performance observer instance and start watching for events.
19
- if(this.method===KEY_FIGURE_METHOD_OBSERVER){// Create observer and start observing.
20
- var observer={instance:new PerformanceObserver(function(event){var _observer$stored;return(_observer$stored=observer.stored).push.apply(_observer$stored,event.getEntries());}),events:[],stored:[]};observer.instance.observe({entryTypes:['measure','paint','navigation','resource']});this.measure.observers[key]=this.measure.observers[key]||[];this.measure.observers[key].push(observer);}}/**
46
+ */
47
+ startMeasure(key) {
48
+ if (!this.started) return;
49
+
50
+ // Prepare storage for key if not existing yet.
51
+ if (typeof this.measure.inclusive[key] === 'undefined') {
52
+ this.measure.inclusive[key] = this.method !== KEY_FIGURE_METHOD_OBSERVER ? 0 : {};
53
+ this.measure.exclusive[key] = this.method !== KEY_FIGURE_METHOD_OBSERVER ? 0 : {};
54
+ }
55
+
56
+ // Push the current start time into an array (allows to collect the action multiple times)
57
+ if (this.method === KEY_FIGURE_METHOD_TIME) {
58
+ this.measure.timings[key] = this.measure.timings[key] || [];
59
+ this.measure.timings[key].push({
60
+ start: performance.now(),
61
+ stop: null,
62
+ others: {}
63
+ });
64
+ }
65
+
66
+ // Push the performance observer instance and start watching for events.
67
+ if (this.method === KEY_FIGURE_METHOD_OBSERVER) {
68
+ // Create observer and start observing.
69
+ const observer = {
70
+ instance: new PerformanceObserver(event => observer.stored.push(...event.getEntries())),
71
+ events: [],
72
+ stored: []
73
+ };
74
+ observer.instance.observe({
75
+ entryTypes: ['measure', 'paint', 'navigation', 'resource']
76
+ });
77
+ this.measure.observers[key] = this.measure.observers[key] || [];
78
+ this.measure.observers[key].push(observer);
79
+ }
80
+ }
81
+
82
+ /**
21
83
  * Stops measuring.
22
84
  * @param {string} key Unique key.
23
- */},{key:"stopMeasure",value:function stopMeasure(key){var _this=this;if(!this.started)return;// For counting mode just increment the call count.
24
- if(this.method===KEY_FIGURE_METHOD_COUNT){this.measure.inclusive[key]+=1;this.measure.exclusive[key]+=1;}// Evaluate performance events.
25
- if(this.method===KEY_FIGURE_METHOD_OBSERVER){/*
85
+ */
86
+ stopMeasure(key) {
87
+ if (!this.started) return;
88
+
89
+ // For counting mode just increment the call count.
90
+ if (this.method === KEY_FIGURE_METHOD_COUNT) {
91
+ this.measure.inclusive[key] += 1;
92
+ this.measure.exclusive[key] += 1;
93
+ }
94
+
95
+ // Evaluate performance events.
96
+ if (this.method === KEY_FIGURE_METHOD_OBSERVER) {
97
+ /*
26
98
  * Collect all events from the observer.
27
99
  *
28
100
  * NOTE: Chrome behaves different in certain cases:
@@ -30,21 +102,99 @@ if(this.method===KEY_FIGURE_METHOD_OBSERVER){/*
30
102
  * the event callback is called for each event.
31
103
  * - The event callback is not executed at all but `takeRecords`
32
104
  * will return all events.
33
- */var _this$measure$observe2=this.measure.observers[key].pop(),instance=_this$measure$observe2.instance,stored=_this$measure$observe2.stored;var records=instance.takeRecords();var events=records.length===0?stored:records;instance.disconnect();// Stops observing, free memory etc.
34
- // We have to parse the event name to figure out what kind of action it was.
35
- var renders=events.filter(function(event){return event.entryType==='measure';}).filter(function(event){return event.name.endsWith('[update]');}).map(function(event){return{duration:event.duration,componentName:event.name.replace('⚛','').replace(/ /g,'').replace('[update]','')};});var mounts=events.filter(function(event){return event.entryType==='measure';}).filter(function(event){return event.name.endsWith('.componentDidMount');}).map(function(event){return{duration:event.duration,componentName:event.name.replace('⚛','').replace(/ /g,'').replace('.componentDidMount','')};});// Process renders that happened for each component during the benchmark.
36
- renders.forEach(function(_ref){var componentName=_ref.componentName,duration=_ref.duration;// For the first time component rendered we need to start at 1.
37
- _this.measure.inclusive[key][componentName]=_this.measure.inclusive[key][componentName]||{render:0,renderTime:0,mount:0,mountTime:0};// Increment total renders by 1.
38
- var comp=_this.measure.inclusive[key][componentName];comp.render+=1;comp.renderTime+=duration;});// Process mounts
39
- mounts.forEach(function(_ref2){var componentName=_ref2.componentName,duration=_ref2.duration;// For the first time component mounted we need to start at 1.
40
- _this.measure.inclusive[key][componentName]=_this.measure.inclusive[key][componentName]||{render:0,renderTime:0,mount:0,mountTime:0};// Increment total mounts by 1.
41
- var comp=_this.measure.inclusive[key][componentName];comp.mount+=1;comp.mountTime+=duration;});}// For timing mode we diff start with end time and summarize the timing.
42
- if(this.method===KEY_FIGURE_METHOD_TIME){// Add inclusive timer.
43
- var timing=this.measure.timings[key].pop();var time=performance.now()-timing.start;// Add exclusive timer.
44
- this.measure.exclusiveTimers.push({key:key,time:time});// Calculate measure.
45
- var currentMeasure={inclusive:time,exclusive:time};/**
105
+ */
106
+ const {
107
+ instance,
108
+ stored
109
+ } = this.measure.observers[key].pop();
110
+ const records = instance.takeRecords();
111
+ const events = records.length === 0 ? stored : records;
112
+ instance.disconnect(); // Stops observing, free memory etc.
113
+
114
+ // We have to parse the event name to figure out what kind of action it was.
115
+ const renders = events.filter(event => event.entryType === 'measure').filter(event => event.name.endsWith('[update]')).map(event => ({
116
+ duration: event.duration,
117
+ componentName: event.name.replace('⚛', '').replace(/ /g, '').replace('[update]', '')
118
+ }));
119
+ const mounts = events.filter(event => event.entryType === 'measure').filter(event => event.name.endsWith('.componentDidMount')).map(event => ({
120
+ duration: event.duration,
121
+ componentName: event.name.replace('⚛', '').replace(/ /g, '').replace('.componentDidMount', '')
122
+ }));
123
+
124
+ // Process renders that happened for each component during the benchmark.
125
+ renders.forEach(({
126
+ componentName,
127
+ duration
128
+ }) => {
129
+ // For the first time component rendered we need to start at 1.
130
+ this.measure.inclusive[key][componentName] = this.measure.inclusive[key][componentName] || {
131
+ render: 0,
132
+ renderTime: 0,
133
+ mount: 0,
134
+ mountTime: 0
135
+ };
136
+
137
+ // Increment total renders by 1.
138
+ const comp = this.measure.inclusive[key][componentName];
139
+ comp.render += 1;
140
+ comp.renderTime += duration;
141
+ });
142
+
143
+ // Process mounts
144
+ mounts.forEach(({
145
+ componentName,
146
+ duration
147
+ }) => {
148
+ // For the first time component mounted we need to start at 1.
149
+ this.measure.inclusive[key][componentName] = this.measure.inclusive[key][componentName] || {
150
+ render: 0,
151
+ renderTime: 0,
152
+ mount: 0,
153
+ mountTime: 0
154
+ };
155
+
156
+ // Increment total mounts by 1.
157
+ const comp = this.measure.inclusive[key][componentName];
158
+ comp.mount += 1;
159
+ comp.mountTime += duration;
160
+ });
161
+ }
162
+
163
+ // For timing mode we diff start with end time and summarize the timing.
164
+ if (this.method === KEY_FIGURE_METHOD_TIME) {
165
+ // Add inclusive timer.
166
+ const timing = this.measure.timings[key].pop();
167
+ const time = performance.now() - timing.start;
168
+
169
+ // Add exclusive timer.
170
+ this.measure.exclusiveTimers.push({
171
+ key,
172
+ time
173
+ });
174
+
175
+ // Calculate measure.
176
+ const currentMeasure = {
177
+ inclusive: time,
178
+ exclusive: time
179
+ };
180
+
181
+ /**
46
182
  * Calculates exclusive timings by removing other executions that
47
183
  * were running at the same time.
48
184
  * https://stackoverflow.com/a/17902682
49
- */if(this.measure.exclusiveTimers.length>=2){var _this$measure$exclusi2=_toArray(this.measure.exclusiveTimers),first=_this$measure$exclusi2[0],remaining=_this$measure$exclusi2.slice(1);this.measure.exclusiveTimers=remaining;currentMeasure.exclusive-=first.time;// Reset if callstack reached last call.
50
- if(this.measure.exclusiveTimers.length===1){this.measure.exclusiveTimers=[];}}this.measure.inclusive[key]+=currentMeasure.inclusive;this.measure.exclusive[key]+=currentMeasure.exclusive;}}}]);}();export{KeyFigure as default};
185
+ */
186
+ if (this.measure.exclusiveTimers.length >= 2) {
187
+ const [first, ...remaining] = this.measure.exclusiveTimers;
188
+ this.measure.exclusiveTimers = remaining;
189
+ currentMeasure.exclusive -= first.time;
190
+
191
+ // Reset if callstack reached last call.
192
+ if (this.measure.exclusiveTimers.length === 1) {
193
+ this.measure.exclusiveTimers = [];
194
+ }
195
+ }
196
+ this.measure.inclusive[key] += currentMeasure.inclusive;
197
+ this.measure.exclusive[key] += currentMeasure.exclusive;
198
+ }
199
+ }
200
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@shopgate/pwa-benchmark",
3
- "version": "7.30.0-alpha.7",
3
+ "version": "7.30.0-alpha.8",
4
4
  "description": "Benchmark suite for PWA",
5
5
  "author": "Rene Eichhorn",
6
6
  "main": "index.js",
@@ -1 +1,11 @@
1
- import Benchmark from'..';export default(function(){return function(next){return function(action){Benchmark.getKeyFigure('ActionCount').startMeasure(action.type);Benchmark.getKeyFigure('ActionTime').startMeasure(action.type);Benchmark.getKeyFigure('ActionEvents').startMeasure(action.type);var result=next(action);Benchmark.getKeyFigure('ActionEvents').stopMeasure(action.type);Benchmark.getKeyFigure('ActionTime').stopMeasure(action.type);Benchmark.getKeyFigure('ActionCount').stopMeasure(action.type);return result;};};});
1
+ import Benchmark from '..';
2
+ export default () => next => action => {
3
+ Benchmark.getKeyFigure('ActionCount').startMeasure(action.type);
4
+ Benchmark.getKeyFigure('ActionTime').startMeasure(action.type);
5
+ Benchmark.getKeyFigure('ActionEvents').startMeasure(action.type);
6
+ const result = next(action);
7
+ Benchmark.getKeyFigure('ActionEvents').stopMeasure(action.type);
8
+ Benchmark.getKeyFigure('ActionTime').stopMeasure(action.type);
9
+ Benchmark.getKeyFigure('ActionCount').stopMeasure(action.type);
10
+ return result;
11
+ };