@oino-ts/common 0.1.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.
- package/README.md +190 -0
- package/dist/cjs/OINOBenchmark.js +108 -0
- package/dist/cjs/OINOHtmlTemplate.js +177 -0
- package/dist/cjs/OINOLog.js +151 -0
- package/dist/cjs/OINOParser.js +466 -0
- package/dist/cjs/OINOResult.js +216 -0
- package/dist/cjs/OINOStr.js +265 -0
- package/dist/cjs/index.js +40 -0
- package/dist/esm/OINOBenchmark.js +104 -0
- package/dist/esm/OINOHtmlTemplate.js +173 -0
- package/dist/esm/OINOLog.js +146 -0
- package/dist/esm/OINOParser.js +462 -0
- package/dist/esm/OINOResult.js +211 -0
- package/dist/esm/OINOStr.js +261 -0
- package/dist/esm/index.js +29 -0
- package/dist/types/OINOBenchmark.d.ts +49 -0
- package/dist/types/OINOHtmlTemplate.d.ts +88 -0
- package/dist/types/OINOLog.d.ts +105 -0
- package/dist/types/OINOParser.d.ts +53 -0
- package/dist/types/OINOResult.d.ts +110 -0
- package/dist/types/OINOStr.d.ts +108 -0
- package/dist/types/index.d.ts +29 -0
- package/package.json +30 -0
- package/src/OINOBenchmark.ts +112 -0
- package/src/OINOHtmlTemplate.ts +186 -0
- package/src/OINOLog.ts +168 -0
- package/src/OINOResult.ts +234 -0
- package/src/OINOStr.ts +254 -0
- package/src/index.ts +31 -0
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
/** Logging levels */
|
|
2
|
+
export declare enum OINOLogLevel {
|
|
3
|
+
/** Debug messages */
|
|
4
|
+
debug = 0,
|
|
5
|
+
/** Informational messages */
|
|
6
|
+
info = 1,
|
|
7
|
+
/** Warning messages */
|
|
8
|
+
warn = 2,
|
|
9
|
+
/** Error messages */
|
|
10
|
+
error = 3
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Abstract base class for logging implementations supporting
|
|
14
|
+
* - error, warning, info and debug channels
|
|
15
|
+
* - setting level of logs outputted
|
|
16
|
+
*
|
|
17
|
+
*/
|
|
18
|
+
export declare abstract class OINOLog {
|
|
19
|
+
protected static _instance: OINOLog;
|
|
20
|
+
protected _logLevel: OINOLogLevel;
|
|
21
|
+
/**
|
|
22
|
+
* Abstract logging method to implement the actual logging operation.
|
|
23
|
+
*
|
|
24
|
+
* @param logLevel level of the log events
|
|
25
|
+
*
|
|
26
|
+
*/
|
|
27
|
+
constructor(logLevel?: OINOLogLevel);
|
|
28
|
+
/**
|
|
29
|
+
* Abstract logging method to implement the actual logging operation.
|
|
30
|
+
*
|
|
31
|
+
* @param levelStr level string of the log event
|
|
32
|
+
* @param message message of the log event
|
|
33
|
+
* @param data structured data associated with the log event
|
|
34
|
+
*
|
|
35
|
+
*/
|
|
36
|
+
protected abstract _writeLog(levelStr: string, message: string, data?: any): void;
|
|
37
|
+
/**
|
|
38
|
+
* Abstract logging method to implement the actual logging operation.
|
|
39
|
+
*
|
|
40
|
+
* @param level level of the log event
|
|
41
|
+
* @param levelStr level string of the log event
|
|
42
|
+
* @param message message of the log event
|
|
43
|
+
* @param data structured data associated with the log event
|
|
44
|
+
*
|
|
45
|
+
*/
|
|
46
|
+
protected static _log(level: OINOLogLevel, levelStr: string, message: string, data?: any): void;
|
|
47
|
+
/**
|
|
48
|
+
* Set active logger and log level.
|
|
49
|
+
*
|
|
50
|
+
* @param logger logger instance
|
|
51
|
+
*
|
|
52
|
+
*/
|
|
53
|
+
static setLogger(logger: OINOLog): void;
|
|
54
|
+
/**
|
|
55
|
+
* Set log level.
|
|
56
|
+
*
|
|
57
|
+
* @param logLevel log level to use
|
|
58
|
+
*
|
|
59
|
+
*/
|
|
60
|
+
static setLogLevel(logLevel: OINOLogLevel): void;
|
|
61
|
+
/**
|
|
62
|
+
* Log error event.
|
|
63
|
+
*
|
|
64
|
+
* @param message message of the log event
|
|
65
|
+
* @param data structured data associated with the log event
|
|
66
|
+
*
|
|
67
|
+
*/
|
|
68
|
+
static error(message: string, data?: any): void;
|
|
69
|
+
/**
|
|
70
|
+
* Log warning event.
|
|
71
|
+
*
|
|
72
|
+
* @param message message of the log event
|
|
73
|
+
* @param data structured data associated with the log event
|
|
74
|
+
*
|
|
75
|
+
*/
|
|
76
|
+
static warning(message: string, data?: any): void;
|
|
77
|
+
/**
|
|
78
|
+
* Log info event.
|
|
79
|
+
*
|
|
80
|
+
* @param message message of the log event
|
|
81
|
+
* @param data structured data associated with the log event
|
|
82
|
+
*
|
|
83
|
+
*/
|
|
84
|
+
static info(message: string, data?: any): void;
|
|
85
|
+
/**
|
|
86
|
+
* Log debug event.
|
|
87
|
+
*
|
|
88
|
+
* @param message message of the log event
|
|
89
|
+
* @param data structured data associated with the log event
|
|
90
|
+
*
|
|
91
|
+
*/
|
|
92
|
+
static debug(message: string, data?: any): void;
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Logging implementation based on console.log.
|
|
96
|
+
*
|
|
97
|
+
*/
|
|
98
|
+
export declare class OINOConsoleLog extends OINOLog {
|
|
99
|
+
/**
|
|
100
|
+
* Constructor of `OINOConsoleLog`
|
|
101
|
+
* @param logLevel logging level
|
|
102
|
+
*/
|
|
103
|
+
constructor(logLevel?: OINOLogLevel);
|
|
104
|
+
protected _writeLog(level: string, message: string, data?: any): void;
|
|
105
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { OINODbDataModel, OINODataRow, OINODbApiRequestParams } from "../../db/src/index.js";
|
|
2
|
+
/**
|
|
3
|
+
* Static factory class for easily creating things based on data
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
6
|
+
export declare class OINOParser {
|
|
7
|
+
/**
|
|
8
|
+
* Create data rows from request body based on the datamodel.
|
|
9
|
+
*
|
|
10
|
+
* @param datamodel datamodel of the api
|
|
11
|
+
* @param data data as a string
|
|
12
|
+
* @param requestParams parameters
|
|
13
|
+
*
|
|
14
|
+
*/
|
|
15
|
+
static createRows(datamodel: OINODbDataModel, data: string | Buffer | object, requestParams: OINODbApiRequestParams): OINODataRow[];
|
|
16
|
+
/**
|
|
17
|
+
* Create data rows from request body based on the datamodel.
|
|
18
|
+
*
|
|
19
|
+
* @param datamodel datamodel of the api
|
|
20
|
+
* @param data data as a string
|
|
21
|
+
* @param requestParams parameters
|
|
22
|
+
*
|
|
23
|
+
*/
|
|
24
|
+
static createRowsFromText(datamodel: OINODbDataModel, data: string, requestParams: OINODbApiRequestParams): OINODataRow[];
|
|
25
|
+
/**
|
|
26
|
+
* Create data rows from request body based on the datamodel.
|
|
27
|
+
*
|
|
28
|
+
* @param datamodel datamodel of the api
|
|
29
|
+
* @param data data as an Buffer
|
|
30
|
+
* @param requestParams parameters
|
|
31
|
+
*
|
|
32
|
+
*/
|
|
33
|
+
static createRowsFromBlob(datamodel: OINODbDataModel, data: Buffer, requestParams: OINODbApiRequestParams): OINODataRow[];
|
|
34
|
+
/**
|
|
35
|
+
* Create one data row from javascript object based on the datamodel.
|
|
36
|
+
* NOTE! Data assumed to be unserialized i.e. of the native type (string, number, boolean, Buffer)
|
|
37
|
+
*
|
|
38
|
+
* @param datamodel datamodel of the api
|
|
39
|
+
* @param data data as javascript object
|
|
40
|
+
*
|
|
41
|
+
*/
|
|
42
|
+
static createRowFromObject(datamodel: OINODbDataModel, data: any): OINODataRow;
|
|
43
|
+
private static _findCsvLineEnd;
|
|
44
|
+
private static _parseCsvLine;
|
|
45
|
+
private static _createRowFromCsv;
|
|
46
|
+
private static _createRowFromJsonObj;
|
|
47
|
+
private static _createRowFromJson;
|
|
48
|
+
private static _findMultipartBoundary;
|
|
49
|
+
private static _parseMultipartLine;
|
|
50
|
+
private static _multipartHeaderRegex;
|
|
51
|
+
private static _createRowFromFormdata;
|
|
52
|
+
private static _createRowFromUrlencoded;
|
|
53
|
+
}
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* OINO API request result object with returned data and/or http status code/message and
|
|
3
|
+
* error / warning messages.
|
|
4
|
+
*
|
|
5
|
+
*/
|
|
6
|
+
export declare class OINOResult {
|
|
7
|
+
/** Wheter request was successfully executed */
|
|
8
|
+
success: boolean;
|
|
9
|
+
/** HTTP status code */
|
|
10
|
+
statusCode: number;
|
|
11
|
+
/** HTTP status message */
|
|
12
|
+
statusMessage: string;
|
|
13
|
+
/** Error / warning messages */
|
|
14
|
+
messages: string[];
|
|
15
|
+
/**
|
|
16
|
+
* Constructor of OINOResult.
|
|
17
|
+
*
|
|
18
|
+
*/
|
|
19
|
+
constructor();
|
|
20
|
+
/**
|
|
21
|
+
* Copy values from different result.
|
|
22
|
+
*
|
|
23
|
+
* @param result source value
|
|
24
|
+
*/
|
|
25
|
+
copy(result: OINOResult): void;
|
|
26
|
+
/**
|
|
27
|
+
* Set HTTP OK status (does not reset messages).
|
|
28
|
+
*
|
|
29
|
+
*/
|
|
30
|
+
setOk(): void;
|
|
31
|
+
/**
|
|
32
|
+
* Set HTTP error status using given code and message. Returns self reference for chaining.
|
|
33
|
+
*
|
|
34
|
+
* @param statusCode HTTP status code
|
|
35
|
+
* @param statusMessage HTTP status message
|
|
36
|
+
* @param operation operation where error occured
|
|
37
|
+
*
|
|
38
|
+
*/
|
|
39
|
+
setError(statusCode: number, statusMessage: string, operation: string): OINOResult;
|
|
40
|
+
/**
|
|
41
|
+
* Add warning message. Returns self reference for chaining.
|
|
42
|
+
*
|
|
43
|
+
* @param message HTTP status message
|
|
44
|
+
* @param operation operation where warning occured
|
|
45
|
+
*
|
|
46
|
+
*/
|
|
47
|
+
addWarning(message: string, operation: string): OINOResult;
|
|
48
|
+
/**
|
|
49
|
+
* Add info message. Returns self reference for chaining.
|
|
50
|
+
*
|
|
51
|
+
* @param message HTTP status message
|
|
52
|
+
* @param operation operation where info occured
|
|
53
|
+
*
|
|
54
|
+
*/
|
|
55
|
+
addInfo(message: string, operation: string): OINOResult;
|
|
56
|
+
/**
|
|
57
|
+
* Add debug message. Returns self reference for chaining.
|
|
58
|
+
*
|
|
59
|
+
* @param message HTTP status message
|
|
60
|
+
* @param operation operation where debug occured
|
|
61
|
+
*
|
|
62
|
+
*/
|
|
63
|
+
addDebug(message: string, operation: string): OINOResult;
|
|
64
|
+
/**
|
|
65
|
+
* Copy given messages to HTTP headers.
|
|
66
|
+
*
|
|
67
|
+
* @param headers HTTP headers
|
|
68
|
+
* @param copyErrors wether error messages should be copied (default true)
|
|
69
|
+
* @param copyWarnings wether warning messages should be copied (default false)
|
|
70
|
+
* @param copyInfos wether info messages should be copied (default false)
|
|
71
|
+
* @param copyDebug wether debug messages should be copied (default false)
|
|
72
|
+
*
|
|
73
|
+
*/
|
|
74
|
+
copyMessagesToHeaders(headers: Headers, copyErrors?: boolean, copyWarnings?: boolean, copyInfos?: boolean, copyDebug?: boolean): void;
|
|
75
|
+
/**
|
|
76
|
+
* Print result for logging.
|
|
77
|
+
*
|
|
78
|
+
*/
|
|
79
|
+
printLog(): string;
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Specialized result for HTTP responses.
|
|
83
|
+
*/
|
|
84
|
+
export declare class OINOHttpResult extends OINOResult {
|
|
85
|
+
private _etag;
|
|
86
|
+
/** HTTP body data */
|
|
87
|
+
readonly body: string;
|
|
88
|
+
/** HTTP cache expiration value */
|
|
89
|
+
expires: number;
|
|
90
|
+
/** HTTP cache last-modified value */
|
|
91
|
+
lastModified: number;
|
|
92
|
+
/**
|
|
93
|
+
* Constructor for a `OINOHttpResult`
|
|
94
|
+
*
|
|
95
|
+
* @param body HTTP body
|
|
96
|
+
*
|
|
97
|
+
*/
|
|
98
|
+
constructor(body: string);
|
|
99
|
+
/**
|
|
100
|
+
* Get the ETag value for the body opportunistically, i.e. don't calculate until requested and reuse value.
|
|
101
|
+
*
|
|
102
|
+
*/
|
|
103
|
+
getEtag(): string;
|
|
104
|
+
/**
|
|
105
|
+
* Get a Response object from the result values.
|
|
106
|
+
*
|
|
107
|
+
* @param headers HTTP headers (overrides existing values)
|
|
108
|
+
*/
|
|
109
|
+
getResponse(headers?: Record<string, string>): Response;
|
|
110
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import { OINOContentType } from ".";
|
|
2
|
+
/** Callback to filter data fields */
|
|
3
|
+
export type OINOStrEncoder = (str: string) => string;
|
|
4
|
+
/**
|
|
5
|
+
* Static class string utilities.
|
|
6
|
+
*
|
|
7
|
+
*/
|
|
8
|
+
export declare class OINOStr {
|
|
9
|
+
/**
|
|
10
|
+
* Split string by the top level of the given type of brackets.
|
|
11
|
+
* E.g. splitByBrackets("a(bc(d))ef(gh)kl", true, true, '(', ')') would return ["a", "bc(d)", "ef", "gh", "kl"]
|
|
12
|
+
*
|
|
13
|
+
* @param str string to split
|
|
14
|
+
* @param includePartsBetweenBlocks whether to include strings between top level brackets
|
|
15
|
+
* @param includeTrailingUnescapedBlock whether to include final block that is missing necessary end brackets
|
|
16
|
+
* @param startBracket starting bracket, e.g. '('
|
|
17
|
+
* @param endBracket ending bracket, e.g. ')'
|
|
18
|
+
*
|
|
19
|
+
*/
|
|
20
|
+
static splitByBrackets(str: string, includePartsBetweenBlocks: boolean, includeTrailingUnescapedBlock: boolean, startBracket: string, endBracket: string): string[];
|
|
21
|
+
/**
|
|
22
|
+
* Split string by delimeter excluding delimeters inside given brackets.
|
|
23
|
+
* E.g. splitExcludingBrackets("a,(bc,d),ef,(g,h),k", ',', '(', ')') would return ["a", "bc,d", "ef", "g,h", "k"]
|
|
24
|
+
*
|
|
25
|
+
* @param str string to split
|
|
26
|
+
* @param delimeter string to use as delimeter
|
|
27
|
+
* @param startBracket starting bracket, e.g. '('
|
|
28
|
+
* @param endBracket ending bracket, e.g. ')'
|
|
29
|
+
*/
|
|
30
|
+
static splitExcludingBrackets(str: string, delimeter: string, startBracket: string, endBracket: string): string[];
|
|
31
|
+
/**
|
|
32
|
+
* Encode OINO serialized strings as valid JSON.
|
|
33
|
+
*
|
|
34
|
+
* @param str string to encode
|
|
35
|
+
* @param valueType wether it is a value type
|
|
36
|
+
*/
|
|
37
|
+
static encodeJSON(str: string | null | undefined, valueType?: boolean): string;
|
|
38
|
+
/**
|
|
39
|
+
* Decode JSON string as OINO serialization.
|
|
40
|
+
*
|
|
41
|
+
* @param str string to decode
|
|
42
|
+
*/
|
|
43
|
+
static decodeJSON(str: string): string;
|
|
44
|
+
/**
|
|
45
|
+
* Encode OINO serialized strings as valid CSV.
|
|
46
|
+
*
|
|
47
|
+
* @param str string to encode
|
|
48
|
+
*/
|
|
49
|
+
static encodeCSV(str: string | null | undefined): string;
|
|
50
|
+
/**
|
|
51
|
+
* Decode CSV string as OINO serialization.
|
|
52
|
+
*
|
|
53
|
+
* @param str string to decode
|
|
54
|
+
*/
|
|
55
|
+
static decodeCSV(str: string): string;
|
|
56
|
+
/**
|
|
57
|
+
* Encode OINO serialized strings as valid Formdata.
|
|
58
|
+
*
|
|
59
|
+
* @param str string to encode
|
|
60
|
+
*/
|
|
61
|
+
static encodeFormdata(str: string | null | undefined): string;
|
|
62
|
+
/**
|
|
63
|
+
* Decode Formdata string as OINO serialization.
|
|
64
|
+
*
|
|
65
|
+
* @param str string to decode
|
|
66
|
+
*/
|
|
67
|
+
static decodeFormdata(str: string): string;
|
|
68
|
+
/**
|
|
69
|
+
* Encode OINO serialized strings as valid Urlencode.
|
|
70
|
+
*
|
|
71
|
+
* @param str string to encode
|
|
72
|
+
*/
|
|
73
|
+
static encodeUrlencode(str: string | null | undefined): string;
|
|
74
|
+
/**
|
|
75
|
+
* Decode Urlencode string as OINO serialization.
|
|
76
|
+
*
|
|
77
|
+
* @param str string to decode
|
|
78
|
+
*/
|
|
79
|
+
static decodeUrlencode(str: string): string;
|
|
80
|
+
/**
|
|
81
|
+
* Encode OINO serialized strings as valid HTML content.
|
|
82
|
+
*
|
|
83
|
+
* @param str string to encode
|
|
84
|
+
*/
|
|
85
|
+
static encodeHtml(str: string | null | undefined): string;
|
|
86
|
+
/**
|
|
87
|
+
* Decode HTML string as OINO serialization.
|
|
88
|
+
*
|
|
89
|
+
* @param str string to encode
|
|
90
|
+
*/
|
|
91
|
+
static decodeHtml(str: string): string;
|
|
92
|
+
/**
|
|
93
|
+
* Decode content type formatted string as OINO serialization.
|
|
94
|
+
*
|
|
95
|
+
* @param str string to decode
|
|
96
|
+
* @param contentType content type for serialization
|
|
97
|
+
*
|
|
98
|
+
*/
|
|
99
|
+
static decode(str: string, contentType: OINOContentType): string;
|
|
100
|
+
/**
|
|
101
|
+
* Encode OINO serialized string to the content type formatting.
|
|
102
|
+
*
|
|
103
|
+
* @param str string to encode
|
|
104
|
+
* @param contentType content type for serialization
|
|
105
|
+
*
|
|
106
|
+
*/
|
|
107
|
+
static encode(str: string | null | undefined, contentType: OINOContentType): string;
|
|
108
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export { OINOBenchmark } from "./OINOBenchmark.js";
|
|
2
|
+
export { OINOLog, OINOLogLevel, OINOConsoleLog } from "./OINOLog.js";
|
|
3
|
+
export { OINOResult, OINOHttpResult } from "./OINOResult.js";
|
|
4
|
+
export { OINOStr } from "./OINOStr.js";
|
|
5
|
+
export { OINOHtmlTemplate } from "./OINOHtmlTemplate.js";
|
|
6
|
+
export { OINOParser } from "./OINOParser";
|
|
7
|
+
/** OINO error message prefix */
|
|
8
|
+
export declare const OINO_ERROR_PREFIX = "OINO ERROR";
|
|
9
|
+
/** OINO warning message prefix */
|
|
10
|
+
export declare const OINO_WARNING_PREFIX = "OINO WARNING";
|
|
11
|
+
/** OINO info message prefix */
|
|
12
|
+
export declare const OINO_INFO_PREFIX = "OINO INFO";
|
|
13
|
+
/** OINO debug message prefix */
|
|
14
|
+
export declare const OINO_DEBUG_PREFIX = "OINO DEBUG";
|
|
15
|
+
/**
|
|
16
|
+
* Supported content format mime-types
|
|
17
|
+
*/
|
|
18
|
+
export declare enum OINOContentType {
|
|
19
|
+
/** JSON encoded data */
|
|
20
|
+
json = "application/json",
|
|
21
|
+
/** CSV encoded data */
|
|
22
|
+
csv = "text/csv",
|
|
23
|
+
/** Multipart encoded form data */
|
|
24
|
+
formdata = "multipart/form-data",
|
|
25
|
+
/** URL encoded form data */
|
|
26
|
+
urlencode = "application/x-www-form-urlencoded",
|
|
27
|
+
/** HTML encoded data (output only) */
|
|
28
|
+
html = "text/html"
|
|
29
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@oino-ts/common",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "OINO TS package for common classes.",
|
|
5
|
+
"author": "Matias Kiviniemi (pragmatta)",
|
|
6
|
+
"license": "MPL-2.0",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/pragmatta/oino-ts.git"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"types",
|
|
13
|
+
"typescript",
|
|
14
|
+
"library"
|
|
15
|
+
],
|
|
16
|
+
"main": "./dist/cjs/index.js",
|
|
17
|
+
"module": "./dist/esm/index.js",
|
|
18
|
+
"types": "./dist/types/index.d.ts",
|
|
19
|
+
"dependencies": {
|
|
20
|
+
},
|
|
21
|
+
"devDependencies": {
|
|
22
|
+
"@oino-ts/types": "0.1.0"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"src/*.ts",
|
|
26
|
+
"dist/cjs/*.js",
|
|
27
|
+
"dist/esm/*.js",
|
|
28
|
+
"dist/types/*.d.ts"
|
|
29
|
+
]
|
|
30
|
+
}
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
3
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
4
|
+
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Static class for benchmarking functions.
|
|
9
|
+
*
|
|
10
|
+
*/
|
|
11
|
+
export class OINOBenchmark {
|
|
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> = {}
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* Reset benchmark data (but not what is enabled).
|
|
20
|
+
*
|
|
21
|
+
*/
|
|
22
|
+
static reset() {
|
|
23
|
+
this._benchmarkData = {}
|
|
24
|
+
this._benchmarkCount = {}
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/**
|
|
28
|
+
* Set benchmark names that are enabled.
|
|
29
|
+
*
|
|
30
|
+
* @param module array of those benchmarks that are enabled
|
|
31
|
+
*/
|
|
32
|
+
static setEnabled(module:string[]):void {
|
|
33
|
+
this._benchmarkEnabled = {}
|
|
34
|
+
module.forEach(module_name => {
|
|
35
|
+
this._benchmarkEnabled[module_name] = true
|
|
36
|
+
});
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Start benchmark timing.
|
|
41
|
+
*
|
|
42
|
+
* @param module of the benchmark
|
|
43
|
+
* @param method of the benchmark
|
|
44
|
+
*/
|
|
45
|
+
static start(module:string, method:string):void {
|
|
46
|
+
const name:string = module + "." + method
|
|
47
|
+
if (this._benchmarkEnabled[module]) {
|
|
48
|
+
if (this._benchmarkCount[name] == undefined) {
|
|
49
|
+
this._benchmarkCount[name] = 0
|
|
50
|
+
this._benchmarkData[name] = 0
|
|
51
|
+
}
|
|
52
|
+
this._benchmarkStart[name] = performance.now()
|
|
53
|
+
}
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Complete benchmark timing
|
|
58
|
+
*
|
|
59
|
+
* @param module of the benchmark
|
|
60
|
+
* @param method of the benchmark
|
|
61
|
+
* @param category optional subcategory of the benchmark
|
|
62
|
+
*/
|
|
63
|
+
static end(module:string, method:string, category?:string):number {
|
|
64
|
+
const name:string = module + "." + method
|
|
65
|
+
let result:number = 0
|
|
66
|
+
if (this._benchmarkEnabled[module]) {
|
|
67
|
+
const duration = performance.now() - this._benchmarkStart[name]
|
|
68
|
+
this._benchmarkCount[name] += 1
|
|
69
|
+
this._benchmarkData[name] += duration
|
|
70
|
+
if (category) {
|
|
71
|
+
const category_name = name + "." + category
|
|
72
|
+
if (this._benchmarkCount[category_name] == undefined) {
|
|
73
|
+
this._benchmarkCount[category_name] = 0
|
|
74
|
+
this._benchmarkData[category_name] = 0
|
|
75
|
+
}
|
|
76
|
+
this._benchmarkCount[category_name] += 1
|
|
77
|
+
this._benchmarkData[category_name] += duration
|
|
78
|
+
}
|
|
79
|
+
result = this._benchmarkData[name] / this._benchmarkCount[name]
|
|
80
|
+
}
|
|
81
|
+
return result
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/**
|
|
85
|
+
* Get given benchmark data.
|
|
86
|
+
*
|
|
87
|
+
* @param module of the benchmark
|
|
88
|
+
* @param method of the benchmark
|
|
89
|
+
*
|
|
90
|
+
*/
|
|
91
|
+
static get(module:string, method:string):number {
|
|
92
|
+
const name:string = module + "." + method
|
|
93
|
+
if (this._benchmarkEnabled[module]) {
|
|
94
|
+
return this._benchmarkData[module] / this._benchmarkCount[module]
|
|
95
|
+
}
|
|
96
|
+
return -1
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
/**
|
|
100
|
+
* Get all benchmark data.
|
|
101
|
+
*
|
|
102
|
+
*/
|
|
103
|
+
static getAll():number {
|
|
104
|
+
let result:any = {}
|
|
105
|
+
for (const name in this._benchmarkData) {
|
|
106
|
+
if (this._benchmarkCount[name] > 0) {
|
|
107
|
+
result[name] = this._benchmarkData[name] / this._benchmarkCount[name]
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
return result
|
|
111
|
+
}
|
|
112
|
+
}
|