Skip to content

Commit

Permalink
Fix an issue calculating analysis server cpu usage.
Browse files Browse the repository at this point in the history
BUG=
R=brianwilkerson@google.com

Review-Url: https://codereview.chromium.org/2959643002 .
  • Loading branch information
devoncarew committed Jun 26, 2017
1 parent b192ecf commit de5cf42
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
8 changes: 6 additions & 2 deletions pkg/analysis_server/lib/src/utilities/profiling.dart
Expand Up @@ -32,15 +32,19 @@ class UsageInfo {
/// never more than slightly above 100.0).
final double cpuPercentage;

/// The process memory usage in bytes.
/// The process memory usage in kilobytes.
final int memoryKB;

UsageInfo(this.cpuPercentage, this.memoryKB);

double get memoryMB => memoryKB / 1024;

String toString() => '$cpuPercentage% ${memoryMB.toStringAsFixed(1)}MB';
}

class _PosixProcessProfiler extends ProcessProfiler {
static final RegExp stringSplitRegExp = new RegExp(r'\s+');

_PosixProcessProfiler() : super._();

@override
Expand Down Expand Up @@ -76,7 +80,7 @@ class _PosixProcessProfiler extends ProcessProfiler {
try {
// " 0.0 378940"
String line = psResults.split('\n').first.trim();
List<String> values = line.split(' ');
List<String> values = line.split(stringSplitRegExp);
return new UsageInfo(double.parse(values[0]), int.parse(values[1]));
} catch (e) {
return null;
Expand Down
17 changes: 8 additions & 9 deletions pkg/analysis_server/test/src/utilities/profiling_test.dart
Expand Up @@ -18,14 +18,13 @@ main() {
expect(ProcessProfiler.getProfilerForPlatform(), isNotNull);
});

// TODO: https://github.com/dart-lang/sdk/issues/29815
// test('getProcessUsage', () async {
// ProcessProfiler profiler = ProcessProfiler.getProfilerForPlatform();
// UsageInfo info = await profiler.getProcessUsage(pid);
//
// expect(info, isNotNull);
// expect(info.cpuPercentage, greaterThanOrEqualTo(0.0));
// expect(info.memoryKB, greaterThanOrEqualTo(0));
// });
test('getProcessUsage', () async {
ProcessProfiler profiler = ProcessProfiler.getProfilerForPlatform();
UsageInfo info = await profiler.getProcessUsage(pid);

expect(info, isNotNull);
expect(info.cpuPercentage, greaterThanOrEqualTo(0.0));
expect(info.memoryKB, greaterThanOrEqualTo(0));
});
});
}

0 comments on commit de5cf42

Please sign in to comment.