You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2013/07/23 17:20:20 UTC
git commit: Added util to print bytes in human readble format
Updated Branches:
refs/heads/master 1278d3762 -> 77b2f7494
Added util to print bytes in human readble format
Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/77b2f749
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/77b2f749
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/77b2f749
Branch: refs/heads/master
Commit: 77b2f7494616fa8204fe4c48d894d2e3e755bd63
Parents: 1278d37
Author: Claus Ibsen <da...@apache.org>
Authored: Tue Jul 23 17:20:11 2013 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Tue Jul 23 17:20:11 2013 +0200
----------------------------------------------------------------------
.../java/org/apache/camel/util/UnitUtils.java | 43 ++++++++++++++++++++
.../org/apache/camel/util/UnitUtilsTest.java | 36 ++++++++++++++++
.../camel/karaf/commands/ContextInfo.java | 10 +++--
3 files changed, 85 insertions(+), 4 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/camel/blob/77b2f749/camel-core/src/main/java/org/apache/camel/util/UnitUtils.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/util/UnitUtils.java b/camel-core/src/main/java/org/apache/camel/util/UnitUtils.java
new file mode 100644
index 0000000..78c18bf
--- /dev/null
+++ b/camel-core/src/main/java/org/apache/camel/util/UnitUtils.java
@@ -0,0 +1,43 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.util;
+
+/**
+ * Unit utils.
+ */
+public final class UnitUtils {
+
+ private UnitUtils() {
+ }
+
+ /**
+ * If having a size in bytes and wanting to print this in human friendly\
+ * format with xx kB, xx MB, xx GB instead of a large byte number.
+ *
+ * @param bytes the value in bytes
+ */
+ public static String printUnitFromBytes(long bytes) {
+ // http://stackoverflow.com/questions/3758606/how-to-convert-byte-size-into-human-readable-format-in-java
+ int unit = 1000;
+ if (bytes < unit) {
+ return bytes + " B";
+ }
+ int exp = (int) (Math.log(bytes) / Math.log(unit));
+ String pre = "" + "kMGTPE".charAt(exp - 1);
+ return String.format("%.1f %sB", bytes / Math.pow(unit, exp), pre);
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/77b2f749/camel-core/src/test/java/org/apache/camel/util/UnitUtilsTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/util/UnitUtilsTest.java b/camel-core/src/test/java/org/apache/camel/util/UnitUtilsTest.java
new file mode 100644
index 0000000..e73d5cd
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/util/UnitUtilsTest.java
@@ -0,0 +1,36 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.util;
+
+import junit.framework.TestCase;
+
+import static org.apache.camel.util.UnitUtils.printUnitFromBytes;
+
+public class UnitUtilsTest extends TestCase {
+
+ public void testPrintUnitFromBytes() throws Exception {
+ assertEquals("999 B", printUnitFromBytes(999));
+ assertEquals("1.0 kB", printUnitFromBytes(1000));
+ assertEquals("1.0 kB", printUnitFromBytes(1001));
+
+ assertEquals("1000.0 kB", printUnitFromBytes(999999));
+ assertEquals("1.0 MB", printUnitFromBytes(1000000));
+ assertEquals("1.0 MB", printUnitFromBytes(1000001));
+
+ assertEquals("1.5 MB", printUnitFromBytes(1500001));
+ }
+}
http://git-wip-us.apache.org/repos/asf/camel/blob/77b2f749/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ContextInfo.java
----------------------------------------------------------------------
diff --git a/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ContextInfo.java b/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ContextInfo.java
index 6df41d1..a23bcf1 100644
--- a/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ContextInfo.java
+++ b/platforms/karaf/commands/src/main/java/org/apache/camel/karaf/commands/ContextInfo.java
@@ -33,6 +33,8 @@ import org.apache.felix.gogo.commands.Command;
import org.apache.karaf.shell.console.OsgiCommandSupport;
import org.apache.karaf.util.StringEscapeUtils;
+import static org.apache.camel.util.UnitUtils.printUnitFromBytes;
+
/**
* Command to display detailed information about a Camel context.
*/
@@ -160,11 +162,11 @@ public class ContextInfo extends OsgiCommandSupport {
if (camelContext.getStreamCachingStrategy().getStatistics().isStatisticsEnabled()) {
System.out.println(StringEscapeUtils.unescapeJava(String.format("\t [cacheMemoryCounter=%s, cacheMemorySize=%s, cacheMemoryAverageSize=%s, cacheSpoolCounter=%s, cacheSpoolSize=%s, cacheSpoolAverageSize=%s]",
camelContext.getStreamCachingStrategy().getStatistics().getCacheMemoryCounter(),
- camelContext.getStreamCachingStrategy().getStatistics().getCacheMemorySize(),
- camelContext.getStreamCachingStrategy().getStatistics().getCacheMemoryAverageSize(),
+ printUnitFromBytes(camelContext.getStreamCachingStrategy().getStatistics().getCacheMemorySize()),
+ printUnitFromBytes(camelContext.getStreamCachingStrategy().getStatistics().getCacheMemoryAverageSize()),
camelContext.getStreamCachingStrategy().getStatistics().getCacheSpoolCounter(),
- camelContext.getStreamCachingStrategy().getStatistics().getCacheSpoolSize(),
- camelContext.getStreamCachingStrategy().getStatistics().getCacheSpoolAverageSize())));
+ printUnitFromBytes(camelContext.getStreamCachingStrategy().getStatistics().getCacheSpoolSize()),
+ printUnitFromBytes(camelContext.getStreamCachingStrategy().getStatistics().getCacheSpoolAverageSize()))));
}
}