-
Notifications
You must be signed in to change notification settings - Fork 50
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Get analysis result out of tool.analyze()
#273
Open
goto-bus-stop
wants to merge
13
commits into
main
Choose a base branch
from
check
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
1183f0b
Expose recommendations and `check()` API
goto-bus-stop 2aa5277
Merge branch 'master' into check
goto-bus-stop 14b86e8
return recommendation from visualize also
goto-bus-stop 731d913
add barebones test
goto-bus-stop 89e4f92
Rename to analyze()
goto-bus-stop 212349c
Remove `result.recommendation`, clinic cli can do this manually
goto-bus-stop 001f54d
update docs to doctor.analyze()
goto-bus-stop 9acffb3
Merge branch 'master' into check
goto-bus-stop f571be6
lint fix
goto-bus-stop be24a71
update tests for analyze()
goto-bus-stop 35c19b7
Return analysis object with streams from `analyze()`
goto-bus-stop c4a7ae3
generate a whole bunch of cpu data
goto-bus-stop cfafe53
Merge branch 'master' into check
goto-bus-stop File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
'use strict' | ||
/* eslint-disable */ | ||
|
||
const fs = require('fs') | ||
const v8 = require('v8') | ||
const { test } = require('tap') | ||
const pump = require('pump') | ||
const async = require('async') | ||
const rimraf = require('rimraf') | ||
const mkdirp = require('mkdirp') | ||
const semver = require('semver') | ||
const startpoint = require('startpoint') | ||
const ClinicDoctor = require('../index.js') | ||
const getLoggingPaths = require('@nearform/clinic-common').getLoggingPaths('doctor') | ||
const generateProcessStat = require('./generate-process-stat.js') | ||
const generateTraceEvent = require('./generate-trace-event.js') | ||
|
||
test('cmd - test analyze - data exists', function (t) { | ||
const tool = new ClinicDoctor({ dest: './foo' }) | ||
|
||
function cleanup (err, dirname) { | ||
t.ifError(err) | ||
|
||
t.match(dirname, /^foo(\/|\\)[0-9]+\.clinic-doctor$/) | ||
|
||
rimraf(dirname, function (err) { | ||
t.ifError(err) | ||
t.end() | ||
}) | ||
} | ||
|
||
const systemInfo = { | ||
clock: { | ||
hrtime: process.hrtime(), | ||
unixtime: Date.now() | ||
}, | ||
nodeVersions: process.versions | ||
} | ||
|
||
const badCPU = generateProcessStat({ | ||
cpu: [ | ||
// duplicate a bunch of times so the interval trimming code | ||
// doesn't discard everything | ||
200, 200, 15, 10, 190, 200, 5, 15, 190, 200, | ||
200, 200, 15, 10, 190, 200, 5, 15, 190, 200, | ||
200, 200, 15, 10, 190, 200, 5, 15, 190, 200, | ||
200, 200, 15, 10, 190, 200, 5, 15, 190, 200, | ||
200, 200, 15, 10, 190, 200, 5, 15, 190, 200, | ||
200, 200, 15, 10, 190, 200, 5, 15, 190, 200, | ||
200, 200, 15, 10, 190, 200, 5, 15, 190, 200 | ||
] | ||
}, 0) | ||
|
||
const goodMemoryGC = generateTraceEvent( | ||
'-S....-S....-S....-S....-S....-S....-S....-S....-S....-S....' + | ||
'-M....-M....-S....-M....-M....-S....-M....-M....-FFF.. CCC..' | ||
) | ||
|
||
const paths = getLoggingPaths({ | ||
identifier: 1234, | ||
path: tool.path | ||
}) | ||
|
||
mkdirp(paths['/'], ondir) | ||
|
||
function ondir (err) { | ||
if (err) return cleanup(err, paths['/']) | ||
const ProcessStatEncoder = require('../format/process-stat-encoder.js') | ||
|
||
async.parallel({ | ||
systeminfo (callback) { | ||
fs.writeFile(paths['/systeminfo'], JSON.stringify(systemInfo), callback) | ||
}, | ||
processstat (callback) { | ||
pump( | ||
startpoint(badCPU, { objectMode: true }), | ||
new ProcessStatEncoder(), | ||
fs.createWriteStream(paths['/processstat']), | ||
callback) | ||
}, | ||
traceevent (callback) { | ||
fs.writeFile(paths['/traceevent'], JSON.stringify({ traceEvents: goodMemoryGC }), callback) | ||
} | ||
}, oncollected) | ||
} | ||
|
||
function oncollected (err) { | ||
if (err) return cleanup(err, paths['/']) | ||
|
||
tool.analyze(paths['/']).analysis | ||
.on('error', function (err) { cleanup(err, paths['/']) }) | ||
.on('data', function (result) { | ||
t.ok(result) | ||
t.same(result.issueCategory, 'performance') | ||
cleanup(null, paths['/']) | ||
}) | ||
} | ||
}) | ||
|
||
test('cmd - test analyze - memory exhausted', function (t) { | ||
const tmp = process.memoryUsage | ||
const HEAP_MAX = v8.getHeapStatistics().heap_size_limit | ||
|
||
// Mock the used function to pretend the memory is exhausted. | ||
process.memoryUsage = () => { | ||
return { | ||
rss: 0, | ||
heapTotal: HEAP_MAX, | ||
heapUsed: 0, | ||
external: 0 | ||
} | ||
} | ||
|
||
const tool = new ClinicDoctor() | ||
|
||
function cleanup (err, dirname) { | ||
process.memoryUsage = tmp | ||
t.ifError(err) | ||
|
||
t.match(dirname, /^[0-9]+\.clinic-doctor$/) | ||
|
||
rimraf(dirname, function (err) { | ||
t.ifError(err) | ||
t.end() | ||
}) | ||
} | ||
|
||
tool.on('warning', function (warning) { | ||
t.equal(warning, 'Truncating input data due to memory constrains') | ||
}) | ||
tool.on('truncate', function (undef) { | ||
t.equal(undef, undefined) | ||
}) | ||
|
||
tool.collect( | ||
[process.execPath, '-e', 'setTimeout(() => {}, 400)'], | ||
function (err, dirname) { | ||
if (err) return cleanup(err, dirname) | ||
|
||
tool.analyze(dirname).analysis | ||
.on('error', function (err) { cleanup(err, dirname) }) | ||
.on('data', function (result) { | ||
t.ok(result) | ||
t.same(result.issueCategory, 'data') | ||
cleanup(null, dirname) | ||
}) | ||
} | ||
) | ||
}) | ||
|
||
test('cmd - test analyze - missing data', function (t) { | ||
const tool = new ClinicDoctor({ debug: true }) | ||
|
||
tool.analyze('missing.clinic-doctor').analysis | ||
.on('error', function (err) { | ||
t.ok(err.message.includes('ENOENT: no such file or directory')) | ||
t.end() | ||
}) | ||
.on('data', function () { | ||
t.fail('should error') | ||
}) | ||
}) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having
visualize
output a structured object is a bit odd. If anything, it should output the file stream. A better separation of logic would becollect() -> analyze() -> visualize()
. Wherevisualize()
takes theanalyze()
output. I do see the incompatibility, but maybeanalyze()
should output streams?I don't have any strong opinions. A valid alternative is also to just force the API consumer to call both methods if they want both a structured object and a file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, I think
analyze()
→visualize()
would be ideal, but I didn't wanna break the API 😛 I did it this way so the CLI could avoid running the analysis twice. I agree with your points tho.