Skip to content

Commit

Permalink
fix #2791 Updated byte display in log messages for detailed size repr…
Browse files Browse the repository at this point in the history
…esentation with decimal precision
  • Loading branch information
marevol committed Dec 29, 2023
1 parent 9445229 commit 5218794
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 4 deletions.
37 changes: 33 additions & 4 deletions src/main/java/org/codelibs/fess/util/MemoryUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,21 @@
*/
package org.codelibs.fess.util;

import static org.apache.commons.io.FileUtils.ONE_EB_BI;
import static org.apache.commons.io.FileUtils.ONE_GB_BI;
import static org.apache.commons.io.FileUtils.ONE_KB_BI;
import static org.apache.commons.io.FileUtils.ONE_MB_BI;
import static org.apache.commons.io.FileUtils.ONE_PB_BI;
import static org.apache.commons.io.FileUtils.ONE_TB_BI;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.util.Collection;
import java.util.Date;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.codelibs.core.lang.StringUtil;
import java.util.Objects;

public final class MemoryUtil {
private MemoryUtil() {
Expand All @@ -39,7 +46,29 @@ public static String getMemoryUsageLog() {
}

public static String byteCountToDisplaySize(final long size) {
return FileUtils.byteCountToDisplaySize(size).replace(" ", StringUtil.EMPTY);
return byteCountToDisplaySize(BigInteger.valueOf(size));
}

private static String byteCountToDisplaySize(final BigInteger size) {
Objects.requireNonNull(size, "size");
final String displaySize;

if (size.divide(ONE_EB_BI).compareTo(BigInteger.ZERO) > 0) {
displaySize = new BigDecimal(size.divide(ONE_PB_BI)).divide(BigDecimal.valueOf(1000)) + "EB";
} else if (size.divide(ONE_PB_BI).compareTo(BigInteger.ZERO) > 0) {
displaySize = new BigDecimal(size.divide(ONE_TB_BI)).divide(BigDecimal.valueOf(1000)) + "PB";
} else if (size.divide(ONE_TB_BI).compareTo(BigInteger.ZERO) > 0) {
displaySize = new BigDecimal(size.divide(ONE_GB_BI)).divide(BigDecimal.valueOf(1000)) + "TB";
} else if (size.divide(ONE_GB_BI).compareTo(BigInteger.ZERO) > 0) {
displaySize = new BigDecimal(size.divide(ONE_MB_BI)).divide(BigDecimal.valueOf(1000)) + "GB";
} else if (size.divide(ONE_MB_BI).compareTo(BigInteger.ZERO) > 0) {
displaySize = new BigDecimal(size.divide(ONE_KB_BI)).divide(BigDecimal.valueOf(1000)) + "MB";
} else if (size.divide(ONE_KB_BI).compareTo(BigInteger.ZERO) > 0) {
displaySize = new BigDecimal(size).divide(BigDecimal.valueOf(1000)) + "KB";
} else {
displaySize = size + "bytes";
}
return displaySize;
}

public static long getUsedMemory() {
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/org/codelibs/fess/util/MemoryUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.time.ZonedDateTime;
import java.util.Date;

import org.apache.commons.io.FileUtils;
import org.codelibs.core.collection.Maps;
import org.codelibs.fess.unit.UnitFessTestCase;
import org.codelibs.sai.internal.ir.debug.ObjectSizeCalculator;
Expand All @@ -27,6 +28,28 @@

public class MemoryUtilTest extends UnitFessTestCase {

public void test_byteCountToDisplaySize() {
assertEquals("0bytes", MemoryUtil.byteCountToDisplaySize(0L));
assertEquals("999bytes", MemoryUtil.byteCountToDisplaySize(999L));
assertEquals("1000bytes", MemoryUtil.byteCountToDisplaySize(1000L));
assertEquals("1.024KB", MemoryUtil.byteCountToDisplaySize(FileUtils.ONE_KB));
assertEquals("999.999KB", MemoryUtil.byteCountToDisplaySize(999_999L));
assertEquals("1000KB", MemoryUtil.byteCountToDisplaySize(1000_000L));
assertEquals("1.024MB", MemoryUtil.byteCountToDisplaySize(FileUtils.ONE_MB));
assertEquals("976.562MB", MemoryUtil.byteCountToDisplaySize(999_999_999L));
assertEquals("976.562MB", MemoryUtil.byteCountToDisplaySize(1000_000_000L));
assertEquals("1.024GB", MemoryUtil.byteCountToDisplaySize(FileUtils.ONE_GB));
assertEquals("953.674GB", MemoryUtil.byteCountToDisplaySize(999_999_999_999L));
assertEquals("953.674GB", MemoryUtil.byteCountToDisplaySize(1000_000_000_000L));
assertEquals("1.024TB", MemoryUtil.byteCountToDisplaySize(FileUtils.ONE_TB));
assertEquals("931.322TB", MemoryUtil.byteCountToDisplaySize(999_999_999_999_999L));
assertEquals("931.322TB", MemoryUtil.byteCountToDisplaySize(1000_000_000_000_000L));
assertEquals("1.024PB", MemoryUtil.byteCountToDisplaySize(FileUtils.ONE_PB));
assertEquals("909.494PB", MemoryUtil.byteCountToDisplaySize(999_999_999_999_999_999L));
assertEquals("909.494PB", MemoryUtil.byteCountToDisplaySize(1000_000_000_000_000_000L));
assertEquals("1.024EB", MemoryUtil.byteCountToDisplaySize(FileUtils.ONE_EB));
}

public void test_getUsedMemory() {
assertTrue(MemoryUtil.getUsedMemory() >= 0);
}
Expand Down

0 comments on commit 5218794

Please sign in to comment.