forked from microsoft/ApplicationInsights-node.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNativePerformance.tests.ts
More file actions
147 lines (115 loc) · 7.33 KB
/
NativePerformance.tests.ts
File metadata and controls
147 lines (115 loc) · 7.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import assert = require("assert");
import sinon = require("sinon");
import AppInsights = require("../../applicationinsights");
import TelemetryClient = require("../../Library/TelemetryClient");
import { AutoCollectNativePerformance } from "../../AutoCollection/NativePerformance";
import { JsonConfig } from "../../Library/JsonConfig";
import * as Constants from "../../Declarations/Constants";
const ENV_nativeMetricsDisablers = "APPLICATION_INSIGHTS_DISABLE_EXTENDED_METRIC";
const ENV_nativeMetricsDisableAll = "APPLICATION_INSIGHTS_DISABLE_ALL_EXTENDED_METRICS";
describe("AutoCollection/NativePerformance", () => {
var sandbox: sinon.SinonSandbox;
beforeEach(() => {
sandbox = sinon.sandbox.create();
JsonConfig["_instance"] = undefined;
});
afterEach(() => {
AppInsights.dispose();
sandbox.restore();
});
describe("#init and #dispose()", () => {
it("init should enable and dispose should stop autocollection interval", () => {
var client = new TelemetryClient("1aa11111-bbbb-1ccc-8ddd-eeeeffff3333");
var setIntervalSpy = sandbox.spy(global, "setInterval");
var clearIntervalSpy = sandbox.spy(global, "clearInterval");
var native = new AutoCollectNativePerformance(client);
// Enable auto collection
native.enable(true);
if (AutoCollectNativePerformance["_metricsAvailable"]) {
assert.equal(setIntervalSpy.callCount, 1, "setInterval should be called when enabling");
} else {
assert.equal(setIntervalSpy.callCount, 0, "setInterval should not be called if native metrics package is not installed");
}
// Dispose should stop the interval
native.dispose();
if (AutoCollectNativePerformance["_metricsAvailable"]) {
assert.equal(clearIntervalSpy.callCount, 1, "clearInterval should be called when disposing");
} else {
assert.equal(clearIntervalSpy.callCount, 0, "clearInterval should not be called if native metrics package is not installed");
}
});
it("constructor should be safe to call multiple times", () => {
var client = new TelemetryClient("1aa11111-bbbb-1ccc-8ddd-eeeeffff3333");
var native = new AutoCollectNativePerformance(client);
var sinonSpy = sandbox.spy(AutoCollectNativePerformance.INSTANCE, "dispose");
assert.ok(native);
assert.ok(sinonSpy.notCalled);
assert.doesNotThrow(() => { native = new AutoCollectNativePerformance(client) }, "NativePerformance can be constructed more than once");
assert.ok(sinonSpy.calledOnce, "dispose is called when second instance is constructed");
});
it("Calling enable multiple times should not create multiple timers", () => {
var client = new TelemetryClient("1aa11111-bbbb-1ccc-8ddd-eeeeffff3333");
var sinonSpy = sandbox.spy(global, "setInterval");
var native = new AutoCollectNativePerformance(client);
assert.ok(native);
assert.doesNotThrow(() => native.enable(true), "Does not throw when trying to enable");
assert.doesNotThrow(() => native.enable(true), "Does not throw when trying to enable");
if (AutoCollectNativePerformance["_metricsAvailable"]) {
assert.equal(sinonSpy.callCount, 1, "setInterval should be singleton");
}
else{
assert.equal(sinonSpy.callCount, 0, "setInterval should not be called if native metrics package is not installed");
}
});
it("Calling enable when metrics are not available should fail gracefully", () => {
var client = new TelemetryClient("1aa11111-bbbb-1ccc-8ddd-eeeeffff3333");
var native = new AutoCollectNativePerformance(client);
AutoCollectNativePerformance["_metricsAvailable"] = false;
assert.ok(!(<any>native)["_emitter"]);
assert.doesNotThrow(() => native.enable(true), "Does not throw when native metrics are not available and trying to enable");
assert.doesNotThrow(() => native.enable(false), "Does not throw when native metrics are not available and trying to disable");
});
});
describe("#_parseEnabled", () => {
it("should return equal input arg if no env vars are set", () => {
const _customConfig = JsonConfig.getInstance();
assert.deepEqual(AutoCollectNativePerformance.parseEnabled(true, _customConfig), { isEnabled: true, disabledMetrics: {} });
assert.deepEqual(AutoCollectNativePerformance.parseEnabled(false, _customConfig), { isEnabled: false, disabledMetrics: {} });
const config = { gc: true, heap: true };
assert.deepEqual(AutoCollectNativePerformance.parseEnabled(config, _customConfig), { isEnabled: true, disabledMetrics: config });
});
it("should overwrite input arg if disable all extended metrics env var is set", () => {
const env = <{ [id: string]: string }>{};
const originalEnv = process.env;
env[ENV_nativeMetricsDisableAll] = "set";
process.env = env;
const _customConfig = JsonConfig.getInstance();
assert.deepEqual(AutoCollectNativePerformance.parseEnabled(true, _customConfig), { isEnabled: false, disabledMetrics: {} });
assert.deepEqual(AutoCollectNativePerformance.parseEnabled({}, _customConfig), { isEnabled: false, disabledMetrics: {} });
assert.deepEqual(AutoCollectNativePerformance.parseEnabled({ gc: true }, _customConfig), { isEnabled: false, disabledMetrics: {} });
process.env = originalEnv;
});
it("should overwrite input arg if individual env vars are set", () => {
const expectation = { gc: true, heap: true };
const env = <{ [id: string]: string }>{};
const originalEnv = process.env;
env[ENV_nativeMetricsDisablers] = "gc,heap";
process.env = env;
const _customConfig = JsonConfig.getInstance();
let inConfig;
inConfig = false;
assert.deepEqual(AutoCollectNativePerformance.parseEnabled(inConfig, _customConfig), { isEnabled: false, disabledMetrics: expectation });
inConfig = true;
assert.deepEqual(AutoCollectNativePerformance.parseEnabled(inConfig, _customConfig), { isEnabled: true, disabledMetrics: expectation });
inConfig = {};
assert.deepEqual(AutoCollectNativePerformance.parseEnabled(inConfig, _customConfig), { isEnabled: true, disabledMetrics: expectation });
inConfig = { gc: true };
assert.deepEqual(AutoCollectNativePerformance.parseEnabled(inConfig, _customConfig), { isEnabled: true, disabledMetrics: expectation });
inConfig = { loop: true };
assert.deepEqual(AutoCollectNativePerformance.parseEnabled(inConfig, _customConfig), { isEnabled: true, disabledMetrics: { ...inConfig, ...expectation } });
inConfig = { gc: false, loop: true, heap: 'abc', something: 'else' };
assert.deepEqual(AutoCollectNativePerformance.parseEnabled(<any>inConfig, _customConfig), { isEnabled: true, disabledMetrics: { ...inConfig, ...expectation } });
process.env = originalEnv;
});
});
});