Skip to content

Instantly share code, notes, and snippets.

@Aitozi
Last active September 25, 2017 01:46
Show Gist options
  • Save Aitozi/29b3c237f722b67ec864670f729c2ca0 to your computer and use it in GitHub Desktop.
Save Aitozi/29b3c237f722b67ec864670f729c2ca0 to your computer and use it in GitHub Desktop.
计算Meter,每个一个UPDATE_INTERVAL_SECONDS调用一次update方法
package org.apache.flink.metrics;
public class MeterView implements Meter, View {
/** The underlying counter maintaining the count */
private final Counter counter;
/** The time-span over which the average is calculated */
private final int timeSpanInSeconds;
/** Circular array containing the history of values */
private final long[] values;
/** The index in the array for the current time */
private int time = 0;
/** The last rate we computed */
private double currentRate = 0;
public MeterView(int timeSpanInSeconds) {
this(new SimpleCounter(), timeSpanInSeconds);
}
public MeterView(Counter counter, int timeSpanInSeconds) {
this.counter = counter;
this.timeSpanInSeconds = timeSpanInSeconds - (timeSpanInSeconds % UPDATE_INTERVAL_SECONDS);
this.values = new long[this.timeSpanInSeconds / UPDATE_INTERVAL_SECONDS + 1];
}
@Override
public void markEvent() {
this.counter.inc();
}
@Override
public void markEvent(long n) {
this.counter.inc(n);
}
@Override
public long getCount() {
return counter.getCount();
}
@Override
public double getRate() {
return currentRate;
}
@Override
//这个方法很有趣,
public void update() {
time = (time + 1) % values.length;
values[time] = counter.getCount();
currentRate = ((double) (values[time] - values[(time + 1) % values.length]) / timeSpanInSeconds);
}
}
@Aitozi
Copy link
Author

Aitozi commented Sep 11, 2017

update方法写的很有意思

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment