Skip to content

Commit

Permalink
Minor clean-up in OsUtils
Browse files Browse the repository at this point in the history
 * 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)
  • Loading branch information
psiroky authored and gnodet committed Mar 12, 2023
1 parent 3657375 commit e1815e5
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ public ExecutionResult execute(ClientOutput output, List<String> argv) {
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()),
Expand Down
22 changes: 8 additions & 14 deletions common/src/main/java/org/mvndaemon/mvnd/common/OsUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -87,7 +81,7 @@ public static long findProcessRssInKb(long pid) {
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());
Expand Down Expand Up @@ -119,7 +113,7 @@ public static long findProcessRssInKb(long pid) {
*/
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 = "))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -701,9 +701,9 @@ private AttributedString formatTransfers(String projectId) {
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 {
Expand Down
28 changes: 20 additions & 8 deletions common/src/test/java/org/mvndaemon/mvnd/common/OsUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,27 @@
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class OsUtilsTest {
class OsUtilsTest {

@Test
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(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 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
Expand Down

0 comments on commit e1815e5

Please sign in to comment.