|
| 1 | +// |
| 2 | +// ======================================================================== |
| 3 | +// Copyright (c) 2016-2022 Mort Bay Consulting Pty Ltd and others. |
| 4 | +// |
| 5 | +// This program and the accompanying materials are made available under the |
| 6 | +// terms of the Eclipse Public License v. 2.0 which is available at |
| 7 | +// https://www.eclipse.org/legal/epl-2.0, or the Apache License, Version 2.0 |
| 8 | +// which is available at https://www.apache.org/licenses/LICENSE-2.0. |
| 9 | +// |
| 10 | +// SPDX-License-Identifier: EPL-2.0 OR Apache-2.0 |
| 11 | +// ======================================================================== |
| 12 | +// |
| 13 | + |
| 14 | +package org.mortbay.jetty.load.generator.listeners; |
| 15 | + |
| 16 | +import java.io.Closeable; |
| 17 | +import java.io.FileNotFoundException; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Timer; |
| 20 | +import java.util.TimerTask; |
| 21 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 22 | + |
| 23 | +import org.HdrHistogram.Histogram; |
| 24 | +import org.HdrHistogram.HistogramLogWriter; |
| 25 | +import org.HdrHistogram.SingleWriterRecorder; |
| 26 | + |
| 27 | +/** |
| 28 | + * A latency recorder that writes to a file one histogram per second in HdrHistogram's |
| 29 | + * {@code HistogramLogReader} format. |
| 30 | + */ |
| 31 | +public class LatencyRecorder |
| 32 | +{ |
| 33 | + private final HistogramLogRecorder recorder; |
| 34 | + private final String histogramFilename; |
| 35 | + |
| 36 | + /** |
| 37 | + * Creates a new instance. |
| 38 | + * @param histogramFilename the histograms file name. |
| 39 | + * @throws FileNotFoundException if the file cannot be created. |
| 40 | + */ |
| 41 | + public LatencyRecorder(String histogramFilename) throws FileNotFoundException |
| 42 | + { |
| 43 | + this(histogramFilename, 0); |
| 44 | + } |
| 45 | + /** |
| 46 | + * Creates a new instance |
| 47 | + * @param histogramFilename the histograms file name. |
| 48 | + * @param minBufferSize the minimum buffer size that is used to collect one histogram, 0 for a default value. |
| 49 | + * @throws FileNotFoundException if the file cannot be created. |
| 50 | + */ |
| 51 | + public LatencyRecorder(String histogramFilename, int minBufferSize) throws FileNotFoundException |
| 52 | + { |
| 53 | + this.recorder = new HistogramLogRecorder(histogramFilename, 3, 1000, minBufferSize); |
| 54 | + this.histogramFilename = histogramFilename; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * @return the histograms file name. |
| 59 | + */ |
| 60 | + public String getFilename() |
| 61 | + { |
| 62 | + return histogramFilename; |
| 63 | + } |
| 64 | + |
| 65 | + /** |
| 66 | + * Starts the recording of the histograms into the file. |
| 67 | + */ |
| 68 | + public void startRecording() |
| 69 | + { |
| 70 | + recorder.startRecording(); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Stops the recording of the histograms into the file. |
| 75 | + */ |
| 76 | + public void stopRecording() |
| 77 | + { |
| 78 | + recorder.close(); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Add a latency value to the current histogram. |
| 83 | + * @param value the latency in ns. |
| 84 | + */ |
| 85 | + public void recordValue(long value) |
| 86 | + { |
| 87 | + recorder.recordValue(value); |
| 88 | + } |
| 89 | + |
| 90 | + private static class HistogramLogRecorder implements Closeable |
| 91 | + { |
| 92 | + private enum State |
| 93 | + { |
| 94 | + NOT_RECORDING, RECORDING, CLOSED |
| 95 | + } |
| 96 | + |
| 97 | + private final ThreadLocal<SingleWriterRecorder> recorderTl; |
| 98 | + private final List<SingleWriterRecorder> recorders = new CopyOnWriteArrayList<>(); |
| 99 | + private final Timer timer = new Timer(); |
| 100 | + private final HistogramLogWriter writer; |
| 101 | + private volatile HistogramLogRecorder.State state = HistogramLogRecorder.State.NOT_RECORDING; |
| 102 | + |
| 103 | + public HistogramLogRecorder(String histogramFilename, int numberOfSignificantValueDigits, int intervalInMs, int minBufferSize) throws FileNotFoundException |
| 104 | + { |
| 105 | + recorderTl = ThreadLocal.withInitial(() -> |
| 106 | + { |
| 107 | + SingleWriterRecorder singleWriterRecorder = new SingleWriterRecorder(numberOfSignificantValueDigits); |
| 108 | + recorders.add(singleWriterRecorder); |
| 109 | + return singleWriterRecorder; |
| 110 | + }); |
| 111 | + writer = new HistogramLogWriter(histogramFilename); |
| 112 | + timer.schedule(new TimerTask() |
| 113 | + { |
| 114 | + private final Histogram collectiveHistogram = new Histogram(numberOfSignificantValueDigits) |
| 115 | + { |
| 116 | + public int getNeededByteBufferCapacity() |
| 117 | + { |
| 118 | + int superNeededByteBufferCapacity = super.getNeededByteBufferCapacity(); |
| 119 | + if (minBufferSize > 0) |
| 120 | + return Math.max(superNeededByteBufferCapacity, minBufferSize); |
| 121 | + else |
| 122 | + return superNeededByteBufferCapacity; |
| 123 | + } |
| 124 | + }; |
| 125 | + private Histogram intervalHistogram; |
| 126 | + |
| 127 | + @Override |
| 128 | + public void run() |
| 129 | + { |
| 130 | + for (SingleWriterRecorder recorder : recorders) |
| 131 | + { |
| 132 | + intervalHistogram = recorder.getIntervalHistogram(intervalHistogram, false); |
| 133 | + collectiveHistogram.add(intervalHistogram); |
| 134 | + } |
| 135 | + if (state == HistogramLogRecorder.State.RECORDING) |
| 136 | + writer.outputIntervalHistogram(collectiveHistogram); |
| 137 | + collectiveHistogram.reset(); |
| 138 | + } |
| 139 | + }, intervalInMs, intervalInMs); |
| 140 | + } |
| 141 | + |
| 142 | + public void startRecording() |
| 143 | + { |
| 144 | + if (state != HistogramLogRecorder.State.NOT_RECORDING) |
| 145 | + throw new IllegalStateException("current state: " + state); |
| 146 | + |
| 147 | + long now = System.currentTimeMillis(); |
| 148 | + writer.setBaseTime(now); |
| 149 | + writer.outputBaseTime(now); |
| 150 | + writer.outputStartTime(now); |
| 151 | + state = HistogramLogRecorder.State.RECORDING; |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public void close() |
| 156 | + { |
| 157 | + if (state == HistogramLogRecorder.State.CLOSED) |
| 158 | + return; |
| 159 | + state = HistogramLogRecorder.State.CLOSED; |
| 160 | + |
| 161 | + timer.cancel(); |
| 162 | + writer.close(); |
| 163 | + } |
| 164 | + |
| 165 | + public void recordValue(long value) |
| 166 | + { |
| 167 | + // Always record values even if state != State.RECORDING, the timer won't write the |
| 168 | + // histogram data on disk, but the histogram code will be jit'ed. |
| 169 | + recorderTl.get().recordValue(value); |
| 170 | + } |
| 171 | + } |
| 172 | +} |
0 commit comments