View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.chukwa.datacollection;
19  
20  import junit.framework.TestCase;
21  
22  /**
23   * Verifies that the stats manager calculates stats properly. This test will take
24   * at least 13 seconds to run.
25   */
26  public class TestOffsetStatsManager extends TestCase {
27  
28    org.apache.hadoop.chukwa.datacollection.OffsetStatsManager<DummyKey> statsManager = null;
29    DummyKey dummyKey = new DummyKey();
30  
31    protected void setUp() throws Exception {
32      statsManager = new OffsetStatsManager<DummyKey>();
33      dummyKey = new DummyKey();
34    }
35  
36    public void testCalcAverageRate() throws InterruptedException {
37  
38      // add roughly 1000 bytes per second for about 5 seconds
39      for (int i = 0; i < 5; i++) {
40        statsManager.addOffsetDataPoint(dummyKey, 1000 * i, System.currentTimeMillis());
41        Thread.sleep(1000);
42      }
43  
44      // calculate 5 second average
45      double rate = statsManager.calcAverageRate(dummyKey, 5);
46      assertTrue("Invalid average, expected about 1 byte/sec, found " + rate,
47                   Math.abs(1000 - rate) < 1.5);
48    }
49  
50    public void testCalcAverageRateStaleData() throws InterruptedException {
51  
52      // add offsets for about 5 seconds, but timestamp them 3 seconds ago to make
53      // them stale
54      for (int i = 0; i < 5; i++) {
55        statsManager.addOffsetDataPoint(dummyKey, 1000 * i, System.currentTimeMillis() - 3000L);
56        Thread.sleep(1000);
57      }
58  
59      // calculate 5 second average
60      double rate = statsManager.calcAverageRate(dummyKey, 5);
61      assertEquals("Should have gotten a stale data response", -1.0, rate);
62    }
63  
64    public void testCalcAverageRateNotEnoughData() throws InterruptedException {
65  
66      // add offsets for about 3 seconds
67      for (int i = 0; i < 3; i++) {
68        statsManager.addOffsetDataPoint(dummyKey, 1000 * i, System.currentTimeMillis());
69        Thread.sleep(1000);
70      }
71  
72      // calculate 5 second average
73      double rate = statsManager.calcAverageRate(dummyKey, 5);
74      assertEquals("Should have gotten a stale data response", -1.0, rate);
75    }
76  
77    private static class DummyKey {}
78  }