<s> import os <EOL> import re <EOL> import tempfile <EOL> import json <EOL> from collections import defaultdict <EOL> from wlauto import AndroidUiAutoBenchmark , Parameter , Artifact <EOL> from wlauto . exceptions import ConfigError , WorkloadError <EOL> from wlauto . utils . misc import capitalize <EOL> import wlauto . common . android . resources <EOL> class Geekbench ( AndroidUiAutoBenchmark ) : <EOL> name = '<STR_LIT>' <EOL> description = """<STR_LIT>""" <EOL> summary_metrics = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> versions = { <EOL> '<STR_LIT:3>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> '<STR_LIT:2>' : { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> } , <EOL> } <EOL> begin_regex = re . compile ( r'<STR_LIT>' <EOL> r'<STR_LIT>' ) <EOL> replace_regex = re . compile ( r'<STR_LIT>' ) <EOL> parameters = [ <EOL> Parameter ( '<STR_LIT:version>' , default = sorted ( versions . keys ( ) ) [ - <NUM_LIT:1> ] , allowed_values = sorted ( versions . keys ( ) ) , <EOL> description = '<STR_LIT>' ) , <EOL> Parameter ( '<STR_LIT>' , kind = int , default = <NUM_LIT:1> , <EOL> description = ( '<STR_LIT>' <EOL> '<STR_LIT>' ) ) , <EOL> ] <EOL> @ property <EOL> def activity ( self ) : <EOL> return self . versions [ self . version ] [ '<STR_LIT>' ] <EOL> @ property <EOL> def package ( self ) : <EOL> return self . versions [ self . version ] [ '<STR_LIT>' ] <EOL> def __init__ ( self , device , ** kwargs ) : <EOL> super ( Geekbench , self ) . __init__ ( device , ** kwargs ) <EOL> self . uiauto_params [ '<STR_LIT:version>' ] = self . version <EOL> self . uiauto_params [ '<STR_LIT>' ] = self . times <EOL> self . run_timeout = <NUM_LIT:5> * <NUM_LIT> * self . times <EOL> def initialize ( self , context ) : <EOL> if self . version == '<STR_LIT:3>' and not self . device . is_rooted : <EOL> raise WorkloadError ( '<STR_LIT>' ) <EOL> def init_resources ( self , context ) : <EOL> self . apk_file = context . resolver . get ( wlauto . common . android . resources . ApkFile ( self ) , version = self . version ) <EOL> self . uiauto_file = context . resolver . get ( wlauto . common . android . resources . JarFile ( self ) ) <EOL> self . device_uiauto_file = self . device . path . join ( self . device . working_directory , <EOL> os . path . basename ( self . uiauto_file ) ) <EOL> if not self . uiauto_package : <EOL> self . uiauto_package = os . path . splitext ( os . path . basename ( self . uiauto_file ) ) [ <NUM_LIT:0> ] <EOL> def update_result ( self , context ) : <EOL> super ( Geekbench , self ) . update_result ( context ) <EOL> update_method = getattr ( self , '<STR_LIT>' . format ( self . version ) ) <EOL> update_method ( context ) <EOL> def validate ( self ) : <EOL> if ( self . times > <NUM_LIT:1> ) and ( self . version == '<STR_LIT:2>' ) : <EOL> raise ConfigError ( '<STR_LIT>' ) <EOL> def update_result_2 ( self , context ) : <EOL> score_calculator = GBScoreCalculator ( ) <EOL> score_calculator . parse ( self . logcat_log ) <EOL> score_calculator . update_results ( context ) <EOL> def update_result_3 ( self , context ) : <EOL> outfile_glob = self . device . path . join ( self . device . package_data_directory , self . package , '<STR_LIT>' , '<STR_LIT>' ) <EOL> on_device_output_files = [ f . strip ( ) for f in <EOL> self . device . execute ( '<STR_LIT>' . format ( outfile_glob ) , as_root = True ) . split ( '<STR_LIT:\n>' ) ] <EOL> for i , on_device_output_file in enumerate ( on_device_output_files ) : <EOL> host_temp_file = tempfile . mktemp ( ) <EOL> self . device . pull_file ( on_device_output_file , host_temp_file ) <EOL> host_output_file = os . path . join ( context . output_directory , os . path . basename ( on_device_output_file ) ) <EOL> with open ( host_temp_file ) as fh : <EOL> data = json . load ( fh ) <EOL> os . remove ( host_temp_file ) <EOL> with open ( host_output_file , '<STR_LIT:w>' ) as wfh : <EOL> json . dump ( data , wfh , indent = <NUM_LIT:4> ) <EOL> context . iteration_artifacts . append ( Artifact ( '<STR_LIT>' , path = os . path . basename ( on_device_output_file ) , <EOL> kind = '<STR_LIT:data>' , <EOL> description = '<STR_LIT>' ) ) <EOL> context . result . add_metric ( namemify ( '<STR_LIT>' , i ) , data [ '<STR_LIT>' ] ) <EOL> context . result . add_metric ( namemify ( '<STR_LIT>' , i ) , data [ '<STR_LIT>' ] ) <EOL> for section in data [ '<STR_LIT>' ] : <EOL> context . result . add_metric ( namemify ( section [ '<STR_LIT:name>' ] + '<STR_LIT>' , i ) , section [ '<STR_LIT>' ] ) <EOL> context . result . add_metric ( namemify ( section [ '<STR_LIT:name>' ] + '<STR_LIT>' , i ) , <EOL> section [ '<STR_LIT>' ] ) <EOL> class GBWorkload ( object ) : <EOL> """<STR_LIT>""" <EOL> categories = [ None , '<STR_LIT>' , '<STR_LIT:float>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> pmac_g5_base_score = <NUM_LIT:1000> <EOL> units_conversion_map = { <EOL> '<STR_LIT>' : <NUM_LIT:1> , <EOL> '<STR_LIT:M>' : <NUM_LIT:1000> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> } <EOL> def __init__ ( self , wlid , name , pmac_g5_st_score , pmac_g5_mt_score ) : <EOL> """<STR_LIT>""" <EOL> self . wlid = wlid <EOL> self . name = name <EOL> self . pmac_g5_st_score = pmac_g5_st_score <EOL> self . pmac_g5_mt_score = pmac_g5_mt_score <EOL> self . category = self . categories [ int ( wlid ) // <NUM_LIT:100> ] <EOL> self . collected_results = [ ] <EOL> def add_result ( self , value , units ) : <EOL> self . collected_results . append ( self . convert_to_kilo ( value , units ) ) <EOL> def convert_to_kilo ( self , value , units ) : <EOL> return value * self . units_conversion_map [ units [ <NUM_LIT:0> ] ] <EOL> def clear ( self ) : <EOL> self . collected_results = [ ] <EOL> def get_scores ( self ) : <EOL> """<STR_LIT>""" <EOL> no_of_results = len ( self . collected_results ) <EOL> if no_of_results == <NUM_LIT:4> : <EOL> return ( self . _calculate ( self . collected_results [ : <NUM_LIT:4> ] , self . pmac_g5_st_score ) , None ) <EOL> if no_of_results == <NUM_LIT:8> : <EOL> return ( self . _calculate ( self . collected_results [ : <NUM_LIT:4> ] , self . pmac_g5_st_score ) , <EOL> self . _calculate ( self . collected_results [ <NUM_LIT:4> : ] , self . pmac_g5_mt_score ) ) <EOL> else : <EOL> msg = '<STR_LIT>' . format ( no_of_results , self . name ) <EOL> msg += '<STR_LIT>' <EOL> raise WorkloadError ( msg ) <EOL> def _calculate ( self , values , scale_factor ) : <EOL> return max ( values ) * self . pmac_g5_base_score / scale_factor <EOL> def __str__ ( self ) : <EOL> return self . name <EOL> __repr__ = __str__ <EOL> class GBScoreCalculator ( object ) : <EOL> """<STR_LIT>""" <EOL> result_regex = re . compile ( r'<STR_LIT>' <EOL> r'<STR_LIT>' ) <EOL> category_weights = { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT:float>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> } <EOL> workloads = [ <EOL> GBWorkload ( <NUM_LIT> , '<STR_LIT>' , <NUM_LIT> , <NUM_LIT> ) , <EOL> GBWorkload ( <NUM_LIT> , '<STR_LIT>' , <NUM_LIT> , <NUM_LIT> ) , <EOL> GBWorkload ( <NUM_LIT> , '<STR_LIT>' , <NUM_LIT> , <NUM_LIT> ) , <EOL> GBWorkload ( <NUM_LIT> , '<STR_LIT>' , <NUM_LIT> , <NUM_LIT> ) , <EOL> GBWorkload ( <NUM_LIT> , '<STR_LIT>' , <NUM_LIT> , <NUM_LIT> ) , <EOL> GBWorkload ( <NUM_LIT> , '<STR_LIT>' , <NUM_LIT> , <NUM_LIT> ) , <EOL> GBWorkload ( <NUM_LIT> , '<STR_LIT>' , <NUM_LIT> , <NUM_LIT> ) , <EOL> GBWorkload ( <NUM_LIT> , '<STR_LIT>' , <NUM_LIT> , <NUM_LIT> ) , <EOL> GBWorkload ( <NUM_LIT> , '<STR_LIT>' , <NUM_LIT> , <NUM_LIT> ) , <EOL> GBWorkload ( <NUM_LIT> , '<STR_LIT>' , <NUM_LIT> , <NUM_LIT> ) , <EOL> GBWorkload ( <NUM_LIT> , '<STR_LIT>' , <NUM_LIT> , <NUM_LIT> ) , <EOL> GBWorkload ( <NUM_LIT> , '<STR_LIT>' , <NUM_LIT> , <NUM_LIT> ) , <EOL> GBWorkload ( <NUM_LIT> , '<STR_LIT>' , <NUM_LIT> , None ) , <EOL> GBWorkload ( <NUM_LIT> , '<STR_LIT>' , <NUM_LIT> , None ) , <EOL> GBWorkload ( <NUM_LIT> , '<STR_LIT>' , <NUM_LIT> , None ) , <EOL> GBWorkload ( <NUM_LIT> , '<STR_LIT>' , <NUM_LIT> , None ) , <EOL> GBWorkload ( <NUM_LIT> , '<STR_LIT>' , <NUM_LIT> , None ) , <EOL> GBWorkload ( <NUM_LIT> , '<STR_LIT>' , <NUM_LIT> , None ) , <EOL> GBWorkload ( <NUM_LIT> , '<STR_LIT>' , <NUM_LIT> , None ) , <EOL> GBWorkload ( <NUM_LIT> , '<STR_LIT>' , <NUM_LIT> , None ) , <EOL> GBWorkload ( <NUM_LIT> , '<STR_LIT>' , <NUM_LIT> , None ) , <EOL> ] <EOL> def __init__ ( self ) : <EOL> self . workload_map = { wl . wlid : wl for wl in self . workloads } <EOL> def parse ( self , filepath ) : <EOL> """<STR_LIT>""" <EOL> for wl in self . workloads : <EOL> wl . clear ( ) <EOL> with open ( filepath ) as fh : <EOL> for line in fh : <EOL> match = self . result_regex . search ( line ) <EOL> if match : <EOL> wkload = self . workload_map [ int ( match . group ( '<STR_LIT:id>' ) ) ] <EOL> wkload . add_result ( float ( match . group ( '<STR_LIT:value>' ) ) , match . group ( '<STR_LIT>' ) ) <EOL> def update_results ( self , context ) : <EOL> """<STR_LIT>""" <EOL> scores_by_category = defaultdict ( list ) <EOL> for wkload in self . workloads : <EOL> st_score , mt_score = wkload . get_scores ( ) <EOL> scores_by_category [ wkload . category ] . append ( st_score ) <EOL> context . result . add_metric ( wkload . name + '<STR_LIT>' , int ( st_score ) ) <EOL> if mt_score is not None : <EOL> scores_by_category [ wkload . category ] . append ( mt_score ) <EOL> context . result . add_metric ( wkload . name + '<STR_LIT>' , int ( mt_score ) ) <EOL> overall_score = <NUM_LIT:0> <EOL> for category in scores_by_category : <EOL> scores = scores_by_category [ category ] <EOL> category_score = sum ( scores ) / len ( scores ) <EOL> overall_score += category_score * self . category_weights [ category ] <EOL> context . result . add_metric ( capitalize ( category ) + '<STR_LIT>' , int ( category_score ) ) <EOL> context . result . add_metric ( '<STR_LIT>' , int ( overall_score ) ) <EOL> def namemify ( basename , i ) : <EOL> return basename + ( '<STR_LIT>' . format ( i ) if i else '<STR_LIT>' ) </s>
