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.hbase;
19  
20  import static org.junit.Assert.*;
21  
22  import java.io.IOException;
23  import java.util.NoSuchElementException;
24  import java.util.Queue;
25  import java.util.LinkedList;
26  
27  import org.apache.hadoop.hbase.testclassification.SmallTests;
28  import org.apache.hadoop.hbase.util.GsonUtil;
29  import org.apache.hbase.thirdparty.com.google.gson.Gson;
30  import org.junit.Test;
31  import org.junit.experimental.categories.Category;
32  
33  @Category(SmallTests.class)
34  public class TestPerformanceEvaluation {
35  
36    @Test
37    public void testSerialization() throws IOException {
38      PerformanceEvaluation.TestOptions options = new PerformanceEvaluation.TestOptions();
39      assertTrue(!options.isAutoFlush());
40      options.setAutoFlush(true);
41      Gson gson = GsonUtil.createGson().create();
42      String optionsString = gson.toJson(options);
43      PerformanceEvaluation.TestOptions optionsDeserialized =
44        gson.fromJson(optionsString, PerformanceEvaluation.TestOptions.class);
45      assertTrue(optionsDeserialized.isAutoFlush());
46    }
47  
48    @Test
49    public void testParseOptsWithThreads() {
50      Queue<String> opts = new LinkedList<>();
51      String cmdName = "sequentialWrite";
52      int threads = 1;
53      opts.offer(cmdName);
54      opts.offer(String.valueOf(threads));
55      PerformanceEvaluation.TestOptions options = PerformanceEvaluation.parseOpts(opts);
56      assertNotNull(options);
57      assertNotNull(options.getCmdName());
58      assertEquals(cmdName, options.getCmdName());
59      assertEquals(threads, options.getNumClientThreads());
60    }
61  
62    @Test
63    public void testParseOptsWrongThreads() {
64      Queue<String> opts = new LinkedList<>();
65      String cmdName = "sequentialWrite";
66      opts.offer(cmdName);
67      opts.offer("qq");
68      try {
69        PerformanceEvaluation.parseOpts(opts);
70      } catch (IllegalArgumentException e) {
71        System.out.println(e.getMessage());
72        assertEquals("Command " + cmdName + " does not have threads number", e.getMessage());
73        assertTrue(e.getCause() instanceof NumberFormatException);
74      }
75    }
76  
77    @Test
78    public void testParseOptsNoThreads() {
79      Queue<String> opts = new LinkedList<>();
80      String cmdName = "sequentialWrite";
81      try {
82        PerformanceEvaluation.parseOpts(opts);
83      } catch (IllegalArgumentException e) {
84        System.out.println(e.getMessage());
85        assertEquals("Command " + cmdName + " does not have threads number", e.getMessage());
86        assertTrue(e.getCause() instanceof NoSuchElementException);
87      }
88    }
89  
90    @Test
91    public void testSetBufferSizeOption() {
92      PerformanceEvaluation.TestOptions opts = new PerformanceEvaluation.TestOptions();
93      long bufferSize = opts.getBufferSize();
94      assertEquals(bufferSize, 2l * 1024l * 1024l);
95      opts.setBufferSize(64l * 1024l);
96      bufferSize = opts.getBufferSize();
97      assertEquals(bufferSize, 64l * 1024l);
98    }
99  }