feat: adjusted RAM top process parser (#1029)

This commit is contained in:
Serhiy Mytrovtsiy
2022-08-11 18:00:48 +02:00
parent 7d35e0f989
commit 900600ce7b
2 changed files with 19 additions and 3 deletions

View File

@@ -156,8 +156,19 @@ public class ProcessReader: Reader<[TopProcess]> {
static public func parseProcess(_ raw: String) -> TopProcess {
let str = raw.trimmingCharacters(in: .whitespaces)
let pidString = str.find(pattern: "^\\d+")
let usageString = str.suffix(6)
var command = str.replacingOccurrences(of: usageString, with: "")
var arr = str.replacingOccurrences(of: pidString, with: "").split(separator: " ")
if arr.first == "*" {
arr.removeFirst()
}
var usageString = str.suffix(6)
if let lastElement = arr.last {
usageString = lastElement
arr.removeLast()
}
var command = arr.joined(separator: " ")
.replacingOccurrences(of: pidString, with: "")
.trimmingCharacters(in: .whitespaces)

View File

@@ -31,7 +31,7 @@ class RAM: XCTestCase {
process = ProcessReader.parseProcess("359 NotificationCent 62M")
XCTAssertEqual(process.pid, 359)
XCTAssertEqual(process.command, "NotificationCe")
XCTAssertEqual(process.command, "NotificationCent")
XCTAssertEqual(process.usage, 62 * Double(1024 * 1024))
process = ProcessReader.parseProcess("623 SafariCloudHisto 1608K")
@@ -48,6 +48,11 @@ class RAM: XCTestCase {
XCTAssertEqual(process.pid, 329)
XCTAssertEqual(process.command, "Finder")
XCTAssertEqual(process.usage, 488 * Double(1024 * 1024))
process = ProcessReader.parseProcess("7163* AutoCAD LT 2023 11G ")
XCTAssertEqual(process.pid, 7163)
XCTAssertEqual(process.command, "AutoCAD LT 2023")
XCTAssertEqual(process.usage, 11 * Double(1024 * 1024 * 1024))
}
func testReplacePID() throws {