1 | 'use strict'
|
2 |
|
3 | const test = require('tap').test
|
4 | const startpoint = require('startpoint')
|
5 | const Analysis = require('../analysis/index.js')
|
6 | const generateProcessStat = require('./generate-process-stat.js')
|
7 | const generateTraceEvent = require('./generate-trace-event.js')
|
8 |
|
9 | function getAnalysis (processStatData, traceEventData) {
|
10 | const processStatReader = startpoint(processStatData, { objectMode: true })
|
11 | const traceEventReader = startpoint(traceEventData, { objectMode: true })
|
12 |
|
13 | const analysisResult = new Analysis(traceEventReader, processStatReader)
|
14 |
|
15 |
|
16 | return new Promise(function (resolve, reject) {
|
17 | const initRead = analysisResult.read()
|
18 | if (initRead !== null) return resolve(initRead)
|
19 |
|
20 | analysisResult.once('readable', function () {
|
21 | const safeRead = analysisResult.read()
|
22 | resolve(safeRead)
|
23 | })
|
24 |
|
25 | analysisResult.once('error', function (error) {
|
26 | reject(error)
|
27 | })
|
28 | })
|
29 | }
|
30 |
|
31 | test('Analysis - pipeline - too little data error', async function (t) {
|
32 | try {
|
33 | await getAnalysis([], [])
|
34 | } catch (e) {
|
35 | t.ok(/not enough data/i.test(e.message))
|
36 | t.end()
|
37 | }
|
38 | })
|
39 |
|
40 | test('Analysis - pipeline - error', async function (t) {
|
41 | const error = new Error('expected error')
|
42 | try {
|
43 | await getAnalysis([], error)
|
44 | } catch (e) {
|
45 | t.strictDeepEqual(e, error)
|
46 | t.end()
|
47 | }
|
48 | })
|
49 |
|
50 | test('Analysis - pipeline - normal interval', async function (t) {
|
51 | for (const noise of [0, 0.1, 0.3]) {
|
52 | const goodCPU = generateProcessStat({
|
53 | handles: [3, 3, 3, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 3, 3, 3],
|
54 | cpu: [1, 1, 1, 100, 100, 120, 90, 110, 100, 80, 110, 90, 110, 1, 1, 1]
|
55 | }, noise)
|
56 | const goodMemoryGC = generateTraceEvent([
|
57 | 'NONE', 'SCA', 'NONE', 'SCA', 'NONE', 'SCA', 'NONE', 'SCA', 'NONE',
|
58 | 'SCA', 'NONE', 'SCA', 'NONE', 'NONE', 'NONE', 'NONE'
|
59 | ])
|
60 | t.strictDeepEqual(await getAnalysis(goodCPU, goodMemoryGC), {
|
61 | interval: [ 30, 120 ],
|
62 | issues: {
|
63 | delay: false,
|
64 | cpu: false,
|
65 | memory: {
|
66 | external: false,
|
67 | rss: false,
|
68 | heapTotal: false,
|
69 | heapUsed: false
|
70 | },
|
71 | handles: false
|
72 | },
|
73 | issueCategory: 'none'
|
74 | })
|
75 |
|
76 | const badCPU = generateProcessStat({
|
77 | handles: [3, 3, 3, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 3, 3, 3],
|
78 | cpu: [1, 1, 1, 50, 40, 10, 10, 100, 50, 40, 10, 10, 10, 1, 1, 1]
|
79 | }, noise)
|
80 | t.strictDeepEqual(await getAnalysis(badCPU, []), {
|
81 | interval: [ 30, 120 ],
|
82 | issues: {
|
83 | delay: false,
|
84 | cpu: true,
|
85 | memory: {
|
86 | external: false,
|
87 | rss: false,
|
88 | heapTotal: false,
|
89 | heapUsed: false
|
90 | },
|
91 | handles: false
|
92 | },
|
93 | issueCategory: 'io'
|
94 | })
|
95 | }
|
96 |
|
97 | t.end()
|
98 | })
|
99 |
|
100 | test('Analysis - pipeline - full interval', async function (t) {
|
101 | const goodCPU = generateProcessStat({
|
102 | handles: [3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
|
103 | cpu: [100, 100, 120, 90, 110, 100, 80, 110, 90, 110]
|
104 | }, 0)
|
105 | t.strictDeepEqual(await getAnalysis(goodCPU, []), {
|
106 | interval: [ 0, 90 ],
|
107 | issues: {
|
108 | delay: false,
|
109 | cpu: false,
|
110 | memory: {
|
111 | external: false,
|
112 | rss: false,
|
113 | heapTotal: false,
|
114 | heapUsed: false
|
115 | },
|
116 | handles: false
|
117 | },
|
118 | issueCategory: 'none'
|
119 | })
|
120 |
|
121 | const badCPU = generateProcessStat({
|
122 | handles: [3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
|
123 | cpu: [50, 40, 10, 10, 100, 50, 40, 10, 10, 10]
|
124 | }, 0)
|
125 |
|
126 | t.strictDeepEqual(await getAnalysis(badCPU, []), {
|
127 | interval: [ 0, 90 ],
|
128 | issues: {
|
129 | delay: false,
|
130 | cpu: true,
|
131 | memory: {
|
132 | external: false,
|
133 | rss: false,
|
134 | heapTotal: false,
|
135 | heapUsed: false
|
136 | },
|
137 | handles: false
|
138 | },
|
139 | issueCategory: 'io'
|
140 | })
|
141 |
|
142 | t.end()
|
143 | })
|