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