use of org.apache.metron.profiler.ProfileMeasurement in project metron by apache.
the class ProfileHBaseMapperTest method setup.
@Before
public void setup() {
rowKeyBuilder = mock(RowKeyBuilder.class);
mapper = new ProfileHBaseMapper();
mapper.setRowKeyBuilder(rowKeyBuilder);
profile = new ProfileConfig("profile", "ip_src_addr", new ProfileResult("2 + 2"));
measurement = new ProfileMeasurement().withProfileName("profile").withEntity("entity").withPeriod(20000, 15, TimeUnit.MINUTES).withProfileValue(22).withDefinition(profile);
// the tuple will contain the original message
tuple = mock(Tuple.class);
when(tuple.getValueByField(eq("measurement"))).thenReturn(measurement);
}
use of org.apache.metron.profiler.ProfileMeasurement in project metron by apache.
the class HBaseProfilerClientTest method testFetchWithDurationAgoAndNoGroup.
/**
* Attempt to fetch a group that does not exist.
*/
@Test
public void testFetchWithDurationAgoAndNoGroup() {
// setup
final int expectedValue = 2302;
final int hours = 2;
final long startTime = System.currentTimeMillis() - TimeUnit.HOURS.toMillis(hours);
// create two groups of measurements - one on weekdays and one on weekends
ProfileMeasurement m = new ProfileMeasurement().withProfileName("profile1").withEntity("entity1").withPeriod(startTime, periodDuration, periodUnits);
profileWriter.write(m, hours * periodsPerHour, Arrays.asList("weekdays"), val -> expectedValue);
profileWriter.write(m, hours * periodsPerHour, Arrays.asList("weekends"), val -> 0);
// execute
List<Object> doesNotExist = Arrays.asList("does-not-exist");
{
List<Integer> results = client.fetch(Integer.class, "profile1", "entity1", doesNotExist, hours, TimeUnit.HOURS, Optional.empty());
// validate
assertEquals(0, results.size());
}
{
// with a default value, we'd expect a bunch of 0's
List<Integer> results = client.fetch(Integer.class, "profile1", "entity1", doesNotExist, hours, TimeUnit.HOURS, Optional.of(0));
// 8 or 9 15 minute periods in 2 hours depending on when you start
assertTrue(results.size() == 8 || results.size() == 9);
results.forEach(actual -> assertEquals(0, (int) actual));
}
}
use of org.apache.metron.profiler.ProfileMeasurement in project metron by apache.
the class HBaseProfilerClientTest method testFetchWithStartEndAndOneGroup.
/**
* The client should be able to distinguish between groups and only fetch those in the correct group.
*/
@Test
public void testFetchWithStartEndAndOneGroup() throws Exception {
final int periodsPerHour = 4;
final int expectedValue = 2302;
final int hours = 2;
final int count = hours * periodsPerHour;
final long endTime = System.currentTimeMillis();
final long startTime = endTime - TimeUnit.HOURS.toMillis(hours);
// setup - write two groups of measurements - 'weekends' and 'weekdays'
ProfileMeasurement m = new ProfileMeasurement().withProfileName("profile1").withEntity("entity1").withPeriod(startTime, periodDuration, periodUnits);
profileWriter.write(m, count, Arrays.asList("weekdays"), val -> expectedValue);
profileWriter.write(m, count, Arrays.asList("weekends"), val -> 0);
// execute
List<Integer> results = client.fetch(Integer.class, "profile1", "entity1", Arrays.asList("weekdays"), startTime, endTime, Optional.empty());
// validate
assertEquals(count, results.size());
results.forEach(actual -> assertEquals(expectedValue, (int) actual));
}
use of org.apache.metron.profiler.ProfileMeasurement in project metron by apache.
the class HBaseProfilerClientTest method testFetchWithDurationAgoAndOneGroup.
/**
* The client should be able to distinguish between groups and only fetch those in the correct group.
*/
@Test
public void testFetchWithDurationAgoAndOneGroup() throws Exception {
final int expectedValue = 2302;
final int hours = 2;
final int count = hours * periodsPerHour;
final long startTime = System.currentTimeMillis() - TimeUnit.HOURS.toMillis(hours);
// setup - write two groups of measurements - 'weekends' and 'weekdays'
ProfileMeasurement m = new ProfileMeasurement().withProfileName("profile1").withEntity("entity1").withPeriod(startTime, periodDuration, periodUnits);
profileWriter.write(m, count, Arrays.asList("weekdays"), val -> expectedValue);
profileWriter.write(m, count, Arrays.asList("weekends"), val -> 0);
// valid results
{
// execute
List<Integer> results = client.fetch(Integer.class, "profile1", "entity1", Arrays.asList("weekdays"), hours, TimeUnit.HOURS, Optional.empty());
// validate
assertEquals(count, results.size());
results.forEach(actual -> assertEquals(expectedValue, (int) actual));
}
}
use of org.apache.metron.profiler.ProfileMeasurement in project metron by apache.
the class ProfileWriter method write.
/**
* Writes profile measurements that can be used for testing.
*
* @param prototype A prototype for the types of ProfileMeasurements that should be written.
* @param count The number of profile measurements to write.
* @param group The name of the group.
* @param valueGenerator A function that consumes the previous ProfileMeasurement value and produces the next.
*/
public void write(ProfileMeasurement prototype, int count, List<Object> group, Function<Object, Object> valueGenerator) {
ProfileMeasurement m = prototype;
for (int i = 0; i < count; i++) {
// generate the next value that should be written
Object nextValue = valueGenerator.apply(m.getProfileValue());
// create a measurement for the next profile period to be written
ProfilePeriod next = m.getPeriod().next();
m = new ProfileMeasurement().withProfileName(prototype.getProfileName()).withEntity(prototype.getEntity()).withPeriod(next.getStartTimeMillis(), prototype.getPeriod().getDurationMillis(), TimeUnit.MILLISECONDS).withGroups(group).withProfileValue(nextValue);
write(m);
}
}
Aggregations