Home | History | Annotate | Download | only in shell_performance
      1 #!/usr/bin/env python
      2 #
      3 # Copyright (C) 2018 The Android Open Source Project
      4 #
      5 # Licensed under the Apache License, Version 2.0 (the "License");
      6 # you may not use this file except in compliance with the License.
      7 # You may obtain a copy of the License at
      8 #
      9 #      http://www.apache.org/licenses/LICENSE-2.0
     10 #
     11 # Unless required by applicable law or agreed to in writing, software
     12 # distributed under the License is distributed on an "AS IS" BASIS,
     13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14 # See the License for the specific language governing permissions and
     15 # limitations under the License.
     16 #
     17 
     18 import logging
     19 
     20 from vts.runners.host import asserts
     21 from vts.runners.host import base_test
     22 from vts.runners.host import const
     23 from vts.runners.host import test_runner
     24 import time
     25 
     26 
     27 class VtsSelfTestShellPerformance(base_test.BaseTestClass):
     28     '''A simple performance test to compare adb shell and VTS shell.'''
     29 
     30     def setUpClass(self):
     31         # Since we are running the actual test cases, run_as_vts_self_test
     32         # must be set to False.
     33         self.run_as_vts_self_test = False
     34 
     35         self.dut = self.android_devices[0]
     36         self.shell = self.dut.shell
     37 
     38     def VtsShell(self, cmd, n):
     39         '''Execute a command for n times via VTS shell.
     40 
     41         Args:
     42             cmd: string, command to execute
     43             n: int, number of repeated calls
     44         '''
     45         for i in range(n):
     46             self.shell.Execute(cmd)
     47 
     48     def AdbShell(self, cmd, n):
     49         '''Execute a command for n times via ADB shell.
     50 
     51         Args:
     52             cmd: string, command to execute
     53             n: int, number of repeated calls
     54         '''
     55         for i in range(n):
     56             self.dut.adb.shell(cmd)
     57 
     58     def VtsShellList(self, cmd, n):
     59         '''Execute a command for n times via VTS shell as a list.
     60 
     61         Args:
     62             cmd: string, command to execute
     63             n: int, number of repeated calls
     64         '''
     65         self.shell.Execute([cmd] * n)
     66 
     67     def Measure(self, func, *args):
     68         '''Measure time lapsed when executing a function.
     69 
     70         Args:
     71             func: function, function to execute
     72             *args: list of arguments for the functions
     73         '''
     74         start = time.time()
     75         func(*args)
     76         return time.time() - start
     77 
     78     def testPerformance(self):
     79         '''Run a empty test case on device for 100 times and log the times.'''
     80 
     81         cmd = "/data/local/tmp/zero_testcase"
     82 
     83         # First call to eliminate caching effects
     84         self.AdbShell(cmd, 1)
     85         self.VtsShell(cmd, 1)
     86 
     87         repeats = 100
     88 
     89         adb_time = self.Measure(self.AdbShell, cmd, repeats)
     90         vts_time = self.Measure(self.VtsShell, cmd, repeats)
     91         vts_list_time = self.Measure(self.VtsShellList, cmd, repeats)
     92 
     93         logging.info("adb shell for 100 times = %s", adb_time)
     94         logging.info("vts shell for 100 times = %s", vts_time)
     95         logging.info("vts shell with for 100 times = %s", vts_list_time)
     96 
     97 
     98 if __name__ == "__main__":
     99     test_runner.main()
    100