Home | History | Annotate | Download | only in command
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.tradefed.command;
     18 
     19 import com.android.tradefed.config.IConfiguration;
     20 import com.android.tradefed.config.IConfigurationFactory;
     21 import com.android.tradefed.device.DeviceSelectionOptions;
     22 import com.android.tradefed.device.IDeviceManager;
     23 import com.android.tradefed.device.MockDeviceManager;
     24 
     25 import junit.framework.TestCase;
     26 
     27 import org.easymock.EasyMock;
     28 
     29 /**
     30  * Longer running stress java app that stresses {@link CommandScheduler}.
     31  * <p/>
     32  * Intended to be executed under a profiler.
     33  */
     34 public class CommandSchedulerStressApp extends TestCase {
     35 
     36     /** the {@link CommandScheduler} under test, with all dependencies mocked out */
     37     private CommandScheduler mCommandScheduler;
     38     private IDeviceManager mMockDeviceManager;
     39     private IConfiguration mMockConfig;
     40     private IConfigurationFactory mMockConfigFactory;
     41     private CommandOptions mCommandOptions;
     42 
     43     public CommandSchedulerStressApp() throws Exception {
     44         mMockConfig = EasyMock.createNiceMock(IConfiguration.class);
     45         mMockDeviceManager = new MockDeviceManager(100);
     46         mMockConfigFactory = EasyMock.createMock(IConfigurationFactory.class);
     47         mCommandOptions = new CommandOptions();
     48         mCommandOptions.setLoopMode(false);
     49         mCommandOptions.setMinLoopTime(0);
     50         EasyMock.expect(mMockConfig.getCommandOptions()).andStubReturn(mCommandOptions);
     51         EasyMock.expect(mMockConfig.getDeviceRequirements()).andStubReturn(
     52                 new DeviceSelectionOptions());
     53 
     54         mCommandScheduler =
     55                 new CommandScheduler() {
     56                     @Override
     57                     protected IDeviceManager getDeviceManager() {
     58                         return mMockDeviceManager;
     59                     }
     60 
     61                     @Override
     62                     protected IConfigurationFactory getConfigFactory() {
     63                         return mMockConfigFactory;
     64                     }
     65                 };
     66     }
     67 
     68     /**
     69      * Test config priority scheduling under load with a large number of commands, when device
     70      * resources are scarce.
     71      * <p/>
     72      * Intended to verify that the device polling scheme used by the scheduler is not overly
     73      * expensive.
     74      * <p/>
     75      * Lacks automated verification - intended to be executed under a profiler.
     76      */
     77     public void testRunLoad() throws Exception {
     78         String[] configArgs = new String[] {"arrgs"};
     79 
     80         EasyMock.expect(
     81                 mMockConfigFactory.createConfigurationFromArgs(EasyMock.aryEq(configArgs)))
     82                 .andReturn(mMockConfig).anyTimes();
     83 
     84         EasyMock.replay(mMockConfig, mMockConfigFactory);
     85         mCommandScheduler.start();
     86 
     87         for (int i=0; i < 10000; i++) {
     88             mCommandScheduler.addCommand(configArgs);
     89         }
     90 
     91         // let scheduler churn through the commands for 30 seconds
     92         Thread.sleep(30 * 1000);
     93         mCommandScheduler.shutdown();
     94         mCommandScheduler.join();
     95     }
     96 
     97     public static void main(String[] args) {
     98         try {
     99             long startTime = System.currentTimeMillis();
    100             CommandSchedulerStressApp stressApp = new CommandSchedulerStressApp();
    101             stressApp.testRunLoad();
    102             System.out.printf("Stress app ran for %s ms", System.currentTimeMillis() - startTime);
    103         } catch (Exception e) {
    104             e.printStackTrace();
    105         }
    106     }
    107 }
    108