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