Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package org.HdrHistogram;
  
  import java.io.File;
 import java.util.Date;
A histogram log writer.

A Histogram logs are used to capture full fidelity, per-time-interval histograms of a recorded value.

For example, a histogram log can be used to capture high fidelity reaction-time logs for some measured system or subsystem component. Such a log would capture a full reaction time histogram for each logged interval, and could be used to later reconstruct a full HdrHistogram of the measured reaction time behavior for any arbitrary time range within the log, by adding [only] the relevant interval histograms.

This log writer will produce histogram logs that adhere to the histogram log format (see {{@link HistogramLogReader} for log format details). Optional comments, start time, legend, and format version can be logged.

By convention, it is typical for the logging application to use a comment to indicate the logging application at the head of the log, followed by the log format version, a start time, and a legend (in that order).

 
 public class HistogramLogWriter {
     private static final String HISTOGRAM_LOG_FORMAT_VERSION = "1.01";
 
     private final PrintStream log;
 
     private ByteBuffer targetBuffer;

    
Constructs a new HistogramLogWriter around a newly created file with the specified file name.

Parameters:
outputFileName The name of the file to create
Throws:
java.io.FileNotFoundException
 
     public HistogramLogWriter(final String outputFileNamethrows FileNotFoundException {
          = new PrintStream(outputFileName);
     }

    
Constructs a new HistogramLogWriter that will write into the specified file.

Parameters:
outputFile The File to write to
Throws:
java.io.FileNotFoundException
 
     public HistogramLogWriter(final File outputFilethrows FileNotFoundException {
          = new PrintStream(outputFile);
     }

    
Constructs a new HistogramLogWriter that will write into the specified output stream.

Parameters:
outputStream The OutputStream to write to
Throws:
java.io.FileNotFoundException
 
     public HistogramLogWriter(final OutputStream outputStreamthrows FileNotFoundException {
          = new PrintStream(outputStream);
     }

    
Constructs a new HistogramLogWriter that will write into the specified print stream.

Parameters:
printStream The PrintStream to write to
Throws:
java.io.FileNotFoundException
 
     public HistogramLogWriter(final PrintStream printStreamthrows FileNotFoundException {
          = printStream;
     }

    
Output an interval histogram, with the given timestamp. (note that the specified timestamp will be used, and the timestamp in the actual histogram will be ignored)

Parameters:
startTimeStampSec The start timestamp to log with the interval histogram, in seconds.
endTimeStampSec The end timestamp to log with the interval histogram, in seconds.
histogram The interval histogram to log.
 
     public void outputIntervalHistogram(final double startTimeStampSec,
                                         final double endTimeStampSec,
                                         final Histogram histogram) {
         if (( == null) || .capacity() < histogram.getNeededByteBufferCapacity()) {
              = ByteBuffer.allocate(histogram.getNeededByteBufferCapacity());
         }
         .clear();
 
        int compressedLength = histogram.encodeIntoCompressedByteBuffer(.);
        byte[] compressedArray = Arrays.copyOf(.array(), compressedLength);
        .format(."%.3f,%.3f,%.3f,%s\n",
                startTimeStampSec,
                endTimeStampSec - startTimeStampSec,
                histogram.getHistogramData().getMaxValue() / 1000000.0,
                DatatypeConverter.printBase64Binary(compressedArray)
        );
    }

    
Output an interval histogram, using the timestamp indicated in the histogram.

Parameters:
histogram The interval histogram to log.
    public void outputIntervalHistogram(final Histogram histogram) {
        outputIntervalHistogram(histogram.getStartTimeStamp()/1000.0,
                histogram.getEndTimeStamp()/1000.0,
                histogram);
    }

    
Log a start time in the log.

Parameters:
startTimeMsec time (in milliseconds) since the absolute start time (the epoch)
    public void outputStartTime(final long startTimeMsec) {
        .format(."#[StartTime: %.3f (seconds since epoch), %s]\n",
                startTimeMsec/1000.0,
                (new Date(startTimeMsec)).toString());
    }

    
Log a comment to the log. Comments will be preceded with with the '#' character.

Parameters:
comment the comment string.
    public void outputComment(final String comment) {
        .format("#%s\n"comment);
    }

    
Output a legend line to the log.
    public void outputLegend() {
        .println("\"StartTimestamp\",\"EndTimestamp\",\"Interval_Max\",\"Interval_Compressed_Histogram\"");
    }

    
Output a log format version to the log.
    public void outputLogFormatVersion() {
        outputComment("[Histogram log format version " +  +"]");
    }
New to GrepCode? Check out our FAQ X