/hazelcast/src/test/java/com/hazelcast/impl/MapOperationsCounterTest.java

https://bitbucket.org/gabral6_gmailcom/hazelcast · Java · 213 lines · 183 code · 15 blank · 15 comment · 13 complexity · 53ee731aa8cec03960b39aa2643fe08b MD5 · raw file

  1. /*
  2. * Copyright (c) 2008-2013, Hazelcast, Inc. All Rights Reserved.
  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.hazelcast.impl;
  17. import com.hazelcast.impl.monitor.LocalMapOperationStatsImpl;
  18. import com.hazelcast.impl.monitor.MapOperationsCounter;
  19. import com.hazelcast.monitor.LocalMapOperationStats;
  20. import com.hazelcast.nio.DataSerializable;
  21. import com.hazelcast.util.Clock;
  22. import org.junit.Test;
  23. import org.junit.runner.RunWith;
  24. import java.io.*;
  25. import static org.hamcrest.CoreMatchers.equalTo;
  26. import static org.junit.Assert.*;
  27. @RunWith(com.hazelcast.util.RandomBlockJUnit4ClassRunner.class)
  28. public class MapOperationsCounterTest {
  29. @Test
  30. public void noOperation() throws Exception {
  31. MapOperationsCounter mapOperationStats = new MapOperationsCounter(100);
  32. Thread.sleep(10);
  33. LocalMapOperationStats stats = mapOperationStats.getPublishedStats();
  34. assertThat(stats.getNumberOfGets(), equalTo((long) 0));
  35. assertThat(stats.getNumberOfPuts(), equalTo((long) 0));
  36. assertThat(stats.getNumberOfRemoves(), equalTo((long) 0));
  37. assertTrue(stats.getPeriodEnd() - stats.getPeriodStart() > 0);
  38. }
  39. @Test
  40. public void callGetPublishedStatsTwice() {
  41. MapOperationsCounter mapOperationStats = new MapOperationsCounter(100);
  42. LocalMapOperationStats stats1 = mapOperationStats.getPublishedStats();
  43. LocalMapOperationStats stats2 = mapOperationStats.getPublishedStats();
  44. assertTrue(stats1 == stats2);
  45. }
  46. @Test
  47. public void doAllOperations() {
  48. MapOperationsCounter mapOperationStats = new MapOperationsCounter(100);
  49. for (int i = 0; i < 10; i++) {
  50. mapOperationStats.incrementPuts(0);
  51. mapOperationStats.incrementGets(0);
  52. mapOperationStats.incrementRemoves(0);
  53. }
  54. assertEquals(10, mapOperationStats.getPublishedStats().getNumberOfGets());
  55. assertEquals(10, mapOperationStats.getPublishedStats().getNumberOfPuts());
  56. assertEquals(10, mapOperationStats.getPublishedStats().getNumberOfRemoves());
  57. }
  58. @Test
  59. public void testTotal() {
  60. MapOperationsCounter mapOperationStats = new MapOperationsCounter(100);
  61. for (int i = 0; i < 10; i++) {
  62. mapOperationStats.incrementPuts(0);
  63. mapOperationStats.incrementGets(0);
  64. mapOperationStats.incrementRemoves(0);
  65. }
  66. assertEquals(10, mapOperationStats.getPublishedStats().getNumberOfGets());
  67. assertEquals(10, mapOperationStats.getPublishedStats().getNumberOfPuts());
  68. assertEquals(10, mapOperationStats.getPublishedStats().getNumberOfRemoves());
  69. assertEquals(30, mapOperationStats.getPublishedStats().total());
  70. }
  71. @Test
  72. public void putInLessThanSubInterval() throws InterruptedException {
  73. MapOperationsCounter mapOperationStats = new MapOperationsCounter(100);
  74. long start = Clock.currentTimeMillis();
  75. long counter = 0;
  76. boolean run = true;
  77. while (run) {
  78. mapOperationStats.incrementPuts(0);
  79. counter++;
  80. Thread.sleep(1);
  81. if (Clock.currentTimeMillis() - start > 5) {
  82. run = false;
  83. }
  84. }
  85. LocalMapOperationStats stats = mapOperationStats.getPublishedStats();
  86. assertEquals(counter, stats.getNumberOfPuts());
  87. long interval = stats.getPeriodEnd() - stats.getPeriodStart();
  88. }
  89. @Test
  90. public void putInHalfOfInterval() throws InterruptedException {
  91. MapOperationsCounter mapOperationStats = new MapOperationsCounter(100);
  92. long start = Clock.currentTimeMillis();
  93. long counter = 0;
  94. boolean run = true;
  95. while (run) {
  96. counter++;
  97. mapOperationStats.incrementPuts(0);
  98. if (Clock.currentTimeMillis() - start > 50) {
  99. run = false;
  100. }
  101. Thread.sleep(1);
  102. }
  103. mapOperationStats.incrementPuts(0);
  104. counter++;
  105. LocalMapOperationStats stats = mapOperationStats.getPublishedStats();
  106. long interval = stats.getPeriodEnd() - stats.getPeriodStart();
  107. double statTps = stats.getNumberOfPuts() / interval;
  108. double totalTps = (double) counter / (Clock.currentTimeMillis() - start);
  109. assertTrue(statTps < totalTps + 5);
  110. assertTrue(statTps > totalTps - 5);
  111. }
  112. @Test
  113. public void putLittleLessThanInterval() throws InterruptedException {
  114. MapOperationsCounter mapOperationStats = new MapOperationsCounter(100);
  115. long start = Clock.currentTimeMillis();
  116. long counter = 0;
  117. boolean run = true;
  118. while (run) {
  119. counter++;
  120. mapOperationStats.incrementPuts(0);
  121. if (Clock.currentTimeMillis() - start > 95) {
  122. run = false;
  123. }
  124. Thread.sleep(1);
  125. }
  126. mapOperationStats.incrementPuts(0);
  127. counter++;
  128. LocalMapOperationStats stats = mapOperationStats.getPublishedStats();
  129. long interval = stats.getPeriodEnd() - stats.getPeriodStart();
  130. double statTps = stats.getNumberOfPuts() / interval;
  131. double totalTps = (double) counter / (Clock.currentTimeMillis() - start);
  132. assertTrue(statTps < totalTps + 5);
  133. assertTrue(statTps > totalTps - 5);
  134. }
  135. @Test
  136. public void putLittleMoreThanInterval() throws InterruptedException {
  137. MapOperationsCounter mapOperationStats = new MapOperationsCounter(100);
  138. long start = Clock.currentTimeMillis();
  139. long counter = 0;
  140. boolean run = true;
  141. while (run) {
  142. counter++;
  143. mapOperationStats.incrementPuts(0);
  144. if (Clock.currentTimeMillis() - start > 105) {
  145. run = false;
  146. }
  147. Thread.sleep(1);
  148. }
  149. mapOperationStats.incrementPuts(0);
  150. counter++;
  151. LocalMapOperationStats stats = mapOperationStats.getPublishedStats();
  152. long interval = stats.getPeriodEnd() - stats.getPeriodStart();
  153. double statTps = stats.getNumberOfPuts() / interval;
  154. double totalTps = (double) counter / (Clock.currentTimeMillis() - start);
  155. assertTrue(statTps < totalTps + 5);
  156. assertTrue(statTps > totalTps - 5);
  157. }
  158. @Test
  159. public void putWayMoreThanInterval() throws InterruptedException {
  160. MapOperationsCounter mapOperationStats = new MapOperationsCounter(100);
  161. long start = Clock.currentTimeMillis();
  162. long counter = 0;
  163. boolean run = true;
  164. while (run) {
  165. counter++;
  166. mapOperationStats.incrementPuts(0);
  167. if (Clock.currentTimeMillis() - start > 205) {
  168. run = false;
  169. }
  170. Thread.sleep(1);
  171. }
  172. mapOperationStats.incrementPuts(0);
  173. counter++;
  174. LocalMapOperationStats stats = mapOperationStats.getPublishedStats();
  175. long interval = stats.getPeriodEnd() - stats.getPeriodStart();
  176. double statTps = stats.getNumberOfPuts() / interval;
  177. double totalTps = (double) counter / (Clock.currentTimeMillis() - start);
  178. assertTrue(statTps < totalTps + 5);
  179. assertTrue(statTps > totalTps - 5);
  180. }
  181. @Test
  182. public void testDataSerializable() throws IOException {
  183. ByteArrayOutputStream bos = new ByteArrayOutputStream();
  184. DataOutputStream dout = new DataOutputStream(bos);
  185. MapOperationsCounter mapOperationStats = new MapOperationsCounter(100);
  186. mapOperationStats.incrementPuts(0);
  187. mapOperationStats.incrementGets(0);
  188. mapOperationStats.incrementRemoves(0);
  189. ((DataSerializable) mapOperationStats.getPublishedStats()).writeData(dout);
  190. LocalMapOperationStatsImpl newStat = new LocalMapOperationStatsImpl();
  191. ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
  192. newStat.readData(new DataInputStream(bis));
  193. assertEquals(mapOperationStats.getPublishedStats().getNumberOfGets(), newStat.getNumberOfGets());
  194. assertEquals(mapOperationStats.getPublishedStats().getNumberOfPuts(), newStat.getNumberOfPuts());
  195. assertEquals(mapOperationStats.getPublishedStats().getNumberOfRemoves(), newStat.getNumberOfRemoves());
  196. String str = newStat.toString();
  197. }
  198. }