Best Python code snippet using Airtest
generate_method_whitelist.py
Source:generate_method_whitelist.py
1# Copyright 2015 The Chromium Authors. All rights reserved.2# Use of this source code is governed by a BSD-style license that can be3# found in the LICENSE file.4#5# Script to generate mods/android/frameworks/base/compiled-methods.6"""7In order to generate all the files needed to run this script:8* Obtain the list of compilable methods:9 - Compile ARC in debug mode without method whitelist:10 `./configure -t=nx --disable-method-whitelist && ninja -j200`11 - Extract the symbols from boot.oat:12 `nm -a out/target/nacl_x86_64_dbg_intermediates/boot_art/boot.oat` and save13 that to a file.14* Obtain the list of methods used during boot:15 - Compile ARC without AOT:16 `./configure -t=nx --disable-art-aot --logging=java-methods && ninja -j200`17 - Run HelloAndroid and redirect stderr to a file.18* Profile several Android applications:19 - Compile ARC in release mode:20 `./configure -t=nx --official-build && ninja -j200`21 - Follow `docs/profiling.md`, "Profiling Java code" section to obtain22 profiles and process the results.23* After compiling boot.oat with the updated whitelist, be sure to update the24 address at which the image will be loaded in mods/android/art/config.py.25"""26import argparse27import collections28import re29import sys30_JAVA_METHOD_ENTER = re.compile('^java_methods:\s+\d+\s+->\s+(.*?)$')31_SYMBOL_RE = re.compile(r'^([0-9a-f]+) T (.*?)(?: \[ DEDUPED \])?$')32_TRACE_RE = re.compile(r'^\s+(\d+)\s+(\d+\.\d+)\s+(\d+\.\d+)\s+\[\d+\]\s+' +33 r'([^ \t]+)\s+\(([^)]*)\)([^ \t]+)')34_TYPE_SIGNATURE_TO_JAVA_TYPE = {'B': 'byte',35 'C': 'char',36 'D': 'double',37 'F': 'float',38 'I': 'int',39 'J': 'long',40 'S': 'short',41 'V': 'void',42 'Z': 'boolean'}43def _parse_java_type_signature(s, idx=0):44 type_signature = s[idx]45 java_type = _TYPE_SIGNATURE_TO_JAVA_TYPE.get(type_signature)46 if java_type is not None:47 return (java_type, idx + 1)48 elif type_signature == 'L':49 semicolon = s.find(';', idx)50 return (s[idx + 1:semicolon].replace('/', '.'), semicolon + 1)51 else:52 assert type_signature == '[', s53 result, idx = _parse_java_type_signature(s, idx + 1)54 return (result + '[]', idx)55def _parse_java_type_signature_list(s):56 typelist = []57 idx = 058 l = len(s)59 while idx < l:60 typename, idx = _parse_java_type_signature(s, idx)61 typelist.append(typename)62 return typelist63def _read_symbol_file(symbol_file):64 method_addresses = collections.defaultdict(list)65 compilable_methods = set()66 with open(symbol_file) as f:67 for line in f:68 address, method_name = _SYMBOL_RE.match(line).groups()69 compilable_methods.add(method_name)70 method_addresses[address].append(method_name)71 return compilable_methods, method_addresses72def _deduplicate_methods(method_addresses):73 """Obtain a set of all de-duplicated methods.74 ART has a mechanism to save space by emitting identical code just once and75 making all symbols/callers point to that version. That means that for no76 additional cost, we can have more methods be compiled. In practice, only about77 10% of the methods returned by this function will actually emit code, for a78 grand total of about 1MB."""79 compiled_methods = set()80 for address, method_names in method_addresses.iteritems():81 if len(method_names) > 1:82 # This function only considers duplicated methods, so an address should83 # have more than one assigned methods to be considered in this step.84 compiled_methods.update(method_names)85 return compiled_methods86def _read_boot_methods(boot_methods_file):87 """Parse a logfile to obtain all methods called during boot.88 Given that ARC tries to optimize boot time significantly, every single Java89 function that is called during boot time should be compiled (if possible).90 This parses a log, generated with --logging=java-methods, to create a set of91 methods that will always be invoked no matter the application being run."""92 boot_methods = set()93 with open(boot_methods_file) as f:94 for line in f:95 match = _JAVA_METHOD_ENTER.match(line)96 if not match:97 continue98 boot_methods.add(match.group(1))99 return boot_methods100def _read_profile(profile):101 """Read an Android profile summary to optimize for costly methods.102 In addition to optimize for boot time, some expensive runtime methods should103 also be included in the boot image. Currently we can add all methods found in104 all profiles without going above the size budget."""105 profile_methods = set()106 with open(profile) as f:107 for line in f:108 match = _TRACE_RE.match(line)109 if not match:110 continue111 calls, exclusive, aggregate, name, params, return_type = match.groups()112 calls = int(calls)113 exclusive = float(exclusive)114 aggregate = float(aggregate)115 params = _parse_java_type_signature_list(params)116 return_type = _parse_java_type_signature_list(return_type)[0]117 profile_methods.add('%s %s(%s)' % (return_type, name, ', '.join(params)))118 return profile_methods119def _write_methods(output, methods, name, emitted_methods):120 filtered_methods = (methods - emitted_methods)121 if filtered_methods:122 print >> output, '# %s' % name123 for method in sorted(filtered_methods):124 print >> output, method125 emitted_methods.update(filtered_methods)126def _generate_whitelist_file(parsed_args):127 print >> parsed_args.output, '# Generated with generate_method_whitelist.py.'128 print >> parsed_args.output, '# Do not edit.'129 compilable_methods, method_addresses = _read_symbol_file(130 parsed_args.symbol_file)131 emitted_methods = set()132 deduplicated_methods = _deduplicate_methods(method_addresses)133 _write_methods(parsed_args.output, deduplicated_methods,134 'Deduplicated methods', emitted_methods)135 boot_methods = _read_boot_methods(parsed_args.boot_methods_file)136 _write_methods(parsed_args.output, boot_methods & compilable_methods,137 'Boot methods', emitted_methods)138 profile_methods = set()139 for profile in parsed_args.profiles:140 profile_methods |= _read_profile(profile)141 _write_methods(parsed_args.output, profile_methods & compilable_methods,142 'Profile methods', emitted_methods)143def _open_output(value):144 return open(value, 'w')145def _parse_args(argv):146 parser = argparse.ArgumentParser()147 parser.add_argument('--output', default=sys.stdout, type=_open_output,148 help='Write the generated whitelist to a file. '149 'The default is stdout')150 parser.add_argument('symbol_file', metavar='<symbol-file>',151 help='A file with all the symbols in boot.oat. This can '152 'be generated by compiling ARC in debug mode and then '153 'running `nm -a <path to>/boot.oat`.')154 parser.add_argument('boot_methods_file', metavar='<boot-methods-file>',155 help='A file with all the methods called during boot. '156 'This can be generated by compiling ARC with the '157 '--disable-art-aot and --logging=java-methods flags '158 'and then launching HelloAndroid.')159 parser.add_argument('profiles', metavar='<profile>', nargs='*', default=[],160 help='A file with a Java profile for an application. '161 'This can be generated by compiling ARC in release mode '162 'and launching it with the --java-trace-startup=<N> flag.'163 'The trace then needs to be grabbed from the device '164 'with `adb` and the trace needs to be processed with '165 '`dmtracedump`. See `docs/profiling.md` for more info.')166 return parser.parse_args(argv)167def main():168 parsed_args = _parse_args(sys.argv[1:])169 return _generate_whitelist_file(parsed_args)170if __name__ == '__main__':...
curtailment.py
Source:curtailment.py
1import pandas as pd2from powersimdata.scenario.scenario import Scenario3default_pmin_dict = {4 "coal": None,5 "dfo": 0,6 "geothermal": 0.95,7 "hydro": None,8 "ng": 0,9 "nuclear": 0.95,10 "other": 0,11 "solar": 0,12 "wind": 0,13 "wind_offshore": 0,14}15profile_methods = {16 "hydro": "get_hydro",17 "solar": "get_solar",18 "wind": "get_wind",19 "wind_offshore": "get_wind",20}21def temporal_curtailment(22 scenario, pmin_by_type=None, pmin_by_id=None, curtailable={"solar", "wind"}23):24 """Calculate the minimum share of potential renewable energy that will be curtailed25 due to supply/demand mismatch, assuming no storage is present.26 :param powersimdata.scenario.scenario.Scenario scenario: scenario instance.27 :param dict/pandas.Series pmin_by_type: Mapping of types to Pmin assumptions. Values28 between 0 and 1 (inclusive) are treated as shares of Pmax, and None values29 either maintain given Pmin values (for dispatchable resources) or track profiles30 (for profile resources, e.g. hydro).31 :param dict/pandas.Series pmin_by_id: Mapping of IDs to Pmin assumptions, as an32 override to the default behavior for that plant type. Values between 0 and 133 (inclusive) are treated as shares of Pmax, and None values either maintain given34 Pmin values (for dispatchable resources) or track profiles35 (for profile resources, e.g. hydro).36 :param iterable curtailable: resource types which can be curtailed.37 :return: (*float*) -- share of curtailable resources that will be curtailed.38 :raises TypeError: if inputs do not mach specified type.39 :raises ValueError: if any entries in curtailable or keys in pmin_by_type are not40 types in the grid, any keys in pmin_by_id are not plant IDs in the Grid,41 or any values in pmin_by_type/pmin_by_id are not in the range [0, 1] or None.42 """43 if not isinstance(scenario, Scenario):44 raise TypeError("scenario must be a Scenario")45 if pmin_by_type is None:46 pmin_by_type = {}47 if pmin_by_id is None:48 pmin_by_id = {}49 check_dicts = {"pmin_by_id": pmin_by_id, "pmin_by_type": pmin_by_type}50 for name, d in check_dicts.items():51 if not isinstance(d, (dict, pd.Series)):52 raise TypeError(f"{name} must be a dict or pandas Series")53 # Access values via appropriate method whether d is a dict or a pandas Series54 values = d.values() if isinstance(d, dict) else d.values55 if not all([v is None or 0 <= v <= 1 for v in values]):56 err_msg = f"all entries in {name} must be None or in the range [0, 1]"57 raise ValueError(err_msg)58 plant = scenario.state.get_grid().plant59 valid_types = plant["type"].unique()60 if not set(pmin_by_type.keys()) <= set(valid_types):61 raise ValueError("Got invalid plant type as a key to pmin_by_type")62 if not set(pmin_by_id.keys()) <= set(plant.index):63 raise ValueError("Got invalid plant id as a key to pmin_by_id")64 try:65 if not set(curtailable) <= set(valid_types):66 raise ValueError("Got invalid plant type within curtailable")67 except TypeError:68 raise TypeError("curtailable must be an iterable")69 # Get profiles, filter out plant-level overrides, then sum70 all_profiles = pd.concat(71 [getattr(scenario.state, m)() for m in set(profile_methods.values())], axis=172 )73 plant_id_mask = ~plant.index.isin(pmin_by_id.keys())74 base_plant_ids_by_type = plant.loc[plant_id_mask].groupby("type").groups75 valid_profile_types = set(base_plant_ids_by_type) & set(profile_methods)76 plant_ids_for_summed_profiles = set().union(77 *[set(base_plant_ids_by_type[g]) for g in valid_profile_types]78 )79 summed_profiles = (80 all_profiles[list(plant_ids_for_summed_profiles)]81 .groupby(plant["type"], axis=1)82 .sum()83 )84 # Build up a series of firm generation85 summed_demand = scenario.state.get_demand().sum(axis=1)86 firm_generation = pd.Series(0, index=summed_demand.index)87 # Add plants without plant-level overrides ('base' plants)88 pmin_dict = {**default_pmin_dict, **pmin_by_type}89 # Don't iterate over plant types not present in this grid90 pmin_dict = {k: pmin_dict[k] for k in base_plant_ids_by_type}91 for resource, pmin in pmin_dict.items():92 if (resource in curtailable) or (pmin == 0):93 continue94 if pmin is None:95 if resource in profile_methods:96 firm_generation += summed_profiles[resource]97 else:98 summed_pmin = plant.Pmin.loc[base_plant_ids_by_type[resource]].sum()99 firm_generation += pd.Series(summed_pmin, index=summed_demand.index)100 else:101 summed_pmin = pmin * plant.Pmax.loc[base_plant_ids_by_type[resource]].sum()102 firm_generation += pd.Series(summed_pmin, index=summed_demand.index)103 # Add plants with plant-level overrides104 for plant_id, pmin in pmin_by_id.items():105 if pmin == 0:106 continue107 if pmin is None:108 if plant.loc[plant_id, "type"] in profile_methods:109 firm_generation += all_profiles[plant_id]110 else:111 plant_pmin = plant.loc[plant_id, "Pmin"]112 firm_generation += pd.Series(plant_pmin, index=summed_demand.index)113 else:114 plant_pmin = plant.loc[plant_id, "Pmax"] * pmin115 firm_generation += pd.Series(plant_pmin, index=summed_demand.index)116 # Finally, compare this summed firm generation against summed curtailable generation117 total_curtailable = summed_profiles[list(curtailable)].sum(axis=1)118 net_demand = summed_demand - firm_generation119 curtailable_max_gen = pd.concat([net_demand, total_curtailable], axis=1).min(axis=1)120 curtailment_fraction = 1 - curtailable_max_gen.sum() / total_curtailable.sum()...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!