From fc7b4eb35916da560d138ff3f66fec88fcbb3338 Mon Sep 17 00:00:00 2001 From: Chisu Yu Date: Mon, 3 Dec 2012 01:56:44 +0000 Subject: [PATCH] =?UTF-8?q?[=EC=9C=A0=EC=B9=98=EC=88=98]=20[NOBTS]=20add?= =?UTF-8?q?=20terminal=20nodes=20statics=20histogram.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: http://svn.bds.nhncorp.com/pe/hippo-commons/trunk@952 84d0f5b1-2673-498c-a247-62c4ff18d310 --- .../com/profiler/common/bo/HistogramBo.java | 112 +++++++++++++++++ .../common/bo/TerminalStatisticsBo.java | 113 ++++++++++++++++++ .../common/hbase/HbaseOperations2.java | 1 + .../profiler/common/hbase/HbaseTemplate2.java | 9 ++ .../profiler/common/bo/HistogramBoTest.java | 27 +++++ 5 files changed, 262 insertions(+) create mode 100644 src/main/java/com/profiler/common/bo/HistogramBo.java create mode 100644 src/main/java/com/profiler/common/bo/TerminalStatisticsBo.java create mode 100644 src/test/java/com/profiler/common/bo/HistogramBoTest.java diff --git a/src/main/java/com/profiler/common/bo/HistogramBo.java b/src/main/java/com/profiler/common/bo/HistogramBo.java new file mode 100644 index 000000000..efb2cd04b --- /dev/null +++ b/src/main/java/com/profiler/common/bo/HistogramBo.java @@ -0,0 +1,112 @@ +package com.profiler.common.bo; + +import java.io.Serializable; +import java.util.Iterator; +import java.util.Map.Entry; +import java.util.TreeMap; + +/** + * + * @author netspider + * + */ +public class HistogramBo implements Serializable { + + private static final long serialVersionUID = 4613517226343565010L; + + public static final int DEFAULT_RESOLUTION = 100; + + private final TreeMap counts = new TreeMap(); + private final int resolution; + + private int min = 0; + private int max = 0; + private int sampleCount = 1; + + public HistogramBo(int resolution) { + this.resolution = resolution; + } + + public void addSample(int value) { + if (min > value) { + min = value; + } + + if (max < value) { + max = value; + } + + int slot = value / resolution; + Integer org = counts.get(slot); + + if (org == null) { + counts.put(slot, 1); + } else { + counts.put(slot, ++org); + } + + sampleCount++; + } + + public HistogramBo mergeWith(HistogramBo histogram) { + if (this.resolution != histogram.resolution) { + throw new IllegalArgumentException("Can't merge Histogram. different resolution."); + } + + if (this.min > histogram.min) { + this.min = histogram.min; + } + + if (this.max < histogram.max) { + this.max = histogram.max; + } + + sampleCount += histogram.sampleCount; + + for (Entry entry : histogram.counts.entrySet()) { + int slot = entry.getKey(); + Integer org = counts.get(slot); + + if (org == null) { + counts.put(slot, entry.getValue()); + } else { + counts.put(slot, org + entry.getValue()); + } + } + + return this; + } + + public int getMin() { + return min; + } + + public int getMax() { + return max; + } + + public int getSampleCount() { + return sampleCount; + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + + Iterator> iterator = counts.entrySet().iterator(); + + sb.append("["); + while (iterator.hasNext()) { + Entry entry = iterator.next(); + sb.append("{ \"from\" : ").append(entry.getKey() * resolution).append(", "); + sb.append("\"to\" : ").append(entry.getKey() * resolution + resolution).append(", "); + sb.append("\"value\" : ").append(entry.getValue()).append(" }"); + if (iterator.hasNext()) { + sb.append(", "); + } + } + sb.append("]"); + + return sb.toString(); + } +} diff --git a/src/main/java/com/profiler/common/bo/TerminalStatisticsBo.java b/src/main/java/com/profiler/common/bo/TerminalStatisticsBo.java new file mode 100644 index 000000000..5ad3a5400 --- /dev/null +++ b/src/main/java/com/profiler/common/bo/TerminalStatisticsBo.java @@ -0,0 +1,113 @@ +package com.profiler.common.bo; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.ObjectInput; +import java.io.ObjectInputStream; +import java.io.ObjectOutput; +import java.io.ObjectOutputStream; +import java.io.Serializable; +import java.util.HashSet; +import java.util.Set; + +/** + * + * @author netspider + * + */ +public class TerminalStatisticsBo implements Serializable { + + private static final long serialVersionUID = -2715236149783596830L; + + private final int histogramResolution; + private final Set agentIds = new HashSet(); + private final HistogramBo histogram; + + public TerminalStatisticsBo() { + histogramResolution = HistogramBo.DEFAULT_RESOLUTION; + histogram = new HistogramBo(histogramResolution); + } + + public void sampleElapsedTime(int value) { + histogram.addSample(value); + } + + public void addAgentId(String agentId) { + agentIds.add(agentId); + } + + public Set getAgentIds() { + return agentIds; + } + + public HistogramBo getHistogram() { + return histogram; + } + + public String toString() { + StringBuilder sb = new StringBuilder(); + + sb.append("AgentIds=").append(agentIds); + sb.append(", Histogram=").append(histogram); + + return sb.toString(); + } + + /** + * TODO SpanBo와 같이 readbytes, writebytes로 변경이 필요해보임. + * + * @return + */ + public byte[] toBytes() { + ByteArrayOutputStream bos = null; + ObjectOutput out = null; + try { + bos = new ByteArrayOutputStream(); + out = new ObjectOutputStream(bos); + out.writeObject(this); + return bos.toByteArray(); + } catch (Exception e) { + e.printStackTrace(); + return null; + } finally { + try { + if (out != null) + out.close(); + } catch (Exception e2) { + e2.printStackTrace(); + } + try { + if (bos != null) + bos.close(); + } catch (Exception e2) { + e2.printStackTrace(); + } + } + } + + public static TerminalStatisticsBo parse(byte[] bytes) { + ByteArrayInputStream bis = null; + ObjectInput in = null; + try { + bis = new ByteArrayInputStream(bytes); + in = new ObjectInputStream(bis); + return (TerminalStatisticsBo) in.readObject(); + } catch (Exception e) { + e.printStackTrace(); + return null; + } finally { + try { + if (in != null) + in.close(); + } catch (Exception e2) { + e2.printStackTrace(); + } + try { + if (bis != null) + bis.close(); + } catch (Exception e2) { + e2.printStackTrace(); + } + } + } +} diff --git a/src/main/java/com/profiler/common/hbase/HbaseOperations2.java b/src/main/java/com/profiler/common/hbase/HbaseOperations2.java index 63b8b3a6a..1db96aa80 100644 --- a/src/main/java/com/profiler/common/hbase/HbaseOperations2.java +++ b/src/main/java/com/profiler/common/hbase/HbaseOperations2.java @@ -76,4 +76,5 @@ public interface HbaseOperations2 extends HbaseOperations { void incrementColumnValue(String tableName, final byte[] rowName, final byte[] familyName, final byte[] qualifier, final long amount, final boolean writeToWAL); + void doUserBatchJob(String tableName, final HBaseBatchJob job); } diff --git a/src/main/java/com/profiler/common/hbase/HbaseTemplate2.java b/src/main/java/com/profiler/common/hbase/HbaseTemplate2.java index 96f71165d..54b261b83 100644 --- a/src/main/java/com/profiler/common/hbase/HbaseTemplate2.java +++ b/src/main/java/com/profiler/common/hbase/HbaseTemplate2.java @@ -408,4 +408,13 @@ public class HbaseTemplate2 implements HbaseOperations2, InitializingBean, Dispo }); } + public void doUserBatchJob(String tableName, final HBaseBatchJob job) { + execute(tableName, new TableCallback() { + @Override + public Object doInTable(HTable htable) throws Throwable { + job.doBatch(htable); + return null; + } + }); + } } diff --git a/src/test/java/com/profiler/common/bo/HistogramBoTest.java b/src/test/java/com/profiler/common/bo/HistogramBoTest.java new file mode 100644 index 000000000..e59f59db0 --- /dev/null +++ b/src/test/java/com/profiler/common/bo/HistogramBoTest.java @@ -0,0 +1,27 @@ +package com.profiler.common.bo; + +import org.junit.Test; + +public class HistogramBoTest { + + @Test + public void testHistogram() { + HistogramBo histogram1 = new HistogramBo(100); + for (int i = 0; i < 10; i++) { + histogram1.addSample(i); + } + + HistogramBo histogram2 = new HistogramBo(100); + histogram2.addSample(5); + for (int i = 200; i < 400; i++) { + histogram2.addSample(i); + } + + System.out.println(histogram1); + System.out.println(histogram2); + System.out.println(histogram1.mergeWith(histogram2)); + System.out.println(histogram1.getMin()); + System.out.println(histogram1.getMax()); + } + +}