Elffuss BraidHub

kikocis/java-driver public

Add should_log_queries_beyond_dynamic_threshold.
Changesetcc705dbd0878528bf7d1241c2eda492853b2d52de54dbf7295a45a6e7db0cfe3
AuthorAndrew Tolbert <andrew.tolbert@datastax.com> · 2016-03-01 16:29 -06:00
Committerolim7t <omichallat+github@gmail.com> · 2016-03-03 16:54 +01:00
Parents65fa7c30289c

Browse the files at this changeset

1 file changed

modified driver-core/src/test/java/com/datastax/driver/core/QueryLoggerErrorsTest.java +53 −1
@@ -16,6 +16,7 @@
1616 package com.datastax.driver.core;
1717
1818 import com.datastax.driver.core.exceptions.*;
19+import com.google.common.util.concurrent.Uninterruptibles;
1920 import org.apache.log4j.Level;
2021 import org.apache.log4j.Logger;
2122 import org.scassandra.http.client.PrimingRequest;
@@ -24,6 +25,8 @@
2425 import org.testng.annotations.DataProvider;
2526 import org.testng.annotations.Test;
2627
28+import java.util.concurrent.TimeUnit;
29+
2730 import static com.datastax.driver.core.QueryLogger.builder;
2831 import static org.apache.log4j.Level.DEBUG;
2932 import static org.apache.log4j.Level.INFO;
@@ -82,7 +85,7 @@
8285 }
8386
8487 @Test(groups = "short")
85- public void should_log_slow_queries() throws Exception {
88+ public void should_log_queries_beyond_constant_threshold() throws Exception {
8689 // given
8790 slow.setLevel(DEBUG);
8891 queryLogger = builder(cluster)
@@ -106,6 +109,55 @@
106109 .contains(query);
107110 }
108111
112+ @Test(groups = "short")
113+ public void should_log_queries_beyond_dynamic_threshold() throws Exception {
114+ // given
115+ slow.setLevel(DEBUG);
116+ queryLogger = builder(cluster)
117+ .withDynamicThreshold(ClusterWidePercentileTracker.builder(1000)
118+ .withMinRecordedValues(100)
119+ .withInterval(1, TimeUnit.SECONDS).build(), 99)
120+ .build();
121+ cluster.register(queryLogger);
122+
123+ // prime a fast query to respond right away.
124+ String fastQuery = "SELECT foo FROM bar";
125+ primingClient.prime(
126+ queryBuilder()
127+ .withQuery(fastQuery)
128+ .build()
129+ );
130+
131+ // prime a slow query to respond after 100ms.
132+ String slowQuery = "SELECT bar from foo";
133+ primingClient.prime(
134+ queryBuilder()
135+ .withQuery(slowQuery)
136+ .withThen(then().withFixedDelay(100L))
137+ .build()
138+ );
139+
140+ // submit 100 fast queries to prime to the histogram.
141+ long startTime = System.currentTimeMillis();
142+ for (int i = 0; i < 100; i++) {
143+ session.execute(fastQuery);
144+ }
145+
146+ // Wait up to 1 second to allow initial histogram to be cached.
147+ long waitTime = 1000 - (System.currentTimeMillis() - startTime);
148+ Uninterruptibles.sleepUninterruptibly(waitTime, TimeUnit.MILLISECONDS);
149+
150+ // when
151+ session.execute(slowQuery);
152+ // then
153+ String line = slowAppender.waitAndGet(5000);
154+ assertThat(line)
155+ .contains("Query too slow")
156+ .contains(ip)
157+ .contains(slowQuery)
158+ .doesNotContain(fastQuery);
159+ }
160+
109161 @Test(groups = "short")
110162 public void should_log_timed_out_queries() throws Exception {
111163 // given