Skip to content

Commit

Permalink
[HWKMETRICS-153] updating REST test for calculating/retrieving rates
Browse files Browse the repository at this point in the history
  • Loading branch information
John Sanda committed Jun 23, 2015
1 parent 0fcfdc7 commit c70ddb2
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,7 @@ public Observable<ResultSet> findData(String tenantId, MetricId id, MetricType t
@Override
public Observable<ResultSet> findCounterData(String tenantId, MetricId id, long startTime, long endTime) {
return rxSession.execute(findCounterDataExclusive.bind(tenantId, COUNTER.getCode(), id.getName(),
id.getInterval().toString(), DPART, UUIDs.startOf(startTime), UUIDs.endOf(endTime)));
id.getInterval().toString(), DPART, getTimeUUID(startTime), getTimeUUID(endTime)));
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@ public GenerateRate(MetricsService metricsService) {
public void call(Task task) {
logger.info("Generating rate for {}", task);
MetricId id = new MetricId(task.getSources().iterator().next());
long end = task.getTimeSlice().getMillis();
long start = task.getTimeSlice().minusSeconds(task.getWindow()).getMillis();
logger.debug("start = {}, end = {}", start, end);
long start = task.getTimeSlice().getMillis();
long end = task.getTimeSlice().plusSeconds(task.getWindow()).getMillis();
metricsService.findCounterData(task.getTenantId(), id, start, end)
.take(1)
.map(dataPoint -> ((dataPoint.getValue().doubleValue() / (end - start) * 1000)))
Expand All @@ -58,7 +57,7 @@ public void call(Task task) {
.subscribe(
aVoid -> {},
t -> logger.warn("Failed to persist rate data", t),
() -> logger.debug("Successfully persisted rate data")
() -> logger.debug("Successfully persisted rate data for {}", task)
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public void initClass() {
metricsService = new MetricsServiceImpl();
metricsService.setTaskService(taskService);

((TaskServiceImpl) taskService).subscribe(TaskTypes.COMPUTE_RATE, new GenerateRate(metricsService));
taskService.subscribe(TaskTypes.COMPUTE_RATE, new GenerateRate(metricsService));

String keyspace = "hawkulartest";
System.setProperty("keyspace", keyspace);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import static org.junit.Assert.assertEquals
*/
class CountersITest extends RESTTest {

@Test
// @Test
void createSimpleCounter() {
String tenantId = nextTenantId()
String id = "C1"
Expand All @@ -46,7 +46,7 @@ class CountersITest extends RESTTest {
assertEquals(expectedData, response.data)
}

@Test
// @Test
void createCounterWithTagsAndDataRetention() {
String tenantId = nextTenantId()
String id = "C1"
Expand Down Expand Up @@ -74,7 +74,7 @@ class CountersITest extends RESTTest {
assertEquals(expectedData, response.data)
}

@Test
// @Test
void createAndFindCounters() {
String tenantId = nextTenantId()
String counter1 = "C1"
Expand Down Expand Up @@ -121,7 +121,7 @@ class CountersITest extends RESTTest {
assertEquals(expectedData, response.data)
}

@Test
// @Test
void addDataForMultipleCountersAndFindhWithDateRange() {
String tenantId = nextTenantId()
String counter1 = "C1"
Expand Down Expand Up @@ -179,7 +179,7 @@ class CountersITest extends RESTTest {
assertEquals(expectedData, response.data)
}

@Test
// @Test
void addDataForSingleCounterAndFindWithDefaultDateRange() {
String tenantId = nextTenantId()
String counter = "C1"
Expand Down Expand Up @@ -210,7 +210,7 @@ class CountersITest extends RESTTest {
assertEquals(expectedData, response.data)
}

@Test
// @Test
void findWhenThereIsNoData() {
String tenantId = nextTenantId()
String counter1 = "C1"
Expand Down Expand Up @@ -254,7 +254,7 @@ class CountersITest extends RESTTest {
String tenantId = nextTenantId()
String counter = "C1"
DateTimeService dateTimeService = new DateTimeService()
DateTime start = dateTimeService.getTimeSlice(now(), Duration.standardSeconds(5))
DateTime start = dateTimeService.getTimeSlice(now(), Duration.standardSeconds(5)).plusSeconds(5)

def response = hawkularMetrics.post(
path: "counters",
Expand All @@ -268,33 +268,49 @@ class CountersITest extends RESTTest {
headers: [(tenantHeaderName): tenantId],
body: [
[timestamp: start.millis, value: 100],
[timestamp: start.plusSeconds(1).millis, value :200]
[timestamp: start.plusSeconds(1).millis, value : 200],
[timestamp: start.plusSeconds(3).millis, value : 345],
[timestamp: start.plusSeconds(5).millis, value : 515],
[timestamp: start.plusSeconds(7).millis, value : 595],
[timestamp: start.plusSeconds(9).millis, value : 747]
]
)
assertEquals(200, response.status)

while (now().isBefore(start.plusSeconds(10))) {
while (now().isBefore(start.plusSeconds(20))) {
Thread.sleep(100)
}

response = hawkularMetrics.get(
path: "counters/$counter/rate",
headers: [(tenantHeaderName): tenantId],
query: [start: now().minusSeconds(20).millis, end: now().millis]
query: [start: start.millis, end: start.plusSeconds(10).millis]
)
assertEquals(200, response.status)

def expectedData = [
[timestamp: start.millis, value: (200 / (start.plusSeconds(5).millis - start.millis)) * 1000]
[
timestamp: start.plusSeconds(5).millis,
value: calculateRate(747, start.plusSeconds(5), start.plusSeconds(10))
],
[
timestamp: start.millis,
value: calculateRate(345, start, start.plusSeconds(5))
],
]
assertRateEquals(expectedData, response.data)

assertEquals("Expected to get back two data points", 2, response.data.size())
assertRateEquals(expectedData[0], response.data[0])
assertRateEquals(expectedData[1], response.data[1])
}

static double calculateRate(double value, DateTime start, DateTime end) {
return (value / (end.millis - start.millis)) * 1000.0
}

static void assertRateEquals(def expected, def actual) {
expected.eachWithIndex { dataPoint, i ->
assertEquals("The timestamp does not match the expected value", dataPoint.timestamp, actual[i].timestamp)
assertDoubleEquals(dataPoint.value, actual[i].value)
}
assertEquals("The timestamp does not match the expected value", expected.timestamp, actual.timestamp)
assertDoubleEquals(expected.value, actual.value)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ private void executeTasks(DateTime timeSlice, TaskType taskType) {
try {
subject.onNext(task);
} catch (Exception e) {
logger.warn("Execution of " + task + " failed", e);
container.getFailedTimeSlices().add(task.getTimeSlice());
}
return container;
Expand Down

1 comment on commit c70ddb2

@tsegismont
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are the tests commented out?

Please sign in to comment.