1   /**
2    * Copyright 2011 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one or more
5    * contributor license agreements. See the NOTICE file distributed with this
6    * work for additional information regarding copyright ownership. The ASF
7    * licenses this file to you under the Apache License, Version 2.0 (the
8    * "License"); you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   * http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16   * License for the specific language governing permissions and limitations under
17   * the License.
18   */
19  package org.apache.hadoop.hbase.rest;
20  
21  import org.apache.hadoop.hbase.SmallTests;
22  import org.apache.hadoop.hbase.rest.metrics.RESTMetrics;
23  import org.junit.Test;
24  import org.junit.experimental.categories.Category;
25  import static org.junit.Assert.*;
26  
27  /**
28   * Test RESTMetrics class
29   */
30  @Category(SmallTests.class)
31  public class TestRESTMetrics {
32  
33    @Test
34    public void testRESTMetrics() throws InterruptedException {
35      long timeout = 2000;
36      RESTMetrics test = new RESTMetrics();
37      int incrementSucessfulGet = 20000;
38      int incrementSucessfulDelete = 3000000;
39      int incrementSucessfulPut = 3000000;
40      int incrementRequest = incrementSucessfulGet + incrementSucessfulDelete + incrementSucessfulPut;
41  
42      int incrementFailedGetRequests = 100;
43      int incrementFailedDeleteRequests = 30;
44      int incrementFailedPutRequests = 2;
45  
46      long start1 = System.currentTimeMillis();
47      test.doUpdates(null);
48  
49      // started value
50      assertEquals(0, test.getRequests(), 0.01);
51      assertEquals(0, test.getSucessfulDeleteCount(), 0.01);
52      assertEquals(0, test.getSucessfulPutCount(), 0.01);
53      assertEquals(0, test.getSucessfulGetCount(), 0.01);
54      assertEquals(0, test.getFailedDeleteCount(), 0.01);
55      assertEquals(0, test.getFailedGetCount(), 0.01);
56      assertEquals(0, test.getFailedPutCount(), 0.01);
57  
58      // sleep some seconds
59      Thread.sleep(timeout);
60      test.incrementRequests(incrementRequest);
61      test.incrementSucessfulGetRequests(incrementSucessfulGet);
62      test.incrementSucessfulDeleteRequests(incrementSucessfulDelete);
63      test.incrementSucessfulPutRequests(incrementSucessfulPut);
64      test.incrementFailedGetRequests(incrementFailedGetRequests);
65      test.incrementFailedDeleteRequests(incrementFailedDeleteRequests);
66      test.incrementFailedPutRequests(incrementFailedPutRequests);
67  
68      test.doUpdates(null);
69  
70      // The maximum time for stability test
71      long tmax = System.currentTimeMillis() - start1;
72  
73      testData(tmax, timeout, test.getRequests(), incrementRequest);
74      testData(tmax, timeout, test.getSucessfulGetCount(), incrementSucessfulGet);
75      testData(tmax, timeout, test.getSucessfulDeleteCount(), incrementSucessfulDelete);
76      testData(tmax, timeout, test.getSucessfulPutCount(), incrementSucessfulPut);
77      testData(tmax, timeout, test.getFailedGetCount(), incrementFailedGetRequests);
78      testData(tmax, timeout, test.getFailedDeleteCount(), incrementFailedDeleteRequests);
79      testData(tmax, timeout, test.getFailedPutCount(), incrementFailedPutRequests);
80  
81      test.shutdown();
82    }
83  
84    // test minimum and maximum speed
85    private void testData(double tmax, long tmin, float value, double requests) {
86      assertTrue((requests / tmax) * 1000 <= value);
87      assertTrue((requests / tmin) * 1000 >= value);
88  
89    }
90  }