@oino-ts/common 0.8.2 → 0.9.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.
@@ -5,33 +5,49 @@
5
5
  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
6
  */
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
8
- exports.OINOBenchmark = void 0;
8
+ exports.OINOMemoryBenchmark = exports.OINOBenchmark = void 0;
9
9
  /**
10
10
  * Static class for benchmarking functions.
11
11
  *
12
12
  */
13
13
  class OINOBenchmark {
14
- static _benchmarkCount = {};
15
- static _benchmarkData = {};
16
- static _benchmarkEnabled = {};
17
- static _benchmarkStart = {};
14
+ static _instance;
15
+ static _enabled = {};
16
+ /**
17
+ * Create a new OINOBenchmark instance.
18
+ *
19
+ * @param enabledModules array of those benchmarks that are enabled
20
+ */
21
+ constructor(enabledModules = []) {
22
+ OINOBenchmark.setEnabled(enabledModules);
23
+ }
24
+ /**
25
+ * Set active benchmarking instance.
26
+ *
27
+ * @param instance OINOBenchmark instance
28
+ *
29
+ */
30
+ static setInstance(instance) {
31
+ if (instance) {
32
+ OINOBenchmark._instance = instance;
33
+ }
34
+ }
18
35
  /**
19
36
  * Reset benchmark data (but not what is enabled).
20
37
  *
21
38
  */
22
39
  static reset() {
23
- this._benchmarkData = {};
24
- this._benchmarkCount = {};
40
+ OINOBenchmark._instance?._reset();
25
41
  }
26
42
  /**
27
43
  * Set benchmark names that are enabled.
28
44
  *
29
- * @param module array of those benchmarks that are enabled
45
+ * @param modules array of those benchmarks that are enabled
30
46
  */
31
- static setEnabled(module) {
32
- this._benchmarkEnabled = {};
33
- module.forEach(module_name => {
34
- this._benchmarkEnabled[module_name] = true;
47
+ static setEnabled(modules) {
48
+ OINOBenchmark._enabled = {};
49
+ modules.forEach(module_name => {
50
+ this._enabled[module_name] = true;
35
51
  });
36
52
  }
37
53
  /**
@@ -41,8 +57,63 @@ class OINOBenchmark {
41
57
  * @param method of the benchmark
42
58
  */
43
59
  static start(module, method) {
60
+ OINOBenchmark._instance?._start(module, method);
61
+ }
62
+ /**
63
+ * Complete benchmark timing
64
+ *
65
+ * @param module of the benchmark
66
+ * @param method of the benchmark
67
+ * @param category optional subcategory of the benchmark
68
+ */
69
+ static end(module, method, category) {
70
+ return OINOBenchmark._instance?._end(module, method, category) || 0;
71
+ }
72
+ /**
73
+ * Get given benchmark data.
74
+ *
75
+ * @param module of the benchmark
76
+ * @param method of the benchmark
77
+ *
78
+ */
79
+ static get(module, method) {
80
+ return OINOBenchmark._instance?._get(module, method);
81
+ }
82
+ /**
83
+ * Get all benchmark data.
84
+ *
85
+ */
86
+ static getAll() {
87
+ return OINOBenchmark._instance?._getAll();
88
+ }
89
+ }
90
+ exports.OINOBenchmark = OINOBenchmark;
91
+ /**
92
+ * OINOMemoryBenchmark is a memory-based benchmark implementation.
93
+ * It stores the benchmark data in memory and allows to reset, start, end and get benchmark data.
94
+ *
95
+ */
96
+ class OINOMemoryBenchmark extends OINOBenchmark {
97
+ _benchmarkCount = {};
98
+ _benchmarkData = {};
99
+ _benchmarkStart = {};
100
+ /**
101
+ * Reset benchmark data (but not what is enabled).
102
+ *
103
+ */
104
+ _reset() {
105
+ this._benchmarkData = {};
106
+ this._benchmarkCount = {};
107
+ }
108
+ /**
109
+ * Start benchmark timing.
110
+ *
111
+ * @param module of the benchmark
112
+ * @param method of the benchmark
113
+ */
114
+ _start(module, method) {
44
115
  const name = module + "." + method;
45
- if (this._benchmarkEnabled[module]) {
116
+ if (OINOBenchmark._enabled[module]) {
46
117
  if (this._benchmarkCount[name] == undefined) {
47
118
  this._benchmarkCount[name] = 0;
48
119
  this._benchmarkData[name] = 0;
@@ -57,10 +128,10 @@ class OINOBenchmark {
57
128
  * @param method of the benchmark
58
129
  * @param category optional subcategory of the benchmark
59
130
  */
60
- static end(module, method, category) {
131
+ _end(module, method, category) {
61
132
  const name = module + "." + method;
62
133
  let result = 0;
63
- if (this._benchmarkEnabled[module]) {
134
+ if (OINOBenchmark._enabled[module]) {
64
135
  const duration = performance.now() - this._benchmarkStart[name];
65
136
  this._benchmarkCount[name] += 1;
66
137
  this._benchmarkData[name] += duration;
@@ -84,9 +155,9 @@ class OINOBenchmark {
84
155
  * @param method of the benchmark
85
156
  *
86
157
  */
87
- static get(module, method) {
158
+ _get(module, method) {
88
159
  const name = module + "." + method;
89
- if (this._benchmarkEnabled[module]) {
160
+ if (OINOBenchmark._enabled[module] && (this._benchmarkCount[name] > 0)) {
90
161
  return this._benchmarkData[module] / this._benchmarkCount[module];
91
162
  }
92
163
  return -1;
@@ -95,7 +166,7 @@ class OINOBenchmark {
95
166
  * Get all benchmark data.
96
167
  *
97
168
  */
98
- static getAll() {
169
+ _getAll() {
99
170
  let result = {};
100
171
  for (const name in this._benchmarkData) {
101
172
  if (this._benchmarkCount[name] > 0) {
@@ -105,4 +176,4 @@ class OINOBenchmark {
105
176
  return result;
106
177
  }
107
178
  }
108
- exports.OINOBenchmark = OINOBenchmark;
179
+ exports.OINOMemoryBenchmark = OINOMemoryBenchmark;
@@ -103,7 +103,9 @@ class OINOHtmlTemplate {
103
103
  *
104
104
  */
105
105
  render(removeUnusedTags = true) {
106
- return this._createHttpResult(this._renderHtml(), removeUnusedTags);
106
+ const html = this._renderHtml();
107
+ this.clearVariables(); // clear variables after rendering
108
+ return this._createHttpResult(html, removeUnusedTags);
107
109
  }
108
110
  /**
109
111
  * Creates HTML Response from a key-value-pair.
@@ -65,15 +65,15 @@ class OINOLog {
65
65
  }
66
66
  }
67
67
  /**
68
- * Set active logger and log level.
68
+ * Set active logger instance.
69
69
  *
70
- * @param logger logger instance
70
+ * @param instance OINOLog instance
71
71
  *
72
72
  */
73
- static setLogger(logger) {
73
+ static setInstance(instance) {
74
74
  // console.log("setLogger: " + log)
75
- if (logger) {
76
- OINOLog._instance = logger;
75
+ if (instance) {
76
+ OINOLog._instance = instance;
77
77
  }
78
78
  }
79
79
  /**
package/dist/cjs/index.js CHANGED
@@ -1,8 +1,9 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.OINOContentType = exports.OINO_DEBUG_PREFIX = exports.OINO_INFO_PREFIX = exports.OINO_WARNING_PREFIX = exports.OINO_ERROR_PREFIX = exports.OINOHtmlTemplate = exports.OINOStr = exports.OINOHttpResult = exports.OINOResult = exports.OINOConsoleLog = exports.OINOLogLevel = exports.OINOLog = exports.OINOBenchmark = void 0;
3
+ exports.OINOContentType = exports.OINO_DEBUG_PREFIX = exports.OINO_INFO_PREFIX = exports.OINO_WARNING_PREFIX = exports.OINO_ERROR_PREFIX = exports.OINOHtmlTemplate = exports.OINOStr = exports.OINOHttpResult = exports.OINOResult = exports.OINOConsoleLog = exports.OINOLogLevel = exports.OINOLog = exports.OINOMemoryBenchmark = exports.OINOBenchmark = void 0;
4
4
  var OINOBenchmark_js_1 = require("./OINOBenchmark.js");
5
5
  Object.defineProperty(exports, "OINOBenchmark", { enumerable: true, get: function () { return OINOBenchmark_js_1.OINOBenchmark; } });
6
+ Object.defineProperty(exports, "OINOMemoryBenchmark", { enumerable: true, get: function () { return OINOBenchmark_js_1.OINOMemoryBenchmark; } });
6
7
  var OINOLog_js_1 = require("./OINOLog.js");
7
8
  Object.defineProperty(exports, "OINOLog", { enumerable: true, get: function () { return OINOLog_js_1.OINOLog; } });
8
9
  Object.defineProperty(exports, "OINOLogLevel", { enumerable: true, get: function () { return OINOLog_js_1.OINOLogLevel; } });
@@ -8,27 +8,43 @@
8
8
  *
9
9
  */
10
10
  export class OINOBenchmark {
11
- static _benchmarkCount = {};
12
- static _benchmarkData = {};
13
- static _benchmarkEnabled = {};
14
- static _benchmarkStart = {};
11
+ static _instance;
12
+ static _enabled = {};
13
+ /**
14
+ * Create a new OINOBenchmark instance.
15
+ *
16
+ * @param enabledModules array of those benchmarks that are enabled
17
+ */
18
+ constructor(enabledModules = []) {
19
+ OINOBenchmark.setEnabled(enabledModules);
20
+ }
21
+ /**
22
+ * Set active benchmarking instance.
23
+ *
24
+ * @param instance OINOBenchmark instance
25
+ *
26
+ */
27
+ static setInstance(instance) {
28
+ if (instance) {
29
+ OINOBenchmark._instance = instance;
30
+ }
31
+ }
15
32
  /**
16
33
  * Reset benchmark data (but not what is enabled).
17
34
  *
18
35
  */
19
36
  static reset() {
20
- this._benchmarkData = {};
21
- this._benchmarkCount = {};
37
+ OINOBenchmark._instance?._reset();
22
38
  }
23
39
  /**
24
40
  * Set benchmark names that are enabled.
25
41
  *
26
- * @param module array of those benchmarks that are enabled
42
+ * @param modules array of those benchmarks that are enabled
27
43
  */
28
- static setEnabled(module) {
29
- this._benchmarkEnabled = {};
30
- module.forEach(module_name => {
31
- this._benchmarkEnabled[module_name] = true;
44
+ static setEnabled(modules) {
45
+ OINOBenchmark._enabled = {};
46
+ modules.forEach(module_name => {
47
+ this._enabled[module_name] = true;
32
48
  });
33
49
  }
34
50
  /**
@@ -38,8 +54,62 @@ export class OINOBenchmark {
38
54
  * @param method of the benchmark
39
55
  */
40
56
  static start(module, method) {
57
+ OINOBenchmark._instance?._start(module, method);
58
+ }
59
+ /**
60
+ * Complete benchmark timing
61
+ *
62
+ * @param module of the benchmark
63
+ * @param method of the benchmark
64
+ * @param category optional subcategory of the benchmark
65
+ */
66
+ static end(module, method, category) {
67
+ return OINOBenchmark._instance?._end(module, method, category) || 0;
68
+ }
69
+ /**
70
+ * Get given benchmark data.
71
+ *
72
+ * @param module of the benchmark
73
+ * @param method of the benchmark
74
+ *
75
+ */
76
+ static get(module, method) {
77
+ return OINOBenchmark._instance?._get(module, method);
78
+ }
79
+ /**
80
+ * Get all benchmark data.
81
+ *
82
+ */
83
+ static getAll() {
84
+ return OINOBenchmark._instance?._getAll();
85
+ }
86
+ }
87
+ /**
88
+ * OINOMemoryBenchmark is a memory-based benchmark implementation.
89
+ * It stores the benchmark data in memory and allows to reset, start, end and get benchmark data.
90
+ *
91
+ */
92
+ export class OINOMemoryBenchmark extends OINOBenchmark {
93
+ _benchmarkCount = {};
94
+ _benchmarkData = {};
95
+ _benchmarkStart = {};
96
+ /**
97
+ * Reset benchmark data (but not what is enabled).
98
+ *
99
+ */
100
+ _reset() {
101
+ this._benchmarkData = {};
102
+ this._benchmarkCount = {};
103
+ }
104
+ /**
105
+ * Start benchmark timing.
106
+ *
107
+ * @param module of the benchmark
108
+ * @param method of the benchmark
109
+ */
110
+ _start(module, method) {
41
111
  const name = module + "." + method;
42
- if (this._benchmarkEnabled[module]) {
112
+ if (OINOBenchmark._enabled[module]) {
43
113
  if (this._benchmarkCount[name] == undefined) {
44
114
  this._benchmarkCount[name] = 0;
45
115
  this._benchmarkData[name] = 0;
@@ -54,10 +124,10 @@ export class OINOBenchmark {
54
124
  * @param method of the benchmark
55
125
  * @param category optional subcategory of the benchmark
56
126
  */
57
- static end(module, method, category) {
127
+ _end(module, method, category) {
58
128
  const name = module + "." + method;
59
129
  let result = 0;
60
- if (this._benchmarkEnabled[module]) {
130
+ if (OINOBenchmark._enabled[module]) {
61
131
  const duration = performance.now() - this._benchmarkStart[name];
62
132
  this._benchmarkCount[name] += 1;
63
133
  this._benchmarkData[name] += duration;
@@ -81,9 +151,9 @@ export class OINOBenchmark {
81
151
  * @param method of the benchmark
82
152
  *
83
153
  */
84
- static get(module, method) {
154
+ _get(module, method) {
85
155
  const name = module + "." + method;
86
- if (this._benchmarkEnabled[module]) {
156
+ if (OINOBenchmark._enabled[module] && (this._benchmarkCount[name] > 0)) {
87
157
  return this._benchmarkData[module] / this._benchmarkCount[module];
88
158
  }
89
159
  return -1;
@@ -92,7 +162,7 @@ export class OINOBenchmark {
92
162
  * Get all benchmark data.
93
163
  *
94
164
  */
95
- static getAll() {
165
+ _getAll() {
96
166
  let result = {};
97
167
  for (const name in this._benchmarkData) {
98
168
  if (this._benchmarkCount[name] > 0) {
@@ -100,7 +100,9 @@ export class OINOHtmlTemplate {
100
100
  *
101
101
  */
102
102
  render(removeUnusedTags = true) {
103
- return this._createHttpResult(this._renderHtml(), removeUnusedTags);
103
+ const html = this._renderHtml();
104
+ this.clearVariables(); // clear variables after rendering
105
+ return this._createHttpResult(html, removeUnusedTags);
104
106
  }
105
107
  /**
106
108
  * Creates HTML Response from a key-value-pair.
@@ -62,15 +62,15 @@ export class OINOLog {
62
62
  }
63
63
  }
64
64
  /**
65
- * Set active logger and log level.
65
+ * Set active logger instance.
66
66
  *
67
- * @param logger logger instance
67
+ * @param instance OINOLog instance
68
68
  *
69
69
  */
70
- static setLogger(logger) {
70
+ static setInstance(instance) {
71
71
  // console.log("setLogger: " + log)
72
- if (logger) {
73
- OINOLog._instance = logger;
72
+ if (instance) {
73
+ OINOLog._instance = instance;
74
74
  }
75
75
  }
76
76
  /**
package/dist/esm/index.js CHANGED
@@ -1,4 +1,4 @@
1
- export { OINOBenchmark } from "./OINOBenchmark.js";
1
+ export { OINOBenchmark, OINOMemoryBenchmark } from "./OINOBenchmark.js";
2
2
  export { OINOLog, OINOLogLevel, OINOConsoleLog } from "./OINOLog.js";
3
3
  export { OINOResult, OINOHttpResult } from "./OINOResult.js";
4
4
  export { OINOStr } from "./OINOStr.js";
@@ -2,11 +2,23 @@
2
2
  * Static class for benchmarking functions.
3
3
  *
4
4
  */
5
- export declare class OINOBenchmark {
6
- private static _benchmarkCount;
7
- private static _benchmarkData;
8
- private static _benchmarkEnabled;
9
- private static _benchmarkStart;
5
+ export declare abstract class OINOBenchmark {
6
+ protected static _instance: OINOBenchmark;
7
+ protected static _enabled: Record<string, boolean>;
8
+ /**
9
+ * Create a new OINOBenchmark instance.
10
+ *
11
+ * @param enabledModules array of those benchmarks that are enabled
12
+ */
13
+ constructor(enabledModules?: string[]);
14
+ /**
15
+ * Set active benchmarking instance.
16
+ *
17
+ * @param instance OINOBenchmark instance
18
+ *
19
+ */
20
+ static setInstance(instance: OINOBenchmark): void;
21
+ protected abstract _reset(): void;
10
22
  /**
11
23
  * Reset benchmark data (but not what is enabled).
12
24
  *
@@ -15,9 +27,10 @@ export declare class OINOBenchmark {
15
27
  /**
16
28
  * Set benchmark names that are enabled.
17
29
  *
18
- * @param module array of those benchmarks that are enabled
30
+ * @param modules array of those benchmarks that are enabled
19
31
  */
20
- static setEnabled(module: string[]): void;
32
+ static setEnabled(modules: string[]): void;
33
+ protected abstract _start(module: string, method: string): void;
21
34
  /**
22
35
  * Start benchmark timing.
23
36
  *
@@ -25,6 +38,7 @@ export declare class OINOBenchmark {
25
38
  * @param method of the benchmark
26
39
  */
27
40
  static start(module: string, method: string): void;
41
+ protected abstract _end(module: string, method: string, category?: string): number;
28
42
  /**
29
43
  * Complete benchmark timing
30
44
  *
@@ -33,6 +47,7 @@ export declare class OINOBenchmark {
33
47
  * @param category optional subcategory of the benchmark
34
48
  */
35
49
  static end(module: string, method: string, category?: string): number;
50
+ protected abstract _get(module: string, method: string): number;
36
51
  /**
37
52
  * Get given benchmark data.
38
53
  *
@@ -41,9 +56,53 @@ export declare class OINOBenchmark {
41
56
  *
42
57
  */
43
58
  static get(module: string, method: string): number;
59
+ protected abstract _getAll(): Record<string, number>;
60
+ /**
61
+ * Get all benchmark data.
62
+ *
63
+ */
64
+ static getAll(): Record<string, number>;
65
+ }
66
+ /**
67
+ * OINOMemoryBenchmark is a memory-based benchmark implementation.
68
+ * It stores the benchmark data in memory and allows to reset, start, end and get benchmark data.
69
+ *
70
+ */
71
+ export declare class OINOMemoryBenchmark extends OINOBenchmark {
72
+ private _benchmarkCount;
73
+ private _benchmarkData;
74
+ private _benchmarkStart;
75
+ /**
76
+ * Reset benchmark data (but not what is enabled).
77
+ *
78
+ */
79
+ protected _reset(): void;
80
+ /**
81
+ * Start benchmark timing.
82
+ *
83
+ * @param module of the benchmark
84
+ * @param method of the benchmark
85
+ */
86
+ protected _start(module: string, method: string): void;
87
+ /**
88
+ * Complete benchmark timing
89
+ *
90
+ * @param module of the benchmark
91
+ * @param method of the benchmark
92
+ * @param category optional subcategory of the benchmark
93
+ */
94
+ protected _end(module: string, method: string, category?: string): number;
95
+ /**
96
+ * Get given benchmark data.
97
+ *
98
+ * @param module of the benchmark
99
+ * @param method of the benchmark
100
+ *
101
+ */
102
+ protected _get(module: string, method: string): number;
44
103
  /**
45
104
  * Get all benchmark data.
46
105
  *
47
106
  */
48
- static getAll(): number;
107
+ protected _getAll(): Record<string, number>;
49
108
  }
@@ -51,12 +51,12 @@ export declare abstract class OINOLog {
51
51
  */
52
52
  protected static _log(level: OINOLogLevel, levelStr: string, domain: string, channel: string, method: string, message: string, data?: any): void;
53
53
  /**
54
- * Set active logger and log level.
54
+ * Set active logger instance.
55
55
  *
56
- * @param logger logger instance
56
+ * @param instance OINOLog instance
57
57
  *
58
58
  */
59
- static setLogger(logger: OINOLog): void;
59
+ static setInstance(instance: OINOLog): void;
60
60
  /**
61
61
  * Set log level for given combination of domain/channel/method. Not defining dimension(s) means they match any value.
62
62
  * Multiple settings can be combined to set different logging accuracy specifically
@@ -1,4 +1,4 @@
1
- export { OINOBenchmark } from "./OINOBenchmark.js";
1
+ export { OINOBenchmark, OINOMemoryBenchmark } from "./OINOBenchmark.js";
2
2
  export { OINOLog, OINOLogLevel, OINOConsoleLog } from "./OINOLog.js";
3
3
  export { OINOResult, OINOHttpResult } from "./OINOResult.js";
4
4
  export { OINOStr } from "./OINOStr.js";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oino-ts/common",
3
- "version": "0.8.2",
3
+ "version": "0.9.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.8.2"
22
+ "@oino-ts/types": "0.9.0"
23
23
  },
24
24
  "files": [
25
25
  "src/*.ts",
@@ -8,34 +8,55 @@
8
8
  * Static class for benchmarking functions.
9
9
  *
10
10
  */
11
- export class OINOBenchmark {
11
+ export abstract class OINOBenchmark {
12
12
 
13
- private static _benchmarkCount:Record<string, number> = {}
14
- private static _benchmarkData:Record<string, number> = {}
15
- private static _benchmarkEnabled:Record<string, boolean> = {}
16
- private static _benchmarkStart:Record<string, number> = {}
13
+ protected static _instance:OINOBenchmark
14
+ protected static _enabled:Record<string, boolean> = {}
17
15
 
16
+ /**
17
+ * Create a new OINOBenchmark instance.
18
+ *
19
+ * @param enabledModules array of those benchmarks that are enabled
20
+ */
21
+ constructor(enabledModules:string[] = []) {
22
+ OINOBenchmark.setEnabled(enabledModules)
23
+ }
24
+
25
+ /**
26
+ * Set active benchmarking instance.
27
+ *
28
+ * @param instance OINOBenchmark instance
29
+ *
30
+ */
31
+ static setInstance(instance: OINOBenchmark) {
32
+ if (instance) {
33
+ OINOBenchmark._instance = instance
34
+ }
35
+ }
36
+
37
+
38
+ protected abstract _reset():void
18
39
  /**
19
40
  * Reset benchmark data (but not what is enabled).
20
41
  *
21
42
  */
22
- static reset() {
23
- this._benchmarkData = {}
24
- this._benchmarkCount = {}
43
+ static reset():void {
44
+ OINOBenchmark._instance?._reset()
25
45
  }
26
46
 
27
47
  /**
28
48
  * Set benchmark names that are enabled.
29
49
  *
30
- * @param module array of those benchmarks that are enabled
50
+ * @param modules array of those benchmarks that are enabled
31
51
  */
32
- static setEnabled(module:string[]):void {
33
- this._benchmarkEnabled = {}
34
- module.forEach(module_name => {
35
- this._benchmarkEnabled[module_name] = true
52
+ static setEnabled(modules:string[]):void {
53
+ OINOBenchmark._enabled = {}
54
+ modules.forEach(module_name => {
55
+ this._enabled[module_name] = true
36
56
  });
37
57
  }
38
58
 
59
+ protected abstract _start(module:string, method:string):void
39
60
  /**
40
61
  * Start benchmark timing.
41
62
  *
@@ -43,8 +64,72 @@ export class OINOBenchmark {
43
64
  * @param method of the benchmark
44
65
  */
45
66
  static start(module:string, method:string):void {
67
+ OINOBenchmark._instance?._start(module, method)
68
+ }
69
+
70
+ protected abstract _end(module:string, method:string, category?:string):number
71
+ /**
72
+ * Complete benchmark timing
73
+ *
74
+ * @param module of the benchmark
75
+ * @param method of the benchmark
76
+ * @param category optional subcategory of the benchmark
77
+ */
78
+ static end(module:string, method:string, category?:string):number {
79
+ return OINOBenchmark._instance?._end(module, method, category) || 0
80
+ }
81
+
82
+ protected abstract _get(module:string, method:string):number
83
+ /**
84
+ * Get given benchmark data.
85
+ *
86
+ * @param module of the benchmark
87
+ * @param method of the benchmark
88
+ *
89
+ */
90
+ static get(module:string, method:string):number {
91
+ return OINOBenchmark._instance?._get(module, method)
92
+ }
93
+
94
+ protected abstract _getAll():Record<string, number>
95
+ /**
96
+ * Get all benchmark data.
97
+ *
98
+ */
99
+ static getAll():Record<string, number> {
100
+ return OINOBenchmark._instance?._getAll()
101
+ }
102
+ }
103
+
104
+ /**
105
+ * OINOMemoryBenchmark is a memory-based benchmark implementation.
106
+ * It stores the benchmark data in memory and allows to reset, start, end and get benchmark data.
107
+ *
108
+ */
109
+ export class OINOMemoryBenchmark extends OINOBenchmark {
110
+
111
+ private _benchmarkCount:Record<string, number> = {}
112
+ private _benchmarkData:Record<string, number> = {}
113
+ private _benchmarkStart:Record<string, number> = {}
114
+
115
+ /**
116
+ * Reset benchmark data (but not what is enabled).
117
+ *
118
+ */
119
+ protected _reset():void {
120
+ this._benchmarkData = {}
121
+ this._benchmarkCount = {}
122
+ }
123
+
124
+ /**
125
+ * Start benchmark timing.
126
+ *
127
+ * @param module of the benchmark
128
+ * @param method of the benchmark
129
+ */
130
+ protected _start(module:string, method:string):void {
46
131
  const name:string = module + "." + method
47
- if (this._benchmarkEnabled[module]) {
132
+ if (OINOBenchmark._enabled[module]) {
48
133
  if (this._benchmarkCount[name] == undefined) {
49
134
  this._benchmarkCount[name] = 0
50
135
  this._benchmarkData[name] = 0
@@ -60,10 +145,10 @@ export class OINOBenchmark {
60
145
  * @param method of the benchmark
61
146
  * @param category optional subcategory of the benchmark
62
147
  */
63
- static end(module:string, method:string, category?:string):number {
148
+ protected _end(module:string, method:string, category?:string):number {
64
149
  const name:string = module + "." + method
65
150
  let result:number = 0
66
- if (this._benchmarkEnabled[module]) {
151
+ if (OINOBenchmark._enabled[module]) {
67
152
  const duration = performance.now() - this._benchmarkStart[name]
68
153
  this._benchmarkCount[name] += 1
69
154
  this._benchmarkData[name] += duration
@@ -88,9 +173,9 @@ export class OINOBenchmark {
88
173
  * @param method of the benchmark
89
174
  *
90
175
  */
91
- static get(module:string, method:string):number {
176
+ protected _get(module:string, method:string):number {
92
177
  const name:string = module + "." + method
93
- if (this._benchmarkEnabled[module]) {
178
+ if (OINOBenchmark._enabled[module] && (this._benchmarkCount[name] > 0)) {
94
179
  return this._benchmarkData[module] / this._benchmarkCount[module]
95
180
  }
96
181
  return -1
@@ -100,8 +185,8 @@ export class OINOBenchmark {
100
185
  * Get all benchmark data.
101
186
  *
102
187
  */
103
- static getAll():number {
104
- let result:any = {}
188
+ protected _getAll():Record<string, number> {
189
+ let result:Record<string, number> = {}
105
190
  for (const name in this._benchmarkData) {
106
191
  if (this._benchmarkCount[name] > 0) {
107
192
  result[name] = this._benchmarkData[name] / this._benchmarkCount[name]
@@ -110,7 +110,9 @@ export class OINOHtmlTemplate {
110
110
  *
111
111
  */
112
112
  render(removeUnusedTags:boolean = true):OINOHttpResult {
113
- return this._createHttpResult(this._renderHtml(), removeUnusedTags)
113
+ const html:string = this._renderHtml()
114
+ this.clearVariables() // clear variables after rendering
115
+ return this._createHttpResult(html, removeUnusedTags)
114
116
  }
115
117
 
116
118
  /**
package/src/OINOLog.ts CHANGED
@@ -82,15 +82,15 @@ export abstract class OINOLog {
82
82
  }
83
83
 
84
84
  /**
85
- * Set active logger and log level.
85
+ * Set active logger instance.
86
86
  *
87
- * @param logger logger instance
87
+ * @param instance OINOLog instance
88
88
  *
89
89
  */
90
- static setLogger(logger: OINOLog) {
90
+ static setInstance(instance: OINOLog) {
91
91
  // console.log("setLogger: " + log)
92
- if (logger) {
93
- OINOLog._instance = logger
92
+ if (instance) {
93
+ OINOLog._instance = instance
94
94
  }
95
95
  }
96
96
 
package/src/index.ts CHANGED
@@ -1,4 +1,4 @@
1
- export { OINOBenchmark } from "./OINOBenchmark.js"
1
+ export { OINOBenchmark, OINOMemoryBenchmark } from "./OINOBenchmark.js"
2
2
  export { OINOLog, OINOLogLevel, OINOConsoleLog } from "./OINOLog.js"
3
3
  export { OINOResult, OINOHttpResult } from "./OINOResult.js"
4
4
  export { OINOStr } from "./OINOStr.js"