Skip to content

Commit

Permalink
Add "hiccup" testing module
Browse files Browse the repository at this point in the history
  • Loading branch information
peter-lawrey committed Apr 9, 2013
1 parent f2643b3 commit 1a9bfd3
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,55 +20,66 @@
* @author peter.lawrey
*/
public class Histogram {
private final int buckets, resolution;
private final int count[];
private final int buckets;
private final int resolution;
private final int ordersOfMagnitude;
private final int count[][];
private final int orderCount[];
private int underflow = 0, overflow = 0;
private long totalCount = 0;

public Histogram(int buckets, int resolution) {
public Histogram(int buckets, int resolution, int ordersOfMagnitude) {
this.buckets = buckets;
this.resolution = resolution;
count = new int[buckets];
this.ordersOfMagnitude = ordersOfMagnitude;
count = new int[ordersOfMagnitude][buckets];
orderCount = new int[ordersOfMagnitude];
}

public boolean sample(long sample) {
totalCount++;
long bucket = ((sample + resolution / 2) / resolution);
if (sample < 0) {
underflow++;
return false;
} else if (bucket >= buckets) {
}
long bucket = ((sample + resolution / 2) / resolution);
int order = 0;
while (bucket >= buckets && order < ordersOfMagnitude) {
bucket /= 10;
order++;
}
if (bucket >= buckets) {
overflow++;
return false;
}
count[((int) bucket)]++;
count[order][((int) bucket)]++;
orderCount[order]++;
return true;
}

public long percentile(double percentile) {
long searchCount = (long) (totalCount * percentile / 100);
// forward search is faster
if (searchCount < totalCount * 3 / 4) {
searchCount -= underflow;
if (searchCount <= 0)
return Long.MIN_VALUE;
for (int i = 0; i < buckets; i++) {
searchCount -= count[i];
if (searchCount <= 0)
return (long) i * resolution;
searchCount = totalCount - searchCount;
searchCount -= overflow;

int order;
for (order = ordersOfMagnitude - 1; order >= 0; order--) {
if (searchCount < orderCount[order]) {
break;
}
searchCount -= orderCount[order];
}
if (searchCount <= 0)
return Long.MAX_VALUE;
} else {
searchCount = totalCount - searchCount;
searchCount -= overflow;
if (searchCount <= 0)
return Long.MAX_VALUE;
for (int i = buckets - 1; i >= 0; i--) {
searchCount -= count[i];
if (searchCount <= 0)
return (long) i * resolution;
for (int i = buckets - 1; i >= 0; i--) {
searchCount -= count[order][i];
if (searchCount <= 0) {
long l = (long) i * resolution;
for (int j = 0; j < order; j++)
l *= 10;
return l;
}
return Long.MIN_VALUE;
}
return Long.MIN_VALUE;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ public static void main(String... args) throws IOException {
return;

case 2:
hostname = args[0];
port = Integer.parseInt(args[1]);
for (int i = 0; i < TESTS; i++) {
new Sender(hostname, port).run();
}
Expand Down Expand Up @@ -185,13 +187,9 @@ public Reader(SocketChannel sc) {
@Override
public void run() {
System.err.println("... starting reader.");
Histogram warmup = new Histogram(1000, 10000);
Histogram[] histograms = new Histogram[5];
int scale = 1000;
for (int i = 0; i < histograms.length; i++) {
histograms[i] = new Histogram(1000, scale);
scale *= 10;
}
Histogram warmup = new Histogram(1000, 100, 6);
Histogram histo = new Histogram(1000, 100, 6);

ByteBuffer time = ByteBuffer.allocateDirect(4096).order(ByteOrder.nativeOrder());
try {
for (int i = 1; i <= WARMUP + RUNS; i++) {
Expand All @@ -205,9 +203,7 @@ public void run() {
long next = time.getLong();
long took = System.nanoTime() - next;
if (i >= WARMUP)
for (Histogram histogram : histograms) {
histogram.sample(took);
}
histo.sample(took);
else
warmup.sample(took);
time.compact();
Expand All @@ -218,13 +214,13 @@ public void run() {
for (double perc : new double[]{50, 90, 93, 99, 99.3, 99.9, 99.93, 99.99, 99.993, 99.999}) {
double oneIn = 1.0 / (1 - perc / 100);
heading.append("\t").append(perc).append("%");
long value = findPercentile(histograms, perc);
long value = histo.percentile(perc);
values.append("\t").append(inNanos(value));
if (RUNS <= oneIn * oneIn)
break;
}
heading.append("\tworst");
long worst = findPercentile(histograms, 99.9999);
long worst = histo.percentile(99.9999);
values.append("\t").append(inNanos(worst)).append("\tmicro-seconds");
System.out.println(heading);
System.out.println(values);
Expand All @@ -242,16 +238,6 @@ public void run() {
}
}

private long findPercentile(Histogram[] histograms, double perc) {
long value = Long.MAX_VALUE;
for (Histogram histogram : histograms) {
value = histogram.percentile(perc);
if (value < Long.MAX_VALUE)
break;
}
return value;
}

private String inNanos(long value) {
return (value < 1e4) ? "" + value / 1e3 :
(value < 1e6) ? "" + value / 1000 :
Expand Down

0 comments on commit 1a9bfd3

Please sign in to comment.