kikocis/java-driver public
Changesets · Branches · Tags · Compare
| Changeset | 19d71907ff87c5b355cd83ee27989aa5d2c8c4210b86c230ee08dc5dc59442ea |
|---|---|
| Author | Andrew Tolbert <andrew.tolbert@datastax.com> · 2016-03-01 22:23 -06:00 |
| Committer | olim7t <omichallat+github@gmail.com> · 2016-03-03 16:54 +01:00 |
| Parents | cc705dbd0878 |
4 files changed
added driver-core/src/test/java/com/datastax/driver/core/ClusterWidePercentileTrackerTest.java +65 −0
| @@ -0,0 +1,65 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core; | |
| 17 | + | |
| 18 | +import com.datastax.driver.core.exceptions.ReadTimeoutException; | |
| 19 | +import com.google.common.collect.Lists; | |
| 20 | +import com.google.common.util.concurrent.Uninterruptibles; | |
| 21 | +import org.testng.annotations.Test; | |
| 22 | + | |
| 23 | +import java.util.List; | |
| 24 | +import java.util.concurrent.TimeUnit; | |
| 25 | + | |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 27 | +import static org.mockito.Mockito.mock; | |
| 28 | + | |
| 29 | +public class ClusterWidePercentileTrackerTest | |
| 30 | + extends PercentileTrackerTest<ClusterWidePercentileTracker.Builder, ClusterWidePercentileTracker> { | |
| 31 | + | |
| 32 | + @Test(groups = "unit") | |
| 33 | + public void should_track_all_measurements_for_cluster() { | |
| 34 | + // given - a cluster wide percentile tracker. | |
| 35 | + Cluster cluster0 = mock(Cluster.class); | |
| 36 | + ClusterWidePercentileTracker tracker = builder() | |
| 37 | + .withInterval(1, TimeUnit.SECONDS) | |
| 38 | + .withMinRecordedValues(100).build(); | |
| 39 | + tracker.onRegister(cluster0); | |
| 40 | + | |
| 41 | + List<Host> hosts = Lists.newArrayList(mock(Host.class), mock(Host.class), mock(Host.class)); | |
| 42 | + List<Statement> statements = Lists.newArrayList(mock(Statement.class), mock(Statement.class)); | |
| 43 | + List<Exception> exceptions = Lists.newArrayList(new Exception(), null, new ReadTimeoutException(ConsistencyLevel.ANY, 1, 1, true), null, null); | |
| 44 | + | |
| 45 | + // when - recording latencies over a linear progression with varying hosts, statements and exceptions. | |
| 46 | + for (int i = 0; i < 100; i++) { | |
| 47 | + tracker.update( | |
| 48 | + hosts.get(i % hosts.size()), | |
| 49 | + statements.get(i % statements.size()), | |
| 50 | + exceptions.get(i % exceptions.size()), TimeUnit.NANOSECONDS.convert(i + 1, TimeUnit.MILLISECONDS)); | |
| 51 | + } | |
| 52 | + Uninterruptibles.sleepUninterruptibly(2, TimeUnit.SECONDS); | |
| 53 | + | |
| 54 | + // then - the resulting tracker's percentiles should represent that linear progression. (x percentile == x) | |
| 55 | + for (int i = 1; i <= 99; i++) { | |
| 56 | + long latencyAtPct = tracker.getLatencyAtPercentile(null, null, null, i); | |
| 57 | + assertThat(latencyAtPct).isEqualTo(i); | |
| 58 | + } | |
| 59 | + } | |
| 60 | + | |
| 61 | + @Override | |
| 62 | + public ClusterWidePercentileTracker.Builder builder() { | |
| 63 | + return ClusterWidePercentileTracker.builder(defaultMaxLatency); | |
| 64 | + } | |
| 65 | +} |
added driver-core/src/test/java/com/datastax/driver/core/CustomPercentileTrackerTest.java +75 −0
| @@ -0,0 +1,75 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core; | |
| 17 | + | |
| 18 | +import com.datastax.driver.core.exceptions.ReadTimeoutException; | |
| 19 | +import com.google.common.collect.Lists; | |
| 20 | +import com.google.common.util.concurrent.Uninterruptibles; | |
| 21 | +import org.testng.annotations.Test; | |
| 22 | + | |
| 23 | +import java.util.List; | |
| 24 | +import java.util.concurrent.TimeUnit; | |
| 25 | + | |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 27 | +import static org.mockito.Mockito.mock; | |
| 28 | + | |
| 29 | +public class CustomPercentileTrackerTest { | |
| 30 | + | |
| 31 | + @Test(groups = "unit") | |
| 32 | + public void should_return_negative_value_when_key_cant_be_computed() { | |
| 33 | + // given - A custom tracker that returns null for a specific host and keys by host otherwise. | |
| 34 | + final Cluster cluster0 = mock(Cluster.class); | |
| 35 | + final Host host0 = mock(Host.class); | |
| 36 | + final Host host1 = mock(Host.class); | |
| 37 | + | |
| 38 | + PercentileTracker tracker = new PercentileTracker(1000, 3, 100, 50) { | |
| 39 | + @Override | |
| 40 | + protected Object computeKey(Host host, Statement statement, Exception exception) { | |
| 41 | + if (host == host0) { | |
| 42 | + return host; | |
| 43 | + } else { | |
| 44 | + return null; | |
| 45 | + } | |
| 46 | + } | |
| 47 | + }; | |
| 48 | + tracker.onRegister(cluster0); | |
| 49 | + | |
| 50 | + List<Statement> statements = Lists.newArrayList(mock(Statement.class), mock(Statement.class)); | |
| 51 | + List<Exception> exceptions = Lists.newArrayList(new Exception(), null, new ReadTimeoutException(ConsistencyLevel.ANY, 1, 1, true), null, null); | |
| 52 | + | |
| 53 | + // when - recording latencies over a linear progression with varying hosts, statements and exceptions. | |
| 54 | + for (int i = 0; i < 100; i++) { | |
| 55 | + tracker.update( | |
| 56 | + host0, | |
| 57 | + statements.get(i % statements.size()), | |
| 58 | + exceptions.get(i % exceptions.size()), TimeUnit.NANOSECONDS.convert((i + 1) * 2, TimeUnit.MILLISECONDS)); | |
| 59 | + | |
| 60 | + tracker.update( | |
| 61 | + host1, | |
| 62 | + statements.get(i % statements.size()), | |
| 63 | + exceptions.get(i % exceptions.size()), TimeUnit.NANOSECONDS.convert(i + 1, TimeUnit.MILLISECONDS)); | |
| 64 | + } | |
| 65 | + Uninterruptibles.sleepUninterruptibly(2, TimeUnit.SECONDS); | |
| 66 | + | |
| 67 | + // then - host0 should return a linear progression (i*2) since it has a tracker. | |
| 68 | + // host1 should return -1 since it has no tracker since it has no key. | |
| 69 | + for (int i = 1; i <= 99; i++) { | |
| 70 | + assertThat(tracker.getLatencyAtPercentile(host0, null, null, i)).isEqualTo(i * 2); | |
| 71 | + assertThat(tracker.getLatencyAtPercentile(host1, null, null, i)).isEqualTo(-1); | |
| 72 | + } | |
| 73 | + | |
| 74 | + } | |
| 75 | +} |
added driver-core/src/test/java/com/datastax/driver/core/PerHostPercentileTrackerTest.java +80 −0
| @@ -0,0 +1,80 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core; | |
| 17 | + | |
| 18 | +import com.datastax.driver.core.exceptions.ReadTimeoutException; | |
| 19 | +import com.google.common.collect.Lists; | |
| 20 | +import com.google.common.util.concurrent.Uninterruptibles; | |
| 21 | +import org.testng.annotations.Test; | |
| 22 | + | |
| 23 | +import java.util.List; | |
| 24 | +import java.util.concurrent.TimeUnit; | |
| 25 | + | |
| 26 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 27 | +import static org.mockito.Mockito.mock; | |
| 28 | + | |
| 29 | +public class PerHostPercentileTrackerTest | |
| 30 | + extends PercentileTrackerTest<PerHostPercentileTracker.Builder, PerHostPercentileTracker> { | |
| 31 | + | |
| 32 | + @Test(groups = "unit") | |
| 33 | + public void should_track_measurements_by_host() { | |
| 34 | + // given - a per host percentile tracker. | |
| 35 | + Cluster cluster0 = mock(Cluster.class); | |
| 36 | + PerHostPercentileTracker tracker = builder() | |
| 37 | + .withInterval(1, TimeUnit.SECONDS) | |
| 38 | + .withMinRecordedValues(100).build(); | |
| 39 | + tracker.onRegister(cluster0); | |
| 40 | + | |
| 41 | + List<Host> hosts = Lists.newArrayList(mock(Host.class), mock(Host.class), mock(Host.class)); | |
| 42 | + List<Statement> statements = Lists.newArrayList(mock(Statement.class), mock(Statement.class)); | |
| 43 | + List<Exception> exceptions = Lists.newArrayList(new Exception(), null, new ReadTimeoutException(ConsistencyLevel.ANY, 1, 1, true), null, null); | |
| 44 | + | |
| 45 | + // when - recording latencies over a linear progression with varying hosts, statements and exceptions. | |
| 46 | + for (int i = 0; i < 100; i++) { | |
| 47 | + tracker.update(hosts.get(0), | |
| 48 | + statements.get(i % statements.size()), | |
| 49 | + exceptions.get(i % exceptions.size()), | |
| 50 | + TimeUnit.NANOSECONDS.convert(i + 1, TimeUnit.MILLISECONDS)); | |
| 51 | + | |
| 52 | + tracker.update(hosts.get(1), | |
| 53 | + statements.get(i % statements.size()), | |
| 54 | + exceptions.get(i % exceptions.size()), | |
| 55 | + TimeUnit.NANOSECONDS.convert((i + 1) * 2, TimeUnit.MILLISECONDS)); | |
| 56 | + | |
| 57 | + tracker.update(hosts.get(2), | |
| 58 | + statements.get(i % statements.size()), | |
| 59 | + exceptions.get(i % exceptions.size()), | |
| 60 | + TimeUnit.NANOSECONDS.convert((i + 1) * 3, TimeUnit.MILLISECONDS)); | |
| 61 | + } | |
| 62 | + Uninterruptibles.sleepUninterruptibly(2000, TimeUnit.MILLISECONDS); | |
| 63 | + | |
| 64 | + // then - the resulting tracker's percentiles should represent that linear progression for each host individually. | |
| 65 | + // host0: (x percentile == x) | |
| 66 | + // host1: (x percentile == 2x) | |
| 67 | + // host2: (x percentile == 3x) | |
| 68 | + for (int i = 1; i <= 99; i++) { | |
| 69 | + assertThat(tracker.getLatencyAtPercentile(hosts.get(0), null, null, i)).isEqualTo(i); | |
| 70 | + assertThat(tracker.getLatencyAtPercentile(hosts.get(1), null, null, i)).isEqualTo(i * 2); | |
| 71 | + assertThat(tracker.getLatencyAtPercentile(hosts.get(2), null, null, i)).isEqualTo(i * 3); | |
| 72 | + } | |
| 73 | + } | |
| 74 | + | |
| 75 | + | |
| 76 | + @Override | |
| 77 | + public PerHostPercentileTracker.Builder builder() { | |
| 78 | + return PerHostPercentileTracker.builder(defaultMaxLatency); | |
| 79 | + } | |
| 80 | +} |
added driver-core/src/test/java/com/datastax/driver/core/PercentileTrackerTest.java +166 −0
| @@ -0,0 +1,166 @@ | ||
| 1 | +/* | |
| 2 | + * Copyright (C) 2012-2015 DataStax Inc. | |
| 3 | + * | |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); | |
| 5 | + * you may not use this file except in compliance with the License. | |
| 6 | + * You may obtain a copy of the License at | |
| 7 | + * | |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 | |
| 9 | + * | |
| 10 | + * Unless required by applicable law or agreed to in writing, software | |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, | |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 13 | + * See the License for the specific language governing permissions and | |
| 14 | + * limitations under the License. | |
| 15 | + */ | |
| 16 | +package com.datastax.driver.core; | |
| 17 | + | |
| 18 | +import com.datastax.driver.core.exceptions.*; | |
| 19 | +import com.google.common.collect.Lists; | |
| 20 | +import com.google.common.util.concurrent.Uninterruptibles; | |
| 21 | +import org.apache.log4j.Level; | |
| 22 | +import org.apache.log4j.Logger; | |
| 23 | +import org.testng.annotations.Test; | |
| 24 | + | |
| 25 | +import java.util.List; | |
| 26 | +import java.util.concurrent.TimeUnit; | |
| 27 | + | |
| 28 | +import static org.assertj.core.api.Assertions.assertThat; | |
| 29 | +import static org.mockito.Mockito.mock; | |
| 30 | + | |
| 31 | +public abstract class PercentileTrackerTest<B extends PercentileTracker.Builder<B, T>, T extends PercentileTracker> { | |
| 32 | + | |
| 33 | + Host defaultHost = mock(Host.class); | |
| 34 | + | |
| 35 | + @SuppressWarnings("ThrowableResultOfMethodCallIgnored") | |
| 36 | + Exception defaultException = mock(Exception.class); | |
| 37 | + | |
| 38 | + Statement defaultStatement = mock(Statement.class); | |
| 39 | + | |
| 40 | + long defaultMaxLatency = 1000; | |
| 41 | + | |
| 42 | + public abstract B builder(); | |
| 43 | + | |
| 44 | + @Test(groups = "unit") | |
| 45 | + public void should_ignore_certain_exceptions() throws Exception { | |
| 46 | + // given - a percentile tracker. | |
| 47 | + Cluster cluster0 = mock(Cluster.class); | |
| 48 | + T tracker = builder() | |
| 49 | + .withInterval(50, TimeUnit.MILLISECONDS) | |
| 50 | + .withMinRecordedValues(100).build(); | |
| 51 | + tracker.onRegister(cluster0); | |
| 52 | + | |
| 53 | + // when - recording measurements with the following exceptions. | |
| 54 | + List<Exception> exceptionsToIgnore = Lists.<Exception>newArrayList( | |
| 55 | + new UnavailableException(ConsistencyLevel.ANY, 0, 0), | |
| 56 | + new OverloadedException(null, "Overloaded"), | |
| 57 | + new BootstrappingException(null, "Bootstrapping"), | |
| 58 | + new UnpreparedException(null, "Unprepared"), | |
| 59 | + new InvalidQueryException("Validation", new Exception())); | |
| 60 | + | |
| 61 | + long startTime = System.currentTimeMillis(); | |
| 62 | + for (Exception exception : exceptionsToIgnore) { | |
| 63 | + tracker.update(defaultHost, defaultStatement, exception, TimeUnit.NANOSECONDS.convert(999, TimeUnit.MILLISECONDS)); | |
| 64 | + } | |
| 65 | + for (int i = 0; i < 100; i++) { | |
| 66 | + tracker.update(defaultHost, defaultStatement, defaultException, TimeUnit.NANOSECONDS.convert(1, TimeUnit.MILLISECONDS)); | |
| 67 | + } | |
| 68 | + long waitTime = 50 - (System.currentTimeMillis() - startTime); | |
| 69 | + Uninterruptibles.sleepUninterruptibly(waitTime + 100, TimeUnit.MILLISECONDS); | |
| 70 | + | |
| 71 | + // then - the resulting tracker's percentiles should all be 1, indicating those exceptions were ignored. | |
| 72 | + for (int i = 1; i <= 99; i++) { | |
| 73 | + long latencyAtPct = tracker.getLatencyAtPercentile(defaultHost, defaultStatement, defaultException, i); | |
| 74 | + assertThat(latencyAtPct).isEqualTo(1); | |
| 75 | + } | |
| 76 | + } | |
| 77 | + | |
| 78 | + @Test(groups = "unit") | |
| 79 | + public void should_not_record_anything_if_not_enough_measurements() throws Exception { | |
| 80 | + // given - a percentile tracker with 100 min recorded values. | |
| 81 | + Cluster cluster0 = mock(Cluster.class); | |
| 82 | + T tracker = builder() | |
| 83 | + .withInterval(50, TimeUnit.MILLISECONDS) | |
| 84 | + .withMinRecordedValues(100).build(); | |
| 85 | + tracker.onRegister(cluster0); | |
| 86 | + | |
| 87 | + // when - recording less measurements then required. | |
| 88 | + long startTime = System.currentTimeMillis(); | |
| 89 | + for (int i = 0; i < 99; i++) { | |
| 90 | + tracker.update(defaultHost, defaultStatement, defaultException, TimeUnit.NANOSECONDS.convert(1, TimeUnit.MILLISECONDS)); | |
| 91 | + } | |
| 92 | + long waitTime = 50 - (System.currentTimeMillis() - startTime); | |
| 93 | + Uninterruptibles.sleepUninterruptibly(waitTime + 100, TimeUnit.MILLISECONDS); | |
| 94 | + | |
| 95 | + // then - the resulting tracker's percentiles should all be -1, indicating there were not enough values to consider. | |
| 96 | + for (int i = 1; i <= 99; i++) { | |
| 97 | + long latencyAtPct = tracker.getLatencyAtPercentile(defaultHost, defaultStatement, defaultException, i); | |
| 98 | + assertThat(latencyAtPct).isEqualTo(-1); | |
| 99 | + } | |
| 100 | + } | |
| 101 | + | |
| 102 | + @Test(groups = "short") | |
| 103 | + public void should_return_negative_value_when_interval_hasnt_elapsed() throws Exception { | |
| 104 | + // given - a percentile tracker with a long interval. | |
| 105 | + Cluster cluster0 = mock(Cluster.class); | |
| 106 | + T tracker = builder() | |
| 107 | + .withInterval(50, TimeUnit.MINUTES) | |
| 108 | + .withMinRecordedValues(100).build(); | |
| 109 | + tracker.onRegister(cluster0); | |
| 110 | + | |
| 111 | + // when - recording enough measurements. | |
| 112 | + for (int i = 0; i < 99; i++) { | |
| 113 | + tracker.update(defaultHost, defaultStatement, defaultException, TimeUnit.NANOSECONDS.convert(1, TimeUnit.MILLISECONDS)); | |
| 114 | + } | |
| 115 | + | |
| 116 | + // then - the resulting tracker's percentiles should all be -1, since not enough time was given to elapse interval. | |
| 117 | + for (int i = 1; i <= 99; i++) { | |
| 118 | + long latencyAtPct = tracker.getLatencyAtPercentile(defaultHost, defaultStatement, defaultException, i); | |
| 119 | + assertThat(latencyAtPct).isEqualTo(-1); | |
| 120 | + } | |
| 121 | + } | |
| 122 | + | |
| 123 | + @Test(groups = "unit") | |
| 124 | + public void should_not_record_value_and_log_when_measurement_higher_than_max_trackable_value() throws Exception { | |
| 125 | + // given - a percentile tracker with a long interval. | |
| 126 | + Cluster cluster0 = mock(Cluster.class); | |
| 127 | + T tracker = builder() | |
| 128 | + .withInterval(50, TimeUnit.MILLISECONDS) | |
| 129 | + .withMinRecordedValues(100).build(); | |
| 130 | + tracker.onRegister(cluster0); | |
| 131 | + | |
| 132 | + Logger percentileLogger = Logger.getLogger(PercentileTracker.class); | |
| 133 | + Level originalLevel = percentileLogger.getLevel(); | |
| 134 | + percentileLogger.setLevel(Level.WARN); | |
| 135 | + MemoryAppender appender = new MemoryAppender(); | |
| 136 | + percentileLogger.addAppender(appender); | |
| 137 | + | |
| 138 | + try { | |
| 139 | + long startTime = System.currentTimeMillis(); | |
| 140 | + for (int i = 0; i < 100; i++) { | |
| 141 | + tracker.update(defaultHost, defaultStatement, defaultException, TimeUnit.NANOSECONDS.convert(1, TimeUnit.MILLISECONDS)); | |
| 142 | + } | |
| 143 | + | |
| 144 | + // HdrHistogram adjusts its max based on bucket size, with these values it allows a max value of 2047 (2^11-1). | |
| 145 | + long largeLatency = 2048; | |
| 146 | + // when - recording a value larger than max trackable value. | |
| 147 | + tracker.update(defaultHost, defaultStatement, defaultException, TimeUnit.NANOSECONDS.convert(largeLatency, TimeUnit.MILLISECONDS)); | |
| 148 | + | |
| 149 | + assertThat(appender.get()) | |
| 150 | + .contains("Got request with latency of " + largeLatency | |
| 151 | + + " ms, which exceeds the configured maximum trackable value " + defaultMaxLatency); | |
| 152 | + | |
| 153 | + long waitTime = 50 - (System.currentTimeMillis() - startTime); | |
| 154 | + Uninterruptibles.sleepUninterruptibly(waitTime + 100, TimeUnit.MILLISECONDS); | |
| 155 | + | |
| 156 | + // then - the resulting tracker's percentiles should all be 1, indicating the large value was ignored. | |
| 157 | + for (int i = 1; i <= 99; i++) { | |
| 158 | + long latencyAtPct = tracker.getLatencyAtPercentile(defaultHost, defaultStatement, defaultException, i); | |
| 159 | + assertThat(latencyAtPct).isEqualTo(1); | |
| 160 | + } | |
| 161 | + } finally { | |
| 162 | + percentileLogger.setLevel(originalLevel); | |
| 163 | + percentileLogger.removeAppender(appender); | |
| 164 | + } | |
| 165 | + } | |
| 166 | +} |