mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
bug fixed & optimzed Histogram memory used
This commit is contained in:
@@ -1388,7 +1388,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
|
||||
map.put("ResultSetLastErrorStackTrace", null);
|
||||
|
||||
// 35 - 39
|
||||
map.put("ConnectionConnectCount", createErrorCount);
|
||||
map.put("ConnectionConnectCount", this.getConnectCount());
|
||||
if (createError != null) {
|
||||
map.put("ConnectionErrorLastMessage", createError.getMessage());
|
||||
map.put("ConnectionErrorLastStackTrace", IOUtils.getStackTrace(createError));
|
||||
|
||||
@@ -16,28 +16,25 @@
|
||||
package com.alibaba.druid.util;
|
||||
|
||||
import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.atomic.AtomicLongArray;
|
||||
|
||||
public class Histogram {
|
||||
|
||||
private final long[] ranges;
|
||||
private final AtomicLong[] rangeCounters;
|
||||
private final long[] ranges;
|
||||
private final AtomicLongArray rangeCounters;
|
||||
|
||||
public Histogram(long... ranges){
|
||||
this.ranges = ranges;
|
||||
this.rangeCounters = new AtomicLong[ranges.length + 1];
|
||||
for (int i = 0; i < rangeCounters.length; i++) {
|
||||
rangeCounters[i] = new AtomicLong();
|
||||
}
|
||||
this.rangeCounters = new AtomicLongArray(ranges.length + 1);
|
||||
}
|
||||
|
||||
|
||||
public static Histogram makeHistogram(int rangeCount) {
|
||||
long[] rangeValues = new long[rangeCount];
|
||||
|
||||
|
||||
for (int i = 0; i < rangeValues.length; ++i) {
|
||||
rangeValues[i] = (long) Math.pow(10, i);
|
||||
}
|
||||
|
||||
|
||||
return new Histogram(rangeValues);
|
||||
}
|
||||
|
||||
@@ -47,20 +44,17 @@ public class Histogram {
|
||||
this.ranges[i] = TimeUnit.MILLISECONDS.convert(ranges[i], timeUnit);
|
||||
}
|
||||
|
||||
rangeCounters = new AtomicLong[ranges.length + 1];
|
||||
for (int i = 0; i < rangeCounters.length; ++i) {
|
||||
rangeCounters[i] = new AtomicLong();
|
||||
}
|
||||
rangeCounters = new AtomicLongArray(ranges.length + 1);
|
||||
}
|
||||
|
||||
public void reset() {
|
||||
for (int i = 0; i < rangeCounters.length; i++) {
|
||||
rangeCounters[i].set(0);
|
||||
for (int i = 0; i < rangeCounters.length(); i++) {
|
||||
rangeCounters.set(i, 0);
|
||||
}
|
||||
}
|
||||
|
||||
public void record(long millis) {
|
||||
int index = rangeCounters.length - 1;
|
||||
int index = rangeCounters.length() - 1;
|
||||
for (int i = 0; i < ranges.length; ++i) {
|
||||
if (millis < ranges[i]) {
|
||||
index = i;
|
||||
@@ -68,17 +62,17 @@ public class Histogram {
|
||||
}
|
||||
}
|
||||
|
||||
rangeCounters[index].incrementAndGet();
|
||||
rangeCounters.incrementAndGet(index);
|
||||
}
|
||||
|
||||
public long get(int index) {
|
||||
return rangeCounters[index].get();
|
||||
return rangeCounters.get(index);
|
||||
}
|
||||
|
||||
public long[] toArray() {
|
||||
long[] array = new long[rangeCounters.length];
|
||||
for (int i = 0; i < rangeCounters.length; i++) {
|
||||
array[i] = rangeCounters[i].get();
|
||||
long[] array = new long[rangeCounters.length()];
|
||||
for (int i = 0; i < rangeCounters.length(); i++) {
|
||||
array[i] = rangeCounters.get(i);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
@@ -86,29 +80,29 @@ public class Histogram {
|
||||
public long[] getRanges() {
|
||||
return ranges;
|
||||
}
|
||||
|
||||
|
||||
public long getValue(int index) {
|
||||
return rangeCounters[index].get();
|
||||
return rangeCounters.get(index);
|
||||
}
|
||||
|
||||
|
||||
public long getSum() {
|
||||
long sum = 0;
|
||||
for (int i = 0; i < rangeCounters.length; ++i) {
|
||||
sum += rangeCounters[i].get();
|
||||
for (int i = 0; i < rangeCounters.length(); ++i) {
|
||||
sum += rangeCounters.get(i);
|
||||
}
|
||||
return sum;
|
||||
}
|
||||
|
||||
|
||||
public String toString() {
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append('[');
|
||||
for (int i = 0; i < rangeCounters.length; ++i) {
|
||||
if (i != 0) {
|
||||
buf.append(',');
|
||||
}
|
||||
buf.append(rangeCounters[i].get());
|
||||
}
|
||||
buf.append(']');
|
||||
return buf.toString();
|
||||
StringBuilder buf = new StringBuilder();
|
||||
buf.append('[');
|
||||
for (int i = 0; i < rangeCounters.length(); ++i) {
|
||||
if (i != 0) {
|
||||
buf.append(',');
|
||||
}
|
||||
buf.append(rangeCounters.get(i));
|
||||
}
|
||||
buf.append(']');
|
||||
return buf.toString();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
package com.alibaba.druid.stat;
|
||||
|
||||
import java.lang.management.ManagementFactory;
|
||||
import java.text.NumberFormat;
|
||||
|
||||
import com.alibaba.druid.util.Histogram;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
|
||||
public class HistogramMemoryTest extends TestCase {
|
||||
|
||||
public void test_0() throws Exception {
|
||||
long memoryStart = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
|
||||
|
||||
final int COUNT = 1024 * 1;
|
||||
Object[] items = new Object[COUNT];
|
||||
for (int i = 0; i < COUNT; ++i) {
|
||||
// items[i] = new JdbcSqlStat("");
|
||||
items[i] = Histogram.makeHistogram(20);
|
||||
}
|
||||
|
||||
long memoryEnd = ManagementFactory.getMemoryMXBean().getHeapMemoryUsage().getUsed();
|
||||
|
||||
System.out.println("memory used : " + NumberFormat.getInstance().format(memoryEnd - memoryStart));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user