PageRenderTime 43ms CodeModel.GetById 10ms app.highlight 27ms RepoModel.GetById 2ms app.codeStats 0ms

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