add unit type for bytes

This commit is contained in:
2023-03-17 16:53:34 +01:00
parent 686e3edd60
commit 359c17bf29
6 changed files with 150 additions and 31 deletions

View File

@@ -1,6 +1,6 @@
package org.lucares.pdb.map;
import java.util.List;
import org.lucares.utils.HumanBytes;
public class PersistentMapStats {
private long values = 0;
@@ -87,7 +87,7 @@ public class PersistentMapStats {
builder.append(String.format("\navg. depth= %.2f", averageDepth));
builder.append(String.format("\navg. fill= %.2f", averageFill));
builder.append(String.format("\nvalues/node=%.2f", averageValuesInNode));
builder.append(String.format("\nfile size= %s\n", toHumanBytes(fileSize)));
builder.append(String.format("\nfile size= %s\n", HumanBytes.toHumanBytes(fileSize)));
return builder.toString();
}
@@ -100,21 +100,9 @@ public class PersistentMapStats {
builder.append(String.format("\navg. depth= %.2f -> %.2f", old.averageDepth, averageDepth));
builder.append(String.format("\navg. fill= %.2f -> %.2f", old.averageFill, averageFill));
builder.append(String.format("\nvalues/node=%.2f -> %.2f", old.averageValuesInNode, averageValuesInNode));
builder.append(String.format("\nfile size= %s -> %s\n", toHumanBytes(old.fileSize), toHumanBytes(fileSize)));
builder.append(String.format("\nfile size= %s -> %s\n", HumanBytes.toHumanBytes(old.fileSize),
HumanBytes.toHumanBytes(fileSize)));
return builder.toString();
}
private static String toHumanBytes(final long bytes) {
final List<String> powers = List.of("bytes", "KB", "MB", "GB", "TB", "PB", "EB");
int power = 1;
String result = String.format("%d bytes", bytes);
while (bytes >= Math.pow(1024, power) && power < powers.size()) {
result = String.format("%.3f", bytes / Math.pow(1024, power));
result = result.replaceAll("\\.?0*$", "");
result = result + " " + powers.get(power);
power = power + 1;
}
return result;
}
}