diff options
author | Luck <git@lucko.me> | 2019-06-02 09:35:04 +0100 |
---|---|---|
committer | Luck <git@lucko.me> | 2019-06-02 09:35:04 +0100 |
commit | eedbb931fba57d398d62024cd77135610d6beadb (patch) | |
tree | b597951715cbc9fcf403ed82d27f472497ba098c /spark-common/src/main/java/me/lucko/spark/common/util | |
parent | 10a73b7ef7910fae0010ea7e06c6ed303a90dc29 (diff) | |
download | spark-eedbb931fba57d398d62024cd77135610d6beadb.tar.gz spark-eedbb931fba57d398d62024cd77135610d6beadb.tar.bz2 spark-eedbb931fba57d398d62024cd77135610d6beadb.zip |
Add support for heap dump compression (#16)
Diffstat (limited to 'spark-common/src/main/java/me/lucko/spark/common/util')
-rw-r--r-- | spark-common/src/main/java/me/lucko/spark/common/util/FormatUtil.java | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/spark-common/src/main/java/me/lucko/spark/common/util/FormatUtil.java b/spark-common/src/main/java/me/lucko/spark/common/util/FormatUtil.java new file mode 100644 index 0000000..37165d1 --- /dev/null +++ b/spark-common/src/main/java/me/lucko/spark/common/util/FormatUtil.java @@ -0,0 +1,39 @@ +/* + * This file is part of spark. + * + * Copyright (c) lucko (Luck) <luck@lucko.me> + * Copyright (c) contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see <http://www.gnu.org/licenses/>. + */ + +package me.lucko.spark.common.util; + +public final class FormatUtil { + private FormatUtil() {} + + public static String percent(double value, double max) { + double percent = (value * 100d) / max; + return (int) percent + "%"; + } + + public static String formatBytes(long bytes) { + if (bytes == 0) { + return "0 bytes"; + } + String[] sizes = new String[]{"bytes", "KB", "MB", "GB", "TB", "PB", "EB", "ZB", "YB"}; + int sizeIndex = (int) (Math.log(bytes) / Math.log(1024)); + return String.format("%.1f", bytes / Math.pow(1024, sizeIndex)) + " " + sizes[sizeIndex]; + } +} |