Home | History | Annotate | Download | only in options
      1 /*
      2  * Copyright (C) 2011 Google Inc.
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
      5  * in compliance with the License. You may obtain a copy of the License at
      6  *
      7  * http://www.apache.org/licenses/LICENSE-2.0
      8  *
      9  * Unless required by applicable law or agreed to in writing, software distributed under the License
     10  * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
     11  * or implied. See the License for the specific language governing permissions and limitations under
     12  * the License.
     13  */
     14 
     15 package com.google.caliper.options;
     16 
     17 import static java.util.concurrent.TimeUnit.SECONDS;
     18 import static org.junit.Assert.assertEquals;
     19 import static org.junit.Assert.assertFalse;
     20 import static org.junit.Assert.assertNull;
     21 import static org.junit.Assert.assertTrue;
     22 import static org.junit.Assert.fail;
     23 
     24 import com.google.caliper.util.DisplayUsageException;
     25 import com.google.caliper.util.InvalidCommandException;
     26 import com.google.caliper.util.ShortDuration;
     27 import com.google.common.collect.ImmutableMap;
     28 import com.google.common.collect.ImmutableSet;
     29 import com.google.common.collect.ImmutableSetMultimap;
     30 import com.google.common.collect.Iterables;
     31 import com.google.common.io.Files;
     32 
     33 import org.junit.After;
     34 import org.junit.Before;
     35 import org.junit.Test;
     36 import org.junit.runner.RunWith;
     37 import org.junit.runners.JUnit4;
     38 
     39 import java.io.File;
     40 import java.io.IOException;
     41 
     42 @RunWith(JUnit4.class)
     43 
     44 public class ParsedOptionsTest {
     45   private File tempDir;
     46 
     47   @Before public void setUp() throws IOException {
     48     tempDir = Files.createTempDir();
     49     makeTestVmTree(tempDir);
     50   }
     51 
     52   @After public void tearDown() throws IOException {
     53     if (tempDir != null) {
     54       Runtime.getRuntime().exec(new String[] {"rm", "-rf", tempDir.getCanonicalPath()});
     55     }
     56   }
     57 
     58   private static void makeTestVmTree(File baseDir) throws IOException {
     59     File bin = new File(baseDir, "testVm/bin");
     60     bin.mkdirs();
     61     File java = new File(bin, "java");
     62     Files.touch(java);
     63   }
     64 
     65   @Test public void testNoOptions_RequireBenchmarkClassName() {
     66     try {
     67       ParsedOptions.from(new String[] {}, true);
     68       fail();
     69     } catch (InvalidCommandException expected) {
     70       assertEquals("No benchmark class specified", expected.getMessage());
     71     }
     72   }
     73 
     74   @Test public void testTooManyArguments_RequireBenchmarkClassName() {
     75     try {
     76       ParsedOptions.from(new String[] {"a", "b"}, true);
     77       fail();
     78     } catch (InvalidCommandException expected) {
     79       assertEquals("Extra stuff, expected only class name: [a, b]", expected.getMessage());
     80     }
     81   }
     82 
     83   @Test public void testTooManyArguments_DoNotRequireBenchmarkClassName() {
     84     try {
     85       ParsedOptions.from(new String[] {"a", "b"}, false);
     86       fail();
     87     } catch (InvalidCommandException expected) {
     88       assertEquals("Extra stuff, did not expect non-option arguments: [a, b]",
     89           expected.getMessage());
     90     }
     91   }
     92 
     93   @Test public void testHelp() throws InvalidCommandException {
     94     try {
     95       ParsedOptions.from(new String[] {"--help"}, true);
     96       fail();
     97     } catch (DisplayUsageException expected) {
     98     }
     99   }
    100 
    101   @Test public void testDefaults_RequireBenchmarkClassName() throws InvalidCommandException {
    102     CaliperOptions options = ParsedOptions.from(new String[] {CLASS_NAME}, true);
    103 
    104     assertEquals(CLASS_NAME, options.benchmarkClassName());
    105     checkDefaults(options);
    106   }
    107 
    108   @Test public void testDefaults_DoNotRequireBenchmarkClassName() throws InvalidCommandException {
    109     CaliperOptions options = ParsedOptions.from(new String[] {}, false);
    110 
    111     assertNull(options.benchmarkClassName());
    112     checkDefaults(options);
    113   }
    114 
    115   private void checkDefaults(CaliperOptions options) {
    116     assertTrue(options.benchmarkMethodNames().isEmpty());
    117     assertFalse(options.dryRun());
    118     ImmutableSet<String> expectedInstruments = new ImmutableSet.Builder<String>()
    119         .add("allocation")
    120         .add("runtime")
    121         .build();
    122     assertEquals(expectedInstruments, options.instrumentNames());
    123     assertEquals(1, options.trialsPerScenario());
    124     assertTrue(options.userParameters().isEmpty());
    125     assertFalse(options.printConfiguration());
    126     assertTrue(options.vmArguments().isEmpty());
    127     assertEquals(0, options.vmNames().size());
    128   }
    129 
    130   @Test public void testKitchenSink() throws InvalidCommandException {
    131     String[] args = {
    132         "--benchmark=foo;bar;qux",
    133         "--instrument=testInstrument",
    134         "--directory=/path/to/some/dir",
    135         "--trials=2",
    136         "--time-limit=15s",
    137         "-Dx=a;b;c",
    138         "-Dy=b;d",
    139         "-Csome.property=value",
    140         "-Csome.other.property=other-value",
    141         "--print-config",
    142         "-JmemoryMax=-Xmx32m;-Xmx64m",
    143         "--vm=testVm",
    144         "--delimiter=;",
    145         CLASS_NAME,
    146     };
    147     CaliperOptions options = ParsedOptions.from(args, true);
    148 
    149     assertEquals(CLASS_NAME, options.benchmarkClassName());
    150     assertEquals(ImmutableSet.of("foo", "bar", "qux"), options.benchmarkMethodNames());
    151     assertFalse(options.dryRun());
    152     assertEquals(ImmutableSet.of("testInstrument"), options.instrumentNames());
    153     assertEquals(new File("/path/to/some/dir"), options.caliperDirectory());
    154     assertEquals(2, options.trialsPerScenario());
    155     assertEquals(ShortDuration.of(15, SECONDS), options.timeLimit());
    156     assertEquals(ImmutableSetMultimap.of("x", "a", "x", "b", "x", "c", "y", "b", "y", "d"),
    157         options.userParameters());
    158     assertEquals(ImmutableMap.of("some.property", "value", "some.other.property", "other-value"),
    159         options.configProperties());
    160     assertTrue(options.printConfiguration());
    161     assertEquals(ImmutableSetMultimap.of("memoryMax", "-Xmx32m", "memoryMax", "-Xmx64m"),
    162         options.vmArguments());
    163 
    164     String vmName = Iterables.getOnlyElement(options.vmNames());
    165     assertEquals("testVm", vmName);
    166   }
    167 
    168   public static class FakeBenchmark {}
    169 
    170   private static final String CLASS_NAME = FakeBenchmark.class.getName();
    171 }
    172