Search in sources :

Example 16 with ProfileMeasurement

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);
}
Also used : ProfileResult(org.apache.metron.common.configuration.profiler.ProfileResult) RowKeyBuilder(org.apache.metron.profiler.hbase.RowKeyBuilder) ProfileMeasurement(org.apache.metron.profiler.ProfileMeasurement) Tuple(org.apache.storm.tuple.Tuple) ProfileConfig(org.apache.metron.common.configuration.profiler.ProfileConfig) Before(org.junit.Before)

Example 17 with ProfileMeasurement

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));
    }
}
Also used : Arrays(java.util.Arrays) SaltyRowKeyBuilder(org.apache.metron.profiler.hbase.SaltyRowKeyBuilder) ColumnBuilder(org.apache.metron.profiler.hbase.ColumnBuilder) ValueOnlyColumnBuilder(org.apache.metron.profiler.hbase.ValueOnlyColumnBuilder) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) StellarStatefulExecutor(org.apache.metron.stellar.common.StellarStatefulExecutor) TimeUnit(java.util.concurrent.TimeUnit) MockHTable(org.apache.metron.hbase.mock.MockHTable) ProfileMeasurement(org.apache.metron.profiler.ProfileMeasurement) List(java.util.List) DefaultStellarStatefulExecutor(org.apache.metron.stellar.common.DefaultStellarStatefulExecutor) After(org.junit.After) Optional(java.util.Optional) RowKeyBuilder(org.apache.metron.profiler.hbase.RowKeyBuilder) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) List(java.util.List) ProfileMeasurement(org.apache.metron.profiler.ProfileMeasurement) Test(org.junit.Test)

Example 18 with ProfileMeasurement

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));
}
Also used : ProfileMeasurement(org.apache.metron.profiler.ProfileMeasurement) Test(org.junit.Test)

Example 19 with ProfileMeasurement

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));
    }
}
Also used : Arrays(java.util.Arrays) SaltyRowKeyBuilder(org.apache.metron.profiler.hbase.SaltyRowKeyBuilder) ColumnBuilder(org.apache.metron.profiler.hbase.ColumnBuilder) ValueOnlyColumnBuilder(org.apache.metron.profiler.hbase.ValueOnlyColumnBuilder) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) StellarStatefulExecutor(org.apache.metron.stellar.common.StellarStatefulExecutor) TimeUnit(java.util.concurrent.TimeUnit) MockHTable(org.apache.metron.hbase.mock.MockHTable) ProfileMeasurement(org.apache.metron.profiler.ProfileMeasurement) List(java.util.List) DefaultStellarStatefulExecutor(org.apache.metron.stellar.common.DefaultStellarStatefulExecutor) After(org.junit.After) Optional(java.util.Optional) RowKeyBuilder(org.apache.metron.profiler.hbase.RowKeyBuilder) Assert.assertEquals(org.junit.Assert.assertEquals) Before(org.junit.Before) List(java.util.List) ProfileMeasurement(org.apache.metron.profiler.ProfileMeasurement) Test(org.junit.Test)

Example 20 with ProfileMeasurement

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);
    }
}
Also used : ProfilePeriod(org.apache.metron.profiler.ProfilePeriod) ProfileMeasurement(org.apache.metron.profiler.ProfileMeasurement)

Aggregations

ProfileMeasurement (org.apache.metron.profiler.ProfileMeasurement)27 Test (org.junit.Test)19 List (java.util.List)6 SaltyRowKeyBuilder (org.apache.metron.profiler.hbase.SaltyRowKeyBuilder)6 ArrayList (java.util.ArrayList)5 RowKeyBuilder (org.apache.metron.profiler.hbase.RowKeyBuilder)5 Tuple (org.apache.storm.tuple.Tuple)5 Values (org.apache.storm.tuple.Values)4 JSONObject (org.json.simple.JSONObject)4 Before (org.junit.Before)4 ColumnBuilder (org.apache.metron.profiler.hbase.ColumnBuilder)3 ValueOnlyColumnBuilder (org.apache.metron.profiler.hbase.ValueOnlyColumnBuilder)3 Arrays (java.util.Arrays)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Optional (java.util.Optional)2 TimeUnit (java.util.concurrent.TimeUnit)2 ProfileConfig (org.apache.metron.common.configuration.profiler.ProfileConfig)2 MockHTable (org.apache.metron.hbase.mock.MockHTable)2 MessageDistributor (org.apache.metron.profiler.MessageDistributor)2