@oino-ts/common 0.9.0 → 0.10.0

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.
@@ -56,8 +56,8 @@ class OINOBenchmark {
56
56
  * @param module of the benchmark
57
57
  * @param method of the benchmark
58
58
  */
59
- static start(module, method) {
60
- OINOBenchmark._instance?._start(module, method);
59
+ static startMetric(module, method) {
60
+ OINOBenchmark._instance?._startMetric(module, method);
61
61
  }
62
62
  /**
63
63
  * Complete benchmark timing
@@ -66,8 +66,8 @@ class OINOBenchmark {
66
66
  * @param method of the benchmark
67
67
  * @param category optional subcategory of the benchmark
68
68
  */
69
- static end(module, method, category) {
70
- return OINOBenchmark._instance?._end(module, method, category) || 0;
69
+ static endMetric(module, method, category = "OK") {
70
+ OINOBenchmark._instance?._endMetric(module, method, category);
71
71
  }
72
72
  /**
73
73
  * Get given benchmark data.
@@ -76,27 +76,64 @@ class OINOBenchmark {
76
76
  * @param method of the benchmark
77
77
  *
78
78
  */
79
- static get(module, method) {
80
- return OINOBenchmark._instance?._get(module, method);
79
+ static getMetric(module, method) {
80
+ return OINOBenchmark._instance?._getMetric(module, method);
81
81
  }
82
82
  /**
83
83
  * Get all benchmark data.
84
84
  *
85
85
  */
86
- static getAll() {
87
- return OINOBenchmark._instance?._getAll();
86
+ static getMetrics() {
87
+ return OINOBenchmark._instance?._getMetrics();
88
+ }
89
+ /**
90
+ * Track a metric value
91
+ *
92
+ * @param value of the metric
93
+ * @param module of the metric
94
+ * @param method of the metric
95
+ * @param category optional subcategory of the metric
96
+ *
97
+ */
98
+ static trackMetric(module, method, category, value) {
99
+ if (OINOBenchmark._enabled[module]) {
100
+ OINOBenchmark._instance?._trackMetric(module, method, category, value);
101
+ }
102
+ }
103
+ /**
104
+ * Track an exception
105
+ *
106
+ * @param module of the benchmark
107
+ * @param method of the benchmark
108
+ * @param category optional subcategory of the benchmark
109
+ * @param name of the exception
110
+ * @param message of the exception
111
+ * @param stack trace of the exception
112
+ */
113
+ static trackException(module, method, category, name, message, stack) {
114
+ if (OINOBenchmark._enabled[module]) {
115
+ OINOBenchmark._instance?._trackException(module, method, category, name, message, stack);
116
+ }
117
+ }
118
+ /**
119
+ * Get all tracked exceptions.
120
+ *
121
+ */
122
+ static getExceptions() {
123
+ return OINOBenchmark._instance?._getExceptions();
88
124
  }
89
125
  }
90
126
  exports.OINOBenchmark = OINOBenchmark;
91
127
  /**
92
128
  * OINOMemoryBenchmark is a memory-based benchmark implementation.
93
129
  * It stores the benchmark data in memory and allows to reset, start, end and get benchmark data.
94
- *
130
+ * In case of recursively/iteratively starting a benchmark, it will honor the first start and ignore the rest. *
95
131
  */
96
132
  class OINOMemoryBenchmark extends OINOBenchmark {
97
133
  _benchmarkCount = {};
98
134
  _benchmarkData = {};
99
135
  _benchmarkStart = {};
136
+ _exceptions = [];
100
137
  /**
101
138
  * Reset benchmark data (but not what is enabled).
102
139
  *
@@ -111,13 +148,9 @@ class OINOMemoryBenchmark extends OINOBenchmark {
111
148
  * @param module of the benchmark
112
149
  * @param method of the benchmark
113
150
  */
114
- _start(module, method) {
151
+ _startMetric(module, method) {
115
152
  const name = module + "." + method;
116
- if (OINOBenchmark._enabled[module]) {
117
- if (this._benchmarkCount[name] == undefined) {
118
- this._benchmarkCount[name] = 0;
119
- this._benchmarkData[name] = 0;
120
- }
153
+ if (OINOBenchmark._enabled[module] && ((this._benchmarkStart[name] === undefined) || (this._benchmarkStart[name] === 0))) { // if benchmark is already started (e.g. loop/recursion), do not start it again
121
154
  this._benchmarkStart[name] = performance.now();
122
155
  }
123
156
  }
@@ -128,25 +161,14 @@ class OINOMemoryBenchmark extends OINOBenchmark {
128
161
  * @param method of the benchmark
129
162
  * @param category optional subcategory of the benchmark
130
163
  */
131
- _end(module, method, category) {
164
+ _endMetric(module, method, category) {
132
165
  const name = module + "." + method;
133
166
  let result = 0;
134
- if (OINOBenchmark._enabled[module]) {
167
+ if (OINOBenchmark._enabled[module] && (this._benchmarkStart[name] > 0)) { // if benchmark is started, end it
135
168
  const duration = performance.now() - this._benchmarkStart[name];
136
- this._benchmarkCount[name] += 1;
137
- this._benchmarkData[name] += duration;
138
- if (category) {
139
- const category_name = name + "." + category;
140
- if (this._benchmarkCount[category_name] == undefined) {
141
- this._benchmarkCount[category_name] = 0;
142
- this._benchmarkData[category_name] = 0;
143
- }
144
- this._benchmarkCount[category_name] += 1;
145
- this._benchmarkData[category_name] += duration;
146
- }
147
- result = this._benchmarkData[name] / this._benchmarkCount[name];
169
+ this._trackMetric(module, method, category, duration);
148
170
  }
149
- return result;
171
+ return;
150
172
  }
151
173
  /**
152
174
  * Get given benchmark data.
@@ -155,7 +177,7 @@ class OINOMemoryBenchmark extends OINOBenchmark {
155
177
  * @param method of the benchmark
156
178
  *
157
179
  */
158
- _get(module, method) {
180
+ _getMetric(module, method) {
159
181
  const name = module + "." + method;
160
182
  if (OINOBenchmark._enabled[module] && (this._benchmarkCount[name] > 0)) {
161
183
  return this._benchmarkData[module] / this._benchmarkCount[module];
@@ -166,7 +188,7 @@ class OINOMemoryBenchmark extends OINOBenchmark {
166
188
  * Get all benchmark data.
167
189
  *
168
190
  */
169
- _getAll() {
191
+ _getMetrics() {
170
192
  let result = {};
171
193
  for (const name in this._benchmarkData) {
172
194
  if (this._benchmarkCount[name] > 0) {
@@ -175,5 +197,33 @@ class OINOMemoryBenchmark extends OINOBenchmark {
175
197
  }
176
198
  return result;
177
199
  }
200
+ _trackMetric(module, method, category, value) {
201
+ const name = module + "." + method;
202
+ if (this._benchmarkCount[name] == undefined) {
203
+ this._benchmarkCount[name] = 1;
204
+ this._benchmarkData[name] = value;
205
+ }
206
+ else {
207
+ this._benchmarkCount[name] += 1;
208
+ this._benchmarkData[name] += value;
209
+ }
210
+ const category_name = name + "." + category;
211
+ if (this._benchmarkCount[category_name] == undefined) {
212
+ this._benchmarkCount[category_name] = 1;
213
+ this._benchmarkData[category_name] = value;
214
+ }
215
+ else {
216
+ this._benchmarkCount[category_name] += 1;
217
+ this._benchmarkData[category_name] += value;
218
+ }
219
+ this._benchmarkStart[name] = 0;
220
+ }
221
+ _trackException(module, method, category, name, message, stack) {
222
+ const exception = { module, method, category, name, message, stack };
223
+ this._exceptions.push(exception);
224
+ }
225
+ _getExceptions() {
226
+ return this._exceptions;
227
+ }
178
228
  }
179
229
  exports.OINOMemoryBenchmark = OINOMemoryBenchmark;
@@ -116,10 +116,10 @@ class OINOHtmlTemplate {
116
116
  *
117
117
  */
118
118
  renderFromKeyValue(key, value, removeUnusedTags = true) {
119
- _1.OINOBenchmark.start("OINOHtmlTemplate", "renderFromKeyValue");
119
+ _1.OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromKeyValue");
120
120
  this.setVariableFromValue(key, value);
121
121
  const result = this.render(removeUnusedTags);
122
- _1.OINOBenchmark.end("OINOHtmlTemplate", "renderFromKeyValue");
122
+ _1.OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromKeyValue");
123
123
  return result;
124
124
  }
125
125
  /**
@@ -130,10 +130,10 @@ class OINOHtmlTemplate {
130
130
  *
131
131
  */
132
132
  renderFromObject(object, removeUnusedTags = true) {
133
- _1.OINOBenchmark.start("OINOHtmlTemplate", "renderFromObject");
133
+ _1.OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromObject");
134
134
  this.setVariableFromProperties(object);
135
135
  const result = this.render(removeUnusedTags);
136
- _1.OINOBenchmark.end("OINOHtmlTemplate", "renderFromObject");
136
+ _1.OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromObject");
137
137
  return result;
138
138
  }
139
139
  /**
@@ -149,7 +149,7 @@ class OINOHtmlTemplate {
149
149
  *
150
150
  */
151
151
  renderFromResult(result, removeUnusedTags = true, messageSeparator = "", includeErrorMessages = false, includeWarningMessages = false, includeInfoMessages = false, includeDebugMessages = false) {
152
- _1.OINOBenchmark.start("OINOHtmlTemplate", "renderFromResult");
152
+ _1.OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromResult");
153
153
  this.setVariableFromValue("statusCode", result.statusCode.toString());
154
154
  this.setVariableFromValue("statusMessage", result.statusMessage.toString());
155
155
  let messages = [];
@@ -171,7 +171,7 @@ class OINOHtmlTemplate {
171
171
  this.setVariableFromValue("messages", messages.join(messageSeparator), false); // messages have been escaped already
172
172
  }
173
173
  const http_result = this.render(removeUnusedTags);
174
- _1.OINOBenchmark.end("OINOHtmlTemplate", "renderFromResult");
174
+ _1.OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromResult");
175
175
  return http_result;
176
176
  }
177
177
  }
@@ -53,8 +53,8 @@ export class OINOBenchmark {
53
53
  * @param module of the benchmark
54
54
  * @param method of the benchmark
55
55
  */
56
- static start(module, method) {
57
- OINOBenchmark._instance?._start(module, method);
56
+ static startMetric(module, method) {
57
+ OINOBenchmark._instance?._startMetric(module, method);
58
58
  }
59
59
  /**
60
60
  * Complete benchmark timing
@@ -63,8 +63,8 @@ export class OINOBenchmark {
63
63
  * @param method of the benchmark
64
64
  * @param category optional subcategory of the benchmark
65
65
  */
66
- static end(module, method, category) {
67
- return OINOBenchmark._instance?._end(module, method, category) || 0;
66
+ static endMetric(module, method, category = "OK") {
67
+ OINOBenchmark._instance?._endMetric(module, method, category);
68
68
  }
69
69
  /**
70
70
  * Get given benchmark data.
@@ -73,26 +73,63 @@ export class OINOBenchmark {
73
73
  * @param method of the benchmark
74
74
  *
75
75
  */
76
- static get(module, method) {
77
- return OINOBenchmark._instance?._get(module, method);
76
+ static getMetric(module, method) {
77
+ return OINOBenchmark._instance?._getMetric(module, method);
78
78
  }
79
79
  /**
80
80
  * Get all benchmark data.
81
81
  *
82
82
  */
83
- static getAll() {
84
- return OINOBenchmark._instance?._getAll();
83
+ static getMetrics() {
84
+ return OINOBenchmark._instance?._getMetrics();
85
+ }
86
+ /**
87
+ * Track a metric value
88
+ *
89
+ * @param value of the metric
90
+ * @param module of the metric
91
+ * @param method of the metric
92
+ * @param category optional subcategory of the metric
93
+ *
94
+ */
95
+ static trackMetric(module, method, category, value) {
96
+ if (OINOBenchmark._enabled[module]) {
97
+ OINOBenchmark._instance?._trackMetric(module, method, category, value);
98
+ }
99
+ }
100
+ /**
101
+ * Track an exception
102
+ *
103
+ * @param module of the benchmark
104
+ * @param method of the benchmark
105
+ * @param category optional subcategory of the benchmark
106
+ * @param name of the exception
107
+ * @param message of the exception
108
+ * @param stack trace of the exception
109
+ */
110
+ static trackException(module, method, category, name, message, stack) {
111
+ if (OINOBenchmark._enabled[module]) {
112
+ OINOBenchmark._instance?._trackException(module, method, category, name, message, stack);
113
+ }
114
+ }
115
+ /**
116
+ * Get all tracked exceptions.
117
+ *
118
+ */
119
+ static getExceptions() {
120
+ return OINOBenchmark._instance?._getExceptions();
85
121
  }
86
122
  }
87
123
  /**
88
124
  * OINOMemoryBenchmark is a memory-based benchmark implementation.
89
125
  * It stores the benchmark data in memory and allows to reset, start, end and get benchmark data.
90
- *
126
+ * In case of recursively/iteratively starting a benchmark, it will honor the first start and ignore the rest. *
91
127
  */
92
128
  export class OINOMemoryBenchmark extends OINOBenchmark {
93
129
  _benchmarkCount = {};
94
130
  _benchmarkData = {};
95
131
  _benchmarkStart = {};
132
+ _exceptions = [];
96
133
  /**
97
134
  * Reset benchmark data (but not what is enabled).
98
135
  *
@@ -107,13 +144,9 @@ export class OINOMemoryBenchmark extends OINOBenchmark {
107
144
  * @param module of the benchmark
108
145
  * @param method of the benchmark
109
146
  */
110
- _start(module, method) {
147
+ _startMetric(module, method) {
111
148
  const name = module + "." + method;
112
- if (OINOBenchmark._enabled[module]) {
113
- if (this._benchmarkCount[name] == undefined) {
114
- this._benchmarkCount[name] = 0;
115
- this._benchmarkData[name] = 0;
116
- }
149
+ if (OINOBenchmark._enabled[module] && ((this._benchmarkStart[name] === undefined) || (this._benchmarkStart[name] === 0))) { // if benchmark is already started (e.g. loop/recursion), do not start it again
117
150
  this._benchmarkStart[name] = performance.now();
118
151
  }
119
152
  }
@@ -124,25 +157,14 @@ export class OINOMemoryBenchmark extends OINOBenchmark {
124
157
  * @param method of the benchmark
125
158
  * @param category optional subcategory of the benchmark
126
159
  */
127
- _end(module, method, category) {
160
+ _endMetric(module, method, category) {
128
161
  const name = module + "." + method;
129
162
  let result = 0;
130
- if (OINOBenchmark._enabled[module]) {
163
+ if (OINOBenchmark._enabled[module] && (this._benchmarkStart[name] > 0)) { // if benchmark is started, end it
131
164
  const duration = performance.now() - this._benchmarkStart[name];
132
- this._benchmarkCount[name] += 1;
133
- this._benchmarkData[name] += duration;
134
- if (category) {
135
- const category_name = name + "." + category;
136
- if (this._benchmarkCount[category_name] == undefined) {
137
- this._benchmarkCount[category_name] = 0;
138
- this._benchmarkData[category_name] = 0;
139
- }
140
- this._benchmarkCount[category_name] += 1;
141
- this._benchmarkData[category_name] += duration;
142
- }
143
- result = this._benchmarkData[name] / this._benchmarkCount[name];
165
+ this._trackMetric(module, method, category, duration);
144
166
  }
145
- return result;
167
+ return;
146
168
  }
147
169
  /**
148
170
  * Get given benchmark data.
@@ -151,7 +173,7 @@ export class OINOMemoryBenchmark extends OINOBenchmark {
151
173
  * @param method of the benchmark
152
174
  *
153
175
  */
154
- _get(module, method) {
176
+ _getMetric(module, method) {
155
177
  const name = module + "." + method;
156
178
  if (OINOBenchmark._enabled[module] && (this._benchmarkCount[name] > 0)) {
157
179
  return this._benchmarkData[module] / this._benchmarkCount[module];
@@ -162,7 +184,7 @@ export class OINOMemoryBenchmark extends OINOBenchmark {
162
184
  * Get all benchmark data.
163
185
  *
164
186
  */
165
- _getAll() {
187
+ _getMetrics() {
166
188
  let result = {};
167
189
  for (const name in this._benchmarkData) {
168
190
  if (this._benchmarkCount[name] > 0) {
@@ -171,4 +193,32 @@ export class OINOMemoryBenchmark extends OINOBenchmark {
171
193
  }
172
194
  return result;
173
195
  }
196
+ _trackMetric(module, method, category, value) {
197
+ const name = module + "." + method;
198
+ if (this._benchmarkCount[name] == undefined) {
199
+ this._benchmarkCount[name] = 1;
200
+ this._benchmarkData[name] = value;
201
+ }
202
+ else {
203
+ this._benchmarkCount[name] += 1;
204
+ this._benchmarkData[name] += value;
205
+ }
206
+ const category_name = name + "." + category;
207
+ if (this._benchmarkCount[category_name] == undefined) {
208
+ this._benchmarkCount[category_name] = 1;
209
+ this._benchmarkData[category_name] = value;
210
+ }
211
+ else {
212
+ this._benchmarkCount[category_name] += 1;
213
+ this._benchmarkData[category_name] += value;
214
+ }
215
+ this._benchmarkStart[name] = 0;
216
+ }
217
+ _trackException(module, method, category, name, message, stack) {
218
+ const exception = { module, method, category, name, message, stack };
219
+ this._exceptions.push(exception);
220
+ }
221
+ _getExceptions() {
222
+ return this._exceptions;
223
+ }
174
224
  }
@@ -113,10 +113,10 @@ export class OINOHtmlTemplate {
113
113
  *
114
114
  */
115
115
  renderFromKeyValue(key, value, removeUnusedTags = true) {
116
- OINOBenchmark.start("OINOHtmlTemplate", "renderFromKeyValue");
116
+ OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromKeyValue");
117
117
  this.setVariableFromValue(key, value);
118
118
  const result = this.render(removeUnusedTags);
119
- OINOBenchmark.end("OINOHtmlTemplate", "renderFromKeyValue");
119
+ OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromKeyValue");
120
120
  return result;
121
121
  }
122
122
  /**
@@ -127,10 +127,10 @@ export class OINOHtmlTemplate {
127
127
  *
128
128
  */
129
129
  renderFromObject(object, removeUnusedTags = true) {
130
- OINOBenchmark.start("OINOHtmlTemplate", "renderFromObject");
130
+ OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromObject");
131
131
  this.setVariableFromProperties(object);
132
132
  const result = this.render(removeUnusedTags);
133
- OINOBenchmark.end("OINOHtmlTemplate", "renderFromObject");
133
+ OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromObject");
134
134
  return result;
135
135
  }
136
136
  /**
@@ -146,7 +146,7 @@ export class OINOHtmlTemplate {
146
146
  *
147
147
  */
148
148
  renderFromResult(result, removeUnusedTags = true, messageSeparator = "", includeErrorMessages = false, includeWarningMessages = false, includeInfoMessages = false, includeDebugMessages = false) {
149
- OINOBenchmark.start("OINOHtmlTemplate", "renderFromResult");
149
+ OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromResult");
150
150
  this.setVariableFromValue("statusCode", result.statusCode.toString());
151
151
  this.setVariableFromValue("statusMessage", result.statusMessage.toString());
152
152
  let messages = [];
@@ -168,7 +168,7 @@ export class OINOHtmlTemplate {
168
168
  this.setVariableFromValue("messages", messages.join(messageSeparator), false); // messages have been escaped already
169
169
  }
170
170
  const http_result = this.render(removeUnusedTags);
171
- OINOBenchmark.end("OINOHtmlTemplate", "renderFromResult");
171
+ OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromResult");
172
172
  return http_result;
173
173
  }
174
174
  }
@@ -30,15 +30,15 @@ export declare abstract class OINOBenchmark {
30
30
  * @param modules array of those benchmarks that are enabled
31
31
  */
32
32
  static setEnabled(modules: string[]): void;
33
- protected abstract _start(module: string, method: string): void;
33
+ protected abstract _startMetric(module: string, method: string): void;
34
34
  /**
35
35
  * Start benchmark timing.
36
36
  *
37
37
  * @param module of the benchmark
38
38
  * @param method of the benchmark
39
39
  */
40
- static start(module: string, method: string): void;
41
- protected abstract _end(module: string, method: string, category?: string): number;
40
+ static startMetric(module: string, method: string): void;
41
+ protected abstract _endMetric(module: string, method: string, category: string): void;
42
42
  /**
43
43
  * Complete benchmark timing
44
44
  *
@@ -46,8 +46,8 @@ export declare abstract class OINOBenchmark {
46
46
  * @param method of the benchmark
47
47
  * @param category optional subcategory of the benchmark
48
48
  */
49
- static end(module: string, method: string, category?: string): number;
50
- protected abstract _get(module: string, method: string): number;
49
+ static endMetric(module: string, method: string, category?: string): void;
50
+ protected abstract _getMetric(module: string, method: string): number;
51
51
  /**
52
52
  * Get given benchmark data.
53
53
  *
@@ -55,23 +55,53 @@ export declare abstract class OINOBenchmark {
55
55
  * @param method of the benchmark
56
56
  *
57
57
  */
58
- static get(module: string, method: string): number;
59
- protected abstract _getAll(): Record<string, number>;
58
+ static getMetric(module: string, method: string): number;
59
+ protected abstract _getMetrics(): Record<string, number>;
60
60
  /**
61
61
  * Get all benchmark data.
62
62
  *
63
63
  */
64
- static getAll(): Record<string, number>;
64
+ static getMetrics(): Record<string, number>;
65
+ protected abstract _trackMetric(module: string, method: string, category: string, value: number): void;
66
+ /**
67
+ * Track a metric value
68
+ *
69
+ * @param value of the metric
70
+ * @param module of the metric
71
+ * @param method of the metric
72
+ * @param category optional subcategory of the metric
73
+ *
74
+ */
75
+ static trackMetric(module: string, method: string, category: string, value: number): void;
76
+ protected abstract _trackException(module: string, method: string, category: string, name: string, message: string, stack: string): void;
77
+ /**
78
+ * Track an exception
79
+ *
80
+ * @param module of the benchmark
81
+ * @param method of the benchmark
82
+ * @param category optional subcategory of the benchmark
83
+ * @param name of the exception
84
+ * @param message of the exception
85
+ * @param stack trace of the exception
86
+ */
87
+ static trackException(module: string, method: string, category: string, name: string, message: string, stack: string): void;
88
+ protected abstract _getExceptions(): any[];
89
+ /**
90
+ * Get all tracked exceptions.
91
+ *
92
+ */
93
+ static getExceptions(): any[];
65
94
  }
66
95
  /**
67
96
  * OINOMemoryBenchmark is a memory-based benchmark implementation.
68
97
  * It stores the benchmark data in memory and allows to reset, start, end and get benchmark data.
69
- *
98
+ * In case of recursively/iteratively starting a benchmark, it will honor the first start and ignore the rest. *
70
99
  */
71
100
  export declare class OINOMemoryBenchmark extends OINOBenchmark {
72
- private _benchmarkCount;
73
- private _benchmarkData;
74
- private _benchmarkStart;
101
+ protected _benchmarkCount: Record<string, number>;
102
+ protected _benchmarkData: Record<string, number>;
103
+ protected _benchmarkStart: Record<string, number>;
104
+ protected _exceptions: any[];
75
105
  /**
76
106
  * Reset benchmark data (but not what is enabled).
77
107
  *
@@ -83,7 +113,7 @@ export declare class OINOMemoryBenchmark extends OINOBenchmark {
83
113
  * @param module of the benchmark
84
114
  * @param method of the benchmark
85
115
  */
86
- protected _start(module: string, method: string): void;
116
+ protected _startMetric(module: string, method: string): void;
87
117
  /**
88
118
  * Complete benchmark timing
89
119
  *
@@ -91,7 +121,7 @@ export declare class OINOMemoryBenchmark extends OINOBenchmark {
91
121
  * @param method of the benchmark
92
122
  * @param category optional subcategory of the benchmark
93
123
  */
94
- protected _end(module: string, method: string, category?: string): number;
124
+ protected _endMetric(module: string, method: string, category: string): void;
95
125
  /**
96
126
  * Get given benchmark data.
97
127
  *
@@ -99,10 +129,13 @@ export declare class OINOMemoryBenchmark extends OINOBenchmark {
99
129
  * @param method of the benchmark
100
130
  *
101
131
  */
102
- protected _get(module: string, method: string): number;
132
+ protected _getMetric(module: string, method: string): number;
103
133
  /**
104
134
  * Get all benchmark data.
105
135
  *
106
136
  */
107
- protected _getAll(): Record<string, number>;
137
+ protected _getMetrics(): Record<string, number>;
138
+ protected _trackMetric(module: string, method: string, category: string, value: number): void;
139
+ protected _trackException(module: string, method: string, category: string, name: string, message: string, stack: string): void;
140
+ protected _getExceptions(): any[];
108
141
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oino-ts/common",
3
- "version": "0.9.0",
3
+ "version": "0.10.0",
4
4
  "description": "OINO TS package for common classes.",
5
5
  "author": "Matias Kiviniemi (pragmatta)",
6
6
  "license": "MPL-2.0",
@@ -19,7 +19,7 @@
19
19
  "dependencies": {
20
20
  },
21
21
  "devDependencies": {
22
- "@oino-ts/types": "0.9.0"
22
+ "@oino-ts/types": "0.10.0"
23
23
  },
24
24
  "files": [
25
25
  "src/*.ts",
@@ -56,18 +56,18 @@ export abstract class OINOBenchmark {
56
56
  });
57
57
  }
58
58
 
59
- protected abstract _start(module:string, method:string):void
59
+ protected abstract _startMetric(module:string, method:string):void
60
60
  /**
61
61
  * Start benchmark timing.
62
62
  *
63
63
  * @param module of the benchmark
64
64
  * @param method of the benchmark
65
65
  */
66
- static start(module:string, method:string):void {
67
- OINOBenchmark._instance?._start(module, method)
66
+ static startMetric(module:string, method:string):void {
67
+ OINOBenchmark._instance?._startMetric(module, method)
68
68
  }
69
69
 
70
- protected abstract _end(module:string, method:string, category?:string):number
70
+ protected abstract _endMetric(module:string, method:string, category:string):void
71
71
  /**
72
72
  * Complete benchmark timing
73
73
  *
@@ -75,11 +75,11 @@ export abstract class OINOBenchmark {
75
75
  * @param method of the benchmark
76
76
  * @param category optional subcategory of the benchmark
77
77
  */
78
- static end(module:string, method:string, category?:string):number {
79
- return OINOBenchmark._instance?._end(module, method, category) || 0
78
+ static endMetric(module:string, method:string, category:string = "OK"):void {
79
+ OINOBenchmark._instance?._endMetric(module, method, category)
80
80
  }
81
81
 
82
- protected abstract _get(module:string, method:string):number
82
+ protected abstract _getMetric(module:string, method:string):number
83
83
  /**
84
84
  * Get given benchmark data.
85
85
  *
@@ -87,30 +87,74 @@ export abstract class OINOBenchmark {
87
87
  * @param method of the benchmark
88
88
  *
89
89
  */
90
- static get(module:string, method:string):number {
91
- return OINOBenchmark._instance?._get(module, method)
90
+ static getMetric(module:string, method:string):number {
91
+ return OINOBenchmark._instance?._getMetric(module, method)
92
92
  }
93
93
 
94
- protected abstract _getAll():Record<string, number>
94
+ protected abstract _getMetrics():Record<string, number>
95
95
  /**
96
96
  * Get all benchmark data.
97
97
  *
98
98
  */
99
- static getAll():Record<string, number> {
100
- return OINOBenchmark._instance?._getAll()
99
+ static getMetrics():Record<string, number> {
100
+ return OINOBenchmark._instance?._getMetrics()
101
+ }
102
+
103
+ protected abstract _trackMetric(module:string, method:string, category:string, value:number):void
104
+ /**
105
+ * Track a metric value
106
+ *
107
+ * @param value of the metric
108
+ * @param module of the metric
109
+ * @param method of the metric
110
+ * @param category optional subcategory of the metric
111
+ *
112
+ */
113
+ static trackMetric(module:string, method:string, category:string, value:number):void {
114
+ if (OINOBenchmark._enabled[module]) {
115
+ OINOBenchmark._instance?._trackMetric(module, method, category, value)
116
+ }
117
+ }
118
+
119
+ protected abstract _trackException(module:string, method:string, category:string, name:string, message:string, stack: string):void
120
+ /**
121
+ * Track an exception
122
+ *
123
+ * @param module of the benchmark
124
+ * @param method of the benchmark
125
+ * @param category optional subcategory of the benchmark
126
+ * @param name of the exception
127
+ * @param message of the exception
128
+ * @param stack trace of the exception
129
+ */
130
+ static trackException(module:string, method:string, category:string, name:string, message:string, stack:string):void {
131
+ if (OINOBenchmark._enabled[module]) {
132
+ OINOBenchmark._instance?._trackException(module, method, category, name, message, stack)
133
+ }
134
+ }
135
+
136
+ protected abstract _getExceptions():any[]
137
+ /**
138
+ * Get all tracked exceptions.
139
+ *
140
+ */
141
+ static getExceptions():any[] {
142
+ return OINOBenchmark._instance?._getExceptions()
101
143
  }
102
144
  }
103
145
 
104
146
  /**
105
147
  * OINOMemoryBenchmark is a memory-based benchmark implementation.
106
148
  * It stores the benchmark data in memory and allows to reset, start, end and get benchmark data.
107
- *
149
+ * In case of recursively/iteratively starting a benchmark, it will honor the first start and ignore the rest. *
108
150
  */
109
151
  export class OINOMemoryBenchmark extends OINOBenchmark {
110
152
 
111
- private _benchmarkCount:Record<string, number> = {}
112
- private _benchmarkData:Record<string, number> = {}
113
- private _benchmarkStart:Record<string, number> = {}
153
+ protected _benchmarkCount:Record<string, number> = {}
154
+ protected _benchmarkData:Record<string, number> = {}
155
+ protected _benchmarkStart:Record<string, number> = {}
156
+
157
+ protected _exceptions:any[] = []
114
158
 
115
159
  /**
116
160
  * Reset benchmark data (but not what is enabled).
@@ -127,13 +171,9 @@ export class OINOMemoryBenchmark extends OINOBenchmark {
127
171
  * @param module of the benchmark
128
172
  * @param method of the benchmark
129
173
  */
130
- protected _start(module:string, method:string):void {
174
+ protected _startMetric(module:string, method:string):void {
131
175
  const name:string = module + "." + method
132
- if (OINOBenchmark._enabled[module]) {
133
- if (this._benchmarkCount[name] == undefined) {
134
- this._benchmarkCount[name] = 0
135
- this._benchmarkData[name] = 0
136
- }
176
+ if (OINOBenchmark._enabled[module] && ((this._benchmarkStart[name] === undefined) || (this._benchmarkStart[name] === 0))) { // if benchmark is already started (e.g. loop/recursion), do not start it again
137
177
  this._benchmarkStart[name] = performance.now()
138
178
  }
139
179
  }
@@ -145,25 +185,14 @@ export class OINOMemoryBenchmark extends OINOBenchmark {
145
185
  * @param method of the benchmark
146
186
  * @param category optional subcategory of the benchmark
147
187
  */
148
- protected _end(module:string, method:string, category?:string):number {
188
+ protected _endMetric(module:string, method:string, category:string):void {
149
189
  const name:string = module + "." + method
150
190
  let result:number = 0
151
- if (OINOBenchmark._enabled[module]) {
191
+ if (OINOBenchmark._enabled[module] && (this._benchmarkStart[name] > 0)) { // if benchmark is started, end it
152
192
  const duration = performance.now() - this._benchmarkStart[name]
153
- this._benchmarkCount[name] += 1
154
- this._benchmarkData[name] += duration
155
- if (category) {
156
- const category_name = name + "." + category
157
- if (this._benchmarkCount[category_name] == undefined) {
158
- this._benchmarkCount[category_name] = 0
159
- this._benchmarkData[category_name] = 0
160
- }
161
- this._benchmarkCount[category_name] += 1
162
- this._benchmarkData[category_name] += duration
163
- }
164
- result = this._benchmarkData[name] / this._benchmarkCount[name]
193
+ this._trackMetric(module, method, category, duration)
165
194
  }
166
- return result
195
+ return
167
196
  }
168
197
 
169
198
  /**
@@ -173,7 +202,7 @@ export class OINOMemoryBenchmark extends OINOBenchmark {
173
202
  * @param method of the benchmark
174
203
  *
175
204
  */
176
- protected _get(module:string, method:string):number {
205
+ protected _getMetric(module:string, method:string):number {
177
206
  const name:string = module + "." + method
178
207
  if (OINOBenchmark._enabled[module] && (this._benchmarkCount[name] > 0)) {
179
208
  return this._benchmarkData[module] / this._benchmarkCount[module]
@@ -185,7 +214,7 @@ export class OINOMemoryBenchmark extends OINOBenchmark {
185
214
  * Get all benchmark data.
186
215
  *
187
216
  */
188
- protected _getAll():Record<string, number> {
217
+ protected _getMetrics():Record<string, number> {
189
218
  let result:Record<string, number> = {}
190
219
  for (const name in this._benchmarkData) {
191
220
  if (this._benchmarkCount[name] > 0) {
@@ -194,4 +223,35 @@ export class OINOMemoryBenchmark extends OINOBenchmark {
194
223
  }
195
224
  return result
196
225
  }
226
+
227
+ protected _trackMetric(module:string, method:string, category:string, value:number):void {
228
+ const name:string = module + "." + method
229
+ if (this._benchmarkCount[name] == undefined) {
230
+ this._benchmarkCount[name] = 1
231
+ this._benchmarkData[name] = value
232
+ } else {
233
+ this._benchmarkCount[name] += 1
234
+ this._benchmarkData[name] += value
235
+ }
236
+
237
+ const category_name = name + "." + category
238
+ if (this._benchmarkCount[category_name] == undefined) {
239
+ this._benchmarkCount[category_name] = 1
240
+ this._benchmarkData[category_name] = value
241
+ } else {
242
+ this._benchmarkCount[category_name] += 1
243
+ this._benchmarkData[category_name] += value
244
+ }
245
+
246
+ this._benchmarkStart[name] = 0
247
+ }
248
+
249
+ protected _trackException(module:string, method:string, category:string, name:string, message:string, stack:string):void {
250
+ const exception = { module, method, category, name, message, stack }
251
+ this._exceptions.push(exception)
252
+ }
253
+
254
+ protected _getExceptions():any[] {
255
+ return this._exceptions
256
+ }
197
257
  }
@@ -124,10 +124,10 @@ export class OINOHtmlTemplate {
124
124
  *
125
125
  */
126
126
  renderFromKeyValue(key:string, value:string, removeUnusedTags:boolean = true):OINOHttpResult {
127
- OINOBenchmark.start("OINOHtmlTemplate", "renderFromKeyValue")
127
+ OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromKeyValue")
128
128
  this.setVariableFromValue(key, value)
129
129
  const result:OINOHttpResult = this.render(removeUnusedTags)
130
- OINOBenchmark.end("OINOHtmlTemplate", "renderFromKeyValue")
130
+ OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromKeyValue")
131
131
  return result
132
132
  }
133
133
 
@@ -139,10 +139,10 @@ export class OINOHtmlTemplate {
139
139
  *
140
140
  */
141
141
  renderFromObject(object:any, removeUnusedTags:boolean = true):OINOHttpResult {
142
- OINOBenchmark.start("OINOHtmlTemplate", "renderFromObject")
142
+ OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromObject")
143
143
  this.setVariableFromProperties(object)
144
144
  const result:OINOHttpResult = this.render(removeUnusedTags)
145
- OINOBenchmark.end("OINOHtmlTemplate", "renderFromObject")
145
+ OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromObject")
146
146
  return result
147
147
  }
148
148
 
@@ -159,7 +159,7 @@ export class OINOHtmlTemplate {
159
159
  *
160
160
  */
161
161
  renderFromResult(result:OINOResult, removeUnusedTags:boolean=true, messageSeparator:string = "", includeErrorMessages:boolean=false, includeWarningMessages:boolean=false, includeInfoMessages:boolean=false, includeDebugMessages:boolean=false):OINOHttpResult {
162
- OINOBenchmark.start("OINOHtmlTemplate", "renderFromResult")
162
+ OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromResult")
163
163
  this.setVariableFromValue("statusCode", result.statusCode.toString())
164
164
  this.setVariableFromValue("statusMessage", result.statusMessage.toString())
165
165
  let messages:string[] = []
@@ -182,7 +182,7 @@ export class OINOHtmlTemplate {
182
182
  this.setVariableFromValue("messages", messages.join(messageSeparator), false) // messages have been escaped already
183
183
  }
184
184
  const http_result:OINOHttpResult = this.render(removeUnusedTags)
185
- OINOBenchmark.end("OINOHtmlTemplate", "renderFromResult")
185
+ OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromResult")
186
186
  return http_result
187
187
  }
188
188
  };