mirror of
https://github.com/apache/maven-mvnd.git
synced 2025-09-10 13:15:27 +00:00
Minor clean-up in OsUtils
* fix typo in method names * add a test for the other *toHumanReadable method * replace StringBuilder usage with simple concatenation where applicable (for fixed length strings)
This commit is contained in:

committed by
Guillaume Nodet

parent
3657375e97
commit
e1815e52a3
@@ -270,7 +270,7 @@ public class DefaultClient implements Client {
|
||||
d.getPid(),
|
||||
d.getAddress(),
|
||||
d.getState(),
|
||||
OsUtils.kbTohumanReadable(OsUtils.findProcessRssInKb(d.getPid())),
|
||||
OsUtils.kbToHumanReadable(OsUtils.findProcessRssInKb(d.getPid())),
|
||||
LocalDateTime.ofInstant(
|
||||
Instant.ofEpochMilli(Math.max(d.getLastIdle(), d.getLastBusy())),
|
||||
ZoneId.systemDefault()),
|
||||
|
@@ -40,37 +40,31 @@ public class OsUtils {
|
||||
|
||||
private OsUtils() {}
|
||||
|
||||
public static String bytesTohumanReadable(long bytes) {
|
||||
public static String bytesToHumanReadable(long bytes) {
|
||||
int unit = 0;
|
||||
while (bytes >= KB && unit < UNITS.length() - 1) {
|
||||
bytes /= KB;
|
||||
unit++;
|
||||
}
|
||||
String kbString = String.valueOf(bytes);
|
||||
return new StringBuilder(kbString.length() + 1)
|
||||
.append(kbString)
|
||||
.append(UNITS.charAt(unit))
|
||||
.toString();
|
||||
String bytesString = String.valueOf(bytes);
|
||||
return bytesString + UNITS.charAt(unit);
|
||||
}
|
||||
|
||||
public static String kbTohumanReadable(long kb) {
|
||||
public static String kbToHumanReadable(long kb) {
|
||||
int unit = 1;
|
||||
while (kb >= KB && unit < UNITS.length() - 1) {
|
||||
kb /= KB;
|
||||
unit++;
|
||||
}
|
||||
String kbString = String.valueOf(kb);
|
||||
return new StringBuilder(kbString.length() + 1)
|
||||
.append(kbString)
|
||||
.append(UNITS.charAt(unit))
|
||||
.toString();
|
||||
return kbString + UNITS.charAt(unit);
|
||||
}
|
||||
|
||||
public static long findProcessRssInKb(long pid) {
|
||||
final Os os = Os.current();
|
||||
if (os.isUnixLike()) {
|
||||
String[] cmd = {"ps", "-o", "rss=", "-p", String.valueOf(pid)};
|
||||
final List<String> output = new ArrayList<String>(1);
|
||||
final List<String> output = new ArrayList<>(1);
|
||||
exec(cmd, output);
|
||||
if (output.size() == 1) {
|
||||
try {
|
||||
@@ -87,7 +81,7 @@ public class OsUtils {
|
||||
return -1;
|
||||
} else if (os == Os.WINDOWS) {
|
||||
String[] cmd = {"wmic", "process", "where", "processid=" + pid, "get", "WorkingSetSize"};
|
||||
final List<String> output = new ArrayList<String>(1);
|
||||
final List<String> output = new ArrayList<>(1);
|
||||
exec(cmd, output);
|
||||
final List<String> nonEmptyLines =
|
||||
output.stream().filter(l -> !l.isEmpty()).collect(Collectors.toList());
|
||||
@@ -119,7 +113,7 @@ public class OsUtils {
|
||||
*/
|
||||
public static String findJavaHomeFromJavaExecutable(String javaExecutable) {
|
||||
String[] cmd = {javaExecutable, "-XshowSettings:properties", "-version"};
|
||||
final List<String> output = new ArrayList<String>();
|
||||
final List<String> output = new ArrayList<>();
|
||||
exec(cmd, output);
|
||||
return output.stream()
|
||||
.filter(l -> l.contains(" java.home = "))
|
||||
|
@@ -701,9 +701,9 @@ public class TerminalOutput implements ClientOutput {
|
||||
asb.append(event.getRepositoryId());
|
||||
if (cur > 0 && cur < max) {
|
||||
asb.append(' ');
|
||||
asb.append(OsUtils.bytesTohumanReadable(cur));
|
||||
asb.append(OsUtils.bytesToHumanReadable(cur));
|
||||
asb.append('/');
|
||||
asb.append(OsUtils.bytesTohumanReadable(max));
|
||||
asb.append(OsUtils.bytesToHumanReadable(max));
|
||||
}
|
||||
return asb.toAttributedString();
|
||||
} else {
|
||||
|
@@ -21,15 +21,27 @@ package org.mvndaemon.mvnd.common;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
public class OsUtilsTest {
|
||||
class OsUtilsTest {
|
||||
|
||||
@Test
|
||||
void kbTohumanReadable() {
|
||||
Assertions.assertEquals("0k", OsUtils.kbTohumanReadable(0));
|
||||
Assertions.assertEquals("1001k", OsUtils.kbTohumanReadable(1001));
|
||||
Assertions.assertEquals("1m", OsUtils.kbTohumanReadable(1024));
|
||||
Assertions.assertEquals("1023m", OsUtils.kbTohumanReadable(1024 * 1024 - 1));
|
||||
Assertions.assertEquals("1g", OsUtils.kbTohumanReadable(1024 * 1024));
|
||||
Assertions.assertEquals("1t", OsUtils.kbTohumanReadable(1024 * 1024 * 1024));
|
||||
void bytesToHumanReadable() {
|
||||
Assertions.assertEquals("0B", OsUtils.bytesToHumanReadable(0L));
|
||||
Assertions.assertEquals("1001B", OsUtils.bytesToHumanReadable(1001L));
|
||||
Assertions.assertEquals("1k", OsUtils.bytesToHumanReadable(1024L));
|
||||
Assertions.assertEquals("1023k", OsUtils.bytesToHumanReadable(1024L * 1024L - 1L));
|
||||
Assertions.assertEquals("1m", OsUtils.bytesToHumanReadable(1024L * 1024L));
|
||||
Assertions.assertEquals("1g", OsUtils.bytesToHumanReadable(1024L * 1024L * 1024L));
|
||||
Assertions.assertEquals("1t", OsUtils.bytesToHumanReadable(1024L * 1024L * 1024L * 1024L));
|
||||
}
|
||||
|
||||
@Test
|
||||
void kbToHumanReadable() {
|
||||
Assertions.assertEquals("0k", OsUtils.kbToHumanReadable(0L));
|
||||
Assertions.assertEquals("1001k", OsUtils.kbToHumanReadable(1001L));
|
||||
Assertions.assertEquals("1m", OsUtils.kbToHumanReadable(1024L));
|
||||
Assertions.assertEquals("1023m", OsUtils.kbToHumanReadable(1024L * 1024L - 1L));
|
||||
Assertions.assertEquals("1g", OsUtils.kbToHumanReadable(1024L * 1024L));
|
||||
Assertions.assertEquals("1t", OsUtils.kbToHumanReadable(1024L * 1024L * 1024L));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
Reference in New Issue
Block a user