HBaseProfilerClient implements ProfilerClient { @Override public <T> List<ProfileMeasurement> fetch(Class<T> clazz, String profile, String entity, List<Object> groups, long start, long end, Optional<T> defaultValue) { List<ProfilePeriod> periods = ProfilePeriod.visitPeriods( start, end, periodDurationMillis, TimeUnit.MILLISECONDS, Optional.empty(), period -> period); return fetch(clazz, profile, entity, groups, periods, defaultValue); } HBaseProfilerClient(TableProvider tableProvider,
                             RowKeyBuilder rowKeyBuilder,
                             ColumnBuilder columnBuilder,
                             long periodDurationMillis,
                             String tableName,
                             Configuration hbaseConfig); @Override List<ProfileMeasurement> fetch(Class<T> clazz, String profile, String entity, List<Object> groups, long start, long end, Optional<T> defaultValue); @Override List<ProfileMeasurement> fetch(Class<T> clazz, String profile, String entity, List<Object> groups, Iterable<ProfilePeriod> periods, Optional<T> defaultValue); void setTableProvider(TableProvider tableProvider); void setRowKeyBuilder(RowKeyBuilder rowKeyBuilder); void setColumnBuilder(ColumnBuilder columnBuilder);  }
@Test public void Should_ReturnMeasurements_When_DataExistsForAGroup() { final String profile = "profile1"; final String entity = "entity1"; final int expectedValue = 2302; final int hours = 2; final int count = hours * periodsPerHour + 1; final long startTime = System.currentTimeMillis() - TimeUnit.HOURS.toMillis(hours); ProfileMeasurement prototype = new ProfileMeasurement() .withProfileName(profile) .withEntity(entity) .withPeriod(startTime, periodDuration, periodUnits); profileWriter.write(prototype, count, Arrays.asList("weekdays"), val -> expectedValue); profileWriter.write(prototype, count, Arrays.asList("weekends"), val -> 0); long end = System.currentTimeMillis(); long start = end - TimeUnit.HOURS.toMillis(2); { List<Object> groups = Arrays.asList("weekdays"); List<ProfileMeasurement> results = client.fetch(Integer.class, profile, entity, groups, start, end, Optional.empty()); assertEquals(count, results.size()); results.forEach(actual -> { assertEquals(profile, actual.getProfileName()); assertEquals(entity, actual.getEntity()); assertEquals(groups, actual.getGroups()); assertEquals(expectedValue, actual.getProfileValue()); }); } { List<Object> groups = Arrays.asList("weekends"); List<ProfileMeasurement> results = client.fetch(Integer.class, profile, entity, groups, start, end, Optional.empty()); assertEquals(count, results.size()); results.forEach(actual -> { assertEquals(profile, actual.getProfileName()); assertEquals(entity, actual.getEntity()); assertEquals(groups, actual.getGroups()); assertEquals(0, actual.getProfileValue()); }); } }
@Test public void Should_ReturnResultFromGroup_When_MultipleGroupsExist() { final String profile = "profile1"; final String entity = "entity1"; 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); 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); List<Object> weekdays = Arrays.asList("weekdays"); List<ProfileMeasurement> results = client.fetch(Integer.class, profile, entity, weekdays, startTime, endTime, Optional.empty()); assertEquals(count, results.size()); results.forEach(actual -> assertEquals(weekdays, actual.getGroups())); }
@Test public void Should_ReturnNoResults_When_GroupDoesNotExist() { final String profile = "profile1"; final String entity = "entity1"; 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); 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); List<Object> groups = Arrays.asList("does-not-exist"); List<ProfileMeasurement> results = client.fetch(Integer.class, profile, entity, groups, startTime, endTime, Optional.empty()); assertEquals(0, results.size()); }
@Test public void Should_ReturnNoResults_When_NoDataInStartToEnd() { final String profile = "profile1"; final String entity = "entity1"; final int hours = 2; int numberToWrite = hours * periodsPerHour; final List<Object> group = Arrays.asList("weekends"); final long measurementTime = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(1); ProfileMeasurement prototype = new ProfileMeasurement() .withProfileName("profile1") .withEntity("entity1") .withPeriod(measurementTime, periodDuration, periodUnits); profileWriter.write(prototype, numberToWrite, group, val -> 1000); final long endFetchAt = System.currentTimeMillis(); final long startFetchAt = endFetchAt - TimeUnit.MILLISECONDS.toMillis(30); List<ProfileMeasurement> results = client.fetch(Integer.class, profile, entity, group, startFetchAt, endFetchAt, Optional.empty()); assertEquals(0, results.size()); }