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:
Petr Široký
2023-03-10 22:44:49 +01:00
committed by Guillaume Nodet
parent 3657375e97
commit e1815e52a3
4 changed files with 31 additions and 25 deletions

View File

@@ -270,7 +270,7 @@ public class DefaultClient implements Client {
d.getPid(), d.getPid(),
d.getAddress(), d.getAddress(),
d.getState(), d.getState(),
OsUtils.kbTohumanReadable(OsUtils.findProcessRssInKb(d.getPid())), OsUtils.kbToHumanReadable(OsUtils.findProcessRssInKb(d.getPid())),
LocalDateTime.ofInstant( LocalDateTime.ofInstant(
Instant.ofEpochMilli(Math.max(d.getLastIdle(), d.getLastBusy())), Instant.ofEpochMilli(Math.max(d.getLastIdle(), d.getLastBusy())),
ZoneId.systemDefault()), ZoneId.systemDefault()),

View File

@@ -40,37 +40,31 @@ public class OsUtils {
private OsUtils() {} private OsUtils() {}
public static String bytesTohumanReadable(long bytes) { public static String bytesToHumanReadable(long bytes) {
int unit = 0; int unit = 0;
while (bytes >= KB && unit < UNITS.length() - 1) { while (bytes >= KB && unit < UNITS.length() - 1) {
bytes /= KB; bytes /= KB;
unit++; unit++;
} }
String kbString = String.valueOf(bytes); String bytesString = String.valueOf(bytes);
return new StringBuilder(kbString.length() + 1) return bytesString + UNITS.charAt(unit);
.append(kbString)
.append(UNITS.charAt(unit))
.toString();
} }
public static String kbTohumanReadable(long kb) { public static String kbToHumanReadable(long kb) {
int unit = 1; int unit = 1;
while (kb >= KB && unit < UNITS.length() - 1) { while (kb >= KB && unit < UNITS.length() - 1) {
kb /= KB; kb /= KB;
unit++; unit++;
} }
String kbString = String.valueOf(kb); String kbString = String.valueOf(kb);
return new StringBuilder(kbString.length() + 1) return kbString + UNITS.charAt(unit);
.append(kbString)
.append(UNITS.charAt(unit))
.toString();
} }
public static long findProcessRssInKb(long pid) { public static long findProcessRssInKb(long pid) {
final Os os = Os.current(); final Os os = Os.current();
if (os.isUnixLike()) { if (os.isUnixLike()) {
String[] cmd = {"ps", "-o", "rss=", "-p", String.valueOf(pid)}; 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); exec(cmd, output);
if (output.size() == 1) { if (output.size() == 1) {
try { try {
@@ -87,7 +81,7 @@ public class OsUtils {
return -1; return -1;
} else if (os == Os.WINDOWS) { } else if (os == Os.WINDOWS) {
String[] cmd = {"wmic", "process", "where", "processid=" + pid, "get", "WorkingSetSize"}; 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); exec(cmd, output);
final List<String> nonEmptyLines = final List<String> nonEmptyLines =
output.stream().filter(l -> !l.isEmpty()).collect(Collectors.toList()); output.stream().filter(l -> !l.isEmpty()).collect(Collectors.toList());
@@ -119,7 +113,7 @@ public class OsUtils {
*/ */
public static String findJavaHomeFromJavaExecutable(String javaExecutable) { public static String findJavaHomeFromJavaExecutable(String javaExecutable) {
String[] cmd = {javaExecutable, "-XshowSettings:properties", "-version"}; String[] cmd = {javaExecutable, "-XshowSettings:properties", "-version"};
final List<String> output = new ArrayList<String>(); final List<String> output = new ArrayList<>();
exec(cmd, output); exec(cmd, output);
return output.stream() return output.stream()
.filter(l -> l.contains(" java.home = ")) .filter(l -> l.contains(" java.home = "))

View File

@@ -701,9 +701,9 @@ public class TerminalOutput implements ClientOutput {
asb.append(event.getRepositoryId()); asb.append(event.getRepositoryId());
if (cur > 0 && cur < max) { if (cur > 0 && cur < max) {
asb.append(' '); asb.append(' ');
asb.append(OsUtils.bytesTohumanReadable(cur)); asb.append(OsUtils.bytesToHumanReadable(cur));
asb.append('/'); asb.append('/');
asb.append(OsUtils.bytesTohumanReadable(max)); asb.append(OsUtils.bytesToHumanReadable(max));
} }
return asb.toAttributedString(); return asb.toAttributedString();
} else { } else {

View File

@@ -21,15 +21,27 @@ package org.mvndaemon.mvnd.common;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
public class OsUtilsTest { class OsUtilsTest {
@Test @Test
void kbTohumanReadable() { void bytesToHumanReadable() {
Assertions.assertEquals("0k", OsUtils.kbTohumanReadable(0)); Assertions.assertEquals("0B", OsUtils.bytesToHumanReadable(0L));
Assertions.assertEquals("1001k", OsUtils.kbTohumanReadable(1001)); Assertions.assertEquals("1001B", OsUtils.bytesToHumanReadable(1001L));
Assertions.assertEquals("1m", OsUtils.kbTohumanReadable(1024)); Assertions.assertEquals("1k", OsUtils.bytesToHumanReadable(1024L));
Assertions.assertEquals("1023m", OsUtils.kbTohumanReadable(1024 * 1024 - 1)); Assertions.assertEquals("1023k", OsUtils.bytesToHumanReadable(1024L * 1024L - 1L));
Assertions.assertEquals("1g", OsUtils.kbTohumanReadable(1024 * 1024)); Assertions.assertEquals("1m", OsUtils.bytesToHumanReadable(1024L * 1024L));
Assertions.assertEquals("1t", OsUtils.kbTohumanReadable(1024 * 1024 * 1024)); 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 @Test