use of com.linkedin.cruisecontrol.IntegerEntity in project cruise-control by linkedin.
the class MetricSampleAggregatorTest method testConcurrency.
@Test
public void testConcurrency() throws NotEnoughValidWindowsException {
final int numThreads = 10;
final int numEntities = 5;
final int samplesPerWindow = 100;
final int numRandomEntities = 10;
// We set the minimum number of samples per window to be the total number of samples to insert.
// So when there is a sample got lost we will fail to collect enough window.
final MetricSampleAggregator<String, IntegerEntity> aggregator = new MetricSampleAggregator<>(NUM_WINDOWS, WINDOW_MS, samplesPerWindow * numThreads * (numRandomEntities / numEntities), 0, 5, _metricDef);
final Random random = new Random();
List<Thread> threads = new ArrayList<>();
for (int i = 0; i < numThreads; i++) {
Thread t = new Thread() {
@Override
public void run() {
// Add samples for 10 random partitions.
int startingEntity = random.nextInt(5) % numEntities;
for (int i = 0; i < numRandomEntities; i++) {
IntegerEntity entity = new IntegerEntity("group", (startingEntity + i) % numEntities);
populateSampleAggregator(2 * NUM_WINDOWS + 1, samplesPerWindow, aggregator, entity);
}
}
};
threads.add(t);
}
threads.forEach(Thread::start);
for (Thread t : threads) {
try {
t.join();
} catch (InterruptedException e) {
// let it go.
}
}
assertEquals((NUM_WINDOWS + 1) * samplesPerWindow * numRandomEntities * numThreads, aggregator.numSamples());
AggregationOptions<String, IntegerEntity> options = new AggregationOptions<>(1, 1, NUM_WINDOWS, Collections.emptySet(), AggregationOptions.Granularity.ENTITY_GROUP, true);
MetricSampleAggregationResult<String, IntegerEntity> aggResult = aggregator.aggregate(-1, Long.MAX_VALUE, options);
assertEquals(numEntities, aggResult.valuesAndExtrapolations().size());
assertTrue(aggResult.invalidEntities().isEmpty());
for (ValuesAndExtrapolations valuesAndExtrapolations : aggResult.valuesAndExtrapolations().values()) {
assertEquals(NUM_WINDOWS, valuesAndExtrapolations.windows().size());
assertTrue(valuesAndExtrapolations.extrapolations().isEmpty());
}
}
use of com.linkedin.cruisecontrol.IntegerEntity in project cruise-control by linkedin.
the class MetricSampleAggregatorTest method testAddSampleInDifferentWindows.
@Test
public void testAddSampleInDifferentWindows() throws NotEnoughValidWindowsException {
MetricSampleAggregator<String, IntegerEntity> aggregator = new MetricSampleAggregator<>(NUM_WINDOWS, WINDOW_MS, MIN_SAMPLES_PER_WINDOW, 0, 5, _metricDef);
// The remaining windows should NUM_WINDOWS - 2 to 2 * NUM_WINDOWS - 3;
populateSampleAggregator(2 * NUM_WINDOWS - 1, MIN_SAMPLES_PER_WINDOW, aggregator);
AggregationOptions<String, IntegerEntity> options = new AggregationOptions<>(1, 1, NUM_WINDOWS, Collections.emptySet(), AggregationOptions.Granularity.ENTITY_GROUP, true);
MetricSampleAggregationResult<String, IntegerEntity> aggResults = aggregator.aggregate(-1, Long.MAX_VALUE, options);
assertNotNull(aggResults);
assertEquals(1, aggResults.valuesAndExtrapolations().size());
for (Map.Entry<IntegerEntity, ValuesAndExtrapolations> entry : aggResults.valuesAndExtrapolations().entrySet()) {
ValuesAndExtrapolations valuesAndExtrapolations = entry.getValue();
List<Long> windows = valuesAndExtrapolations.windows();
assertEquals(NUM_WINDOWS, windows.size());
for (int i = 0; i < NUM_WINDOWS; i++) {
assertEquals((2 * NUM_WINDOWS - 2 - i) * WINDOW_MS, windows.get(i).longValue());
}
for (MetricInfo info : _metricDef.all()) {
MetricValues valuesForMetric = valuesAndExtrapolations.metricValues().valuesFor(info.id());
for (int i = 0; i < NUM_WINDOWS; i++) {
double expectedValue;
if (info.strategy() == AggregationFunction.LATEST || info.strategy() == AggregationFunction.MAX) {
expectedValue = (2 * NUM_WINDOWS - 3 - i) * 10 + MIN_SAMPLES_PER_WINDOW - 1;
} else {
expectedValue = (2 * NUM_WINDOWS - 3 - i) * 10 + (MIN_SAMPLES_PER_WINDOW - 1) / 2.0;
}
assertEquals("The utilization for " + info.name() + " should be " + expectedValue, expectedValue, valuesForMetric.get(i % NUM_WINDOWS), 0);
}
}
}
assertEquals(NUM_WINDOWS + 1, aggregator.allWindows().size());
assertEquals(NUM_WINDOWS, aggregator.numAvailableWindows());
}
use of com.linkedin.cruisecontrol.IntegerEntity in project cruise-control by linkedin.
the class RawMetricValuesTest method populate.
private float[][] populate(RawMetricValues rawValues, long startingWindowIndex) {
float[][] expected = new float[_metricDef.size()][NUM_WINDOWS_TO_KEEP];
for (long windowIdx = startingWindowIndex; windowIdx < startingWindowIndex + NUM_WINDOWS_TO_KEEP; windowIdx++) {
for (int i = 0; i < MIN_SAMPLES_PER_WINDOW; i++) {
float v = 10 * windowIdx + i;
MetricSample<String, IntegerEntity> m = getMetricSample(v, v, v);
rawValues.addSample(m, windowIdx, _metricDef);
for (MetricInfo info : _metricDef.all()) {
switch(info.strategy()) {
case AVG:
expected[info.id()][(int) (windowIdx % NUM_WINDOWS_TO_KEEP)] += v / MIN_SAMPLES_PER_WINDOW;
break;
case MAX:
case LATEST:
expected[info.id()][(int) (windowIdx % NUM_WINDOWS_TO_KEEP)] = v;
break;
default:
throw new IllegalStateException("Should never be here");
}
}
}
}
return expected;
}
use of com.linkedin.cruisecontrol.IntegerEntity in project cruise-control by linkedin.
the class RawMetricValuesTest method getMetricSample.
private MetricSample<String, IntegerEntity> getMetricSample(float v1, float v2, float v3) {
MetricSample<String, IntegerEntity> metricSample = new MetricSample<>(new IntegerEntity("group", 0));
metricSample.record(_metricDef.metricInfo("metric1"), v1);
metricSample.record(_metricDef.metricInfo("metric2"), v2);
metricSample.record(_metricDef.metricInfo("metric3"), v3);
metricSample.close(0);
return metricSample;
}
Aggregations