Skip to content

Commit

Permalink
[core] Fix checkstyle for measurements package
Browse files Browse the repository at this point in the history
  • Loading branch information
risdenk committed Feb 2, 2017
1 parent a564c4c commit 2c2d53f
Show file tree
Hide file tree
Showing 13 changed files with 521 additions and 586 deletions.
257 changes: 112 additions & 145 deletions core/src/main/java/com/yahoo/ycsb/measurements/Measurements.java

Large diffs are not rendered by default.

33 changes: 17 additions & 16 deletions core/src/main/java/com/yahoo/ycsb/measurements/OneMeasurement.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Copyright (c) 2010 Yahoo! Inc. All rights reserved.
*
* Copyright (c) 2010-2016 Yahoo! Inc., 2017 YCSB contributors All rights reserved.
* <p>
* Licensed 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
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* 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
Expand All @@ -26,37 +26,38 @@
import java.util.concurrent.atomic.AtomicInteger;

/**
* A single measured metric (such as READ LATENCY)
* A single measured metric (such as READ LATENCY).
*/
public abstract class OneMeasurement {

private final String _name;
private final ConcurrentHashMap<Status, AtomicInteger> _returncodes;
private final String name;
private final ConcurrentHashMap<Status, AtomicInteger> returncodes;

public String getName() {
return _name;
return name;
}

/**
* @param _name
* @param name measurement name
*/
public OneMeasurement(String _name) {
this._name = _name;
this._returncodes = new ConcurrentHashMap<Status, AtomicInteger>();
public OneMeasurement(String name) {
this.name = name;
this.returncodes = new ConcurrentHashMap<>();
}

public abstract void measure(int latency);

public abstract String getSummary();

/**
* No need for synchronization, using CHM to deal with that
* No need for synchronization, using CHM to deal with that.
*/
public void reportStatus(Status status) {
AtomicInteger counter = _returncodes.get(status);
AtomicInteger counter = returncodes.get(status);

if (counter == null) {
AtomicInteger other = _returncodes.putIfAbsent(status, counter = new AtomicInteger());
counter = new AtomicInteger();
AtomicInteger other = returncodes.putIfAbsent(status, counter);
if (other != null) {
counter = other;
}
Expand All @@ -74,7 +75,7 @@ public void reportStatus(Status status) {
public abstract void exportMeasurements(MeasurementsExporter exporter) throws IOException;

protected final void exportStatusCounts(MeasurementsExporter exporter) throws IOException {
for (Map.Entry<Status, AtomicInteger> entry : _returncodes.entrySet()) {
for (Map.Entry<Status, AtomicInteger> entry : returncodes.entrySet()) {
exporter.write(getName(), "Return=" + entry.getKey().getName(), entry.getValue().get());
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
/**
* Copyright (c) 2010 Yahoo! Inc. All rights reserved.
*
* Copyright (c) 2010-2016 Yahoo! Inc., 2017 YCSB contributors All rights reserved.
* <p>
* Licensed 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
*
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
*
* <p>
* 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
Expand All @@ -17,36 +17,32 @@

package com.yahoo.ycsb.measurements;

import java.io.File;
import com.yahoo.ycsb.measurements.exporter.MeasurementsExporter;
import org.HdrHistogram.Histogram;
import org.HdrHistogram.HistogramLogWriter;
import org.HdrHistogram.Recorder;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.text.DecimalFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;

import org.HdrHistogram.Histogram;
import org.HdrHistogram.HistogramLogWriter;
import org.HdrHistogram.Recorder;

import com.yahoo.ycsb.measurements.exporter.MeasurementsExporter;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
* Take measurements and maintain a HdrHistogram of a given metric, such as READ
* LATENCY.
*
* @author nitsanw
* Take measurements and maintain a HdrHistogram of a given metric, such as READ LATENCY.
*
*/
public class OneMeasurementHdrHistogram extends OneMeasurement {

// we need one log per measurement histogram
final PrintStream log;
final HistogramLogWriter histogramLogWriter;
protected final PrintStream log;
protected final HistogramLogWriter histogramLogWriter;

final Recorder histogram;
Histogram totalHistogram;
protected final Recorder histogram;
protected Histogram totalHistogram;

/**
* The name of the property for deciding what percentile values to output.
Expand All @@ -58,7 +54,7 @@ public class OneMeasurementHdrHistogram extends OneMeasurement {
*/
public static final String PERCENTILES_PROPERTY_DEFAULT = "95,99";

List<Double> percentiles;
protected List<Double> percentiles;

public OneMeasurementHdrHistogram(String name, Properties props) {
super(name);
Expand Down Expand Up @@ -86,20 +82,16 @@ public OneMeasurementHdrHistogram(String name, Properties props) {
}

/**
* It appears latency is reported in micros.
* Using {@link Recorder} to support concurrent updates to histogram.
*
* @see com.yahoo.ycsb.OneMeasurement#measure(int)
*/
* It appears latency is reported in micros.
* Using {@link Recorder} to support concurrent updates to histogram.
*/
public void measure(int latencyInMicros) {
histogram.recordValue(latencyInMicros);
}

/**
* This is called from a main thread, on orderly termination.
*
* @see com.yahoo.ycsb.measurements.OneMeasurement#exportMeasurements(com.yahoo.ycsb.measurements.exporter.MeasurementsExporter)
*/
* This is called from a main thread, on orderly termination.
*/
@Override
public void exportMeasurements(MeasurementsExporter exporter) throws IOException {
// accumulate the last interval which was not caught by status thread
Expand All @@ -114,95 +106,93 @@ public void exportMeasurements(MeasurementsExporter exporter) throws IOException
exporter.write(getName(), "MinLatency(us)", totalHistogram.getMinValue());
exporter.write(getName(), "MaxLatency(us)", totalHistogram.getMaxValue());

for (Double percentile: percentiles) {
exporter.write(getName(), ordinal(percentile) + "PercentileLatency(us)", totalHistogram.getValueAtPercentile(percentile));
for (Double percentile : percentiles) {
exporter.write(getName(), ordinal(percentile) + "PercentileLatency(us)",
totalHistogram.getValueAtPercentile(percentile));
}

exportStatusCounts(exporter);
}

/**
* This is called periodically from the StatusThread. There's a single
* StatusThread per Client process. We optionally serialize the interval to
* log on this opportunity.
*
* @see com.yahoo.ycsb.measurements.OneMeasurement#getSummary()
*/
@Override
public String getSummary() {
Histogram intervalHistogram = getIntervalHistogramAndAccumulate();
// we use the summary interval as the histogram file interval.
if (histogramLogWriter != null) {
histogramLogWriter.outputIntervalHistogram(intervalHistogram);
}

DecimalFormat d = new DecimalFormat("#.##");
return "[" + getName() + ": Count=" + intervalHistogram.getTotalCount() + ", Max="
+ intervalHistogram.getMaxValue() + ", Min=" + intervalHistogram.getMinValue() + ", Avg="
+ d.format(intervalHistogram.getMean()) + ", 90=" + d.format(intervalHistogram.getValueAtPercentile(90))
+ ", 99=" + d.format(intervalHistogram.getValueAtPercentile(99)) + ", 99.9="
+ d.format(intervalHistogram.getValueAtPercentile(99.9)) + ", 99.99="
+ d.format(intervalHistogram.getValueAtPercentile(99.99)) + "]";
}

private Histogram getIntervalHistogramAndAccumulate() {
Histogram intervalHistogram = histogram.getIntervalHistogram();
// add this to the total time histogram.
if (totalHistogram == null) {
totalHistogram = intervalHistogram;
} else {
totalHistogram.add(intervalHistogram);
}
return intervalHistogram;
}

/**
* Helper method to parse the given percentile value string
*
* @param percentileString - comma delimited string of Integer values
* @return An Integer List of percentile values
*/
private List<Double> getPercentileValues(String percentileString) {
List<Double> percentileValues = new ArrayList<Double>();
/**
* This is called periodically from the StatusThread. There's a single
* StatusThread per Client process. We optionally serialize the interval to
* log on this opportunity.
*
* @see com.yahoo.ycsb.measurements.OneMeasurement#getSummary()
*/
@Override
public String getSummary() {
Histogram intervalHistogram = getIntervalHistogramAndAccumulate();
// we use the summary interval as the histogram file interval.
if (histogramLogWriter != null) {
histogramLogWriter.outputIntervalHistogram(intervalHistogram);
}

try {
for (String rawPercentile: percentileString.split(",")) {
percentileValues.add(Double.parseDouble(rawPercentile));
}
} catch(Exception e) {
// If the given hdrhistogram.percentiles value is unreadable for whatever reason,
// then calculate and return the default set.
System.err.println("[WARN] Couldn't read " + PERCENTILES_PROPERTY + " value: '" + percentileString +
"', the default of '" + PERCENTILES_PROPERTY_DEFAULT + "' will be used.");
e.printStackTrace();
return getPercentileValues(PERCENTILES_PROPERTY_DEFAULT);
}
DecimalFormat d = new DecimalFormat("#.##");
return "[" + getName() + ": Count=" + intervalHistogram.getTotalCount() + ", Max="
+ intervalHistogram.getMaxValue() + ", Min=" + intervalHistogram.getMinValue() + ", Avg="
+ d.format(intervalHistogram.getMean()) + ", 90=" + d.format(intervalHistogram.getValueAtPercentile(90))
+ ", 99=" + d.format(intervalHistogram.getValueAtPercentile(99)) + ", 99.9="
+ d.format(intervalHistogram.getValueAtPercentile(99.9)) + ", 99.99="
+ d.format(intervalHistogram.getValueAtPercentile(99.99)) + "]";
}

return percentileValues;
private Histogram getIntervalHistogramAndAccumulate() {
Histogram intervalHistogram = histogram.getIntervalHistogram();
// add this to the total time histogram.
if (totalHistogram == null) {
totalHistogram = intervalHistogram;
} else {
totalHistogram.add(intervalHistogram);
}
return intervalHistogram;
}

/**
* Helper method to parse the given percentile value string.
*
* @param percentileString - comma delimited string of Integer values
* @return An Integer List of percentile values
*/
private List<Double> getPercentileValues(String percentileString) {
List<Double> percentileValues = new ArrayList<>();

/**
* Helper method to find the ordinal of any number. eg 1 -> 1st
* @param i
* @return ordinal string
*/
private String ordinal(Double i) {
String[] suffixes = new String[] { "th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th" };
Integer j = i.intValue();
if (i%1 == 0)
{
switch (j % 100) {
case 11:
case 12:
case 13:
return j + "th";
default:
return j + suffixes[j % 10];
}
try {
for (String rawPercentile : percentileString.split(",")) {
percentileValues.add(Double.parseDouble(rawPercentile));
}
else
{
return i.toString();
} catch (Exception e) {
// If the given hdrhistogram.percentiles value is unreadable for whatever reason,
// then calculate and return the default set.
System.err.println("[WARN] Couldn't read " + PERCENTILES_PROPERTY + " value: '" + percentileString +
"', the default of '" + PERCENTILES_PROPERTY_DEFAULT + "' will be used.");
e.printStackTrace();
return getPercentileValues(PERCENTILES_PROPERTY_DEFAULT);
}

return percentileValues;
}

/**
* Helper method to find the ordinal of any number. eg 1 -> 1st
* @param i number
* @return ordinal string
*/
private String ordinal(Double i) {
String[] suffixes = new String[]{"th", "st", "nd", "rd", "th", "th", "th", "th", "th", "th"};
Integer j = i.intValue();
if (i % 1 == 0) {
switch (j % 100) {
case 11:
case 12:
case 13:
return j + "th";
default:
return j + suffixes[j % 10];
}
} else {
return i.toString();
}
}
}
Loading

0 comments on commit 2c2d53f

Please sign in to comment.