def run_benchmarks(): # type: () -> None n = 100000 # C++ exceptions are slower than Python! Woah. result = '' num_exceptions = 0 i = 0 # Even one failure makes C++ slower! Otherwise it's faster. cases = ['fail', 'ok', 'ok', 'ok'] # 870 ms in C++, 366 ms in Python #cases = ['fail', 'fail', 'fail', 'fail'] # 26 ms in C++, 70 ms in Python # OK it is inverted! Exceptions are really expensive. #cases = ['ok', 'ok', 'ok', 'ok'] while i < n: for prog in cases: try: result = f(prog) except ParseError as e: num_exceptions += 1 continue i += 1 log('num_exceptions = %d', num_exceptions) log('Ran %d iterations of try/except', n)
def IntDemo(): # type: () -> None intlist = [] # type: List[int] # NOTE: this annotation is required, or MyPy has a partial type. # I thoguht it would be able to infer it from expressions below. intlist.append(1) intlist.append(2) intlist.append(3) log("len(intlist) = %d", len(intlist)) for i in intlist: log("i = %d", i) strlist = [] # type: List[str] strlist.append('a') strlist.append('b') log("len(strlist) = %d", len(strlist)) for s in strlist: log("s = %s", s) #strlist.pop() #strlist.pop() x = strlist.pop() log("len(strlist) = %d", len(strlist))
def run_tests(): # type: () -> None log("escaped: %s", escape('<html>', True)) # TODO: Fix double escaping here s = 'xo--xo' log("%s\n", s.replace('xo', 'OX'))
def run_tests(): # type: () -> None x = 33 # NOTE: This is very slow and should be separated result = fib_recursive(x) log('fib_recursive(%d) = %d', x, result)
def TupleDemo(): # type: () -> None t2 = (3, 'hello') # type: Tuple[int, str] # Destructuring myint, mystr = t2 log('myint = %d', myint) log('mystr = %s', mystr)
def run_tests(): # type: () -> None # TODO: xrange() for i in [1, 2, 3, 4, 5]: IfDemo(i) log('') ExceptDemo()
def run_benchmarks(): # type: () -> None i = 0 n = 2000000 result = 0 while i < n: result += module1.fortytwo() i = i + 1 log('result = %d', result)
def run_tests(): # type: () -> None log("result: %s", BackslashEscape('echo *.[ch] *.?', GLOB_META_CHARS)) # 200K iterations takes ~433 ms in Python, and ~311 ms in C with -O2 (~600 ms # with -O0) The algorithm is very inefficient. There are many tiny objects # being allocated. TestNotIn()
def ExceptDemo(): # type: () -> None result = '' for prog in ['foo', 'bar']: try: result = f(prog) except ParseError as e: log('error: %s', e.reason) continue log('result = %s', result)
def init_handler(file, func): try: func_handler = get_func_handler(file, func) except Exception as ex: runtime.log('get user function[%s:%s] failed' % (file, func)) runtime.log(str(ex)) global init_handler_fault init_handler_fault = str(ex) return None, None return HttpHandler(func_handler), EventHandler(func_handler)
def run_benchmarks(): # type: () -> None n = 1000000 i = 0 intlist = [] # type: List[int] strlist = [] # type: List[str] while i < n: intlist.append(i) strlist.append("foo") i += 1 log('Appended %d items to 2 lists', n)
def run_benchmarks(): # type: () -> None n = 1 # Just one iteration is enough x = 33 result = -1 i = 0 while i < n: result = fib_recursive(x) i += 1 log('fib_recursive(%d) = %d', x, result)
def run_tests(): # type: () -> None f = cStringIO.StringIO() for i in xrange(100): f.write(str(i)) contents = f.getvalue() log('Wrote %d bytes to StringIO', len(contents)) log('contents = %s ... %s', contents[:10], contents[-10:]) f = sys.stdout f.write('stdout\n')
def get_func_handler(mname, fname): need_del_module = [] try: for item in sys.modules: if item not in module_save: need_del_module.append(item) for item in need_del_module: del sys.modules[item] except Exception as ex: runtime.log("del old module err", str(ex)) module = imp.load_source("", mname + ".py") return getattr(module, fname)
def run_benchmarks(): # type: () -> None n = 500000 x = 33 result = -1 i = 0 while i < n: result = fib_iter(x) i += 1 log('fib_iter(%d) = %d', x, result) log('Ran %d iterations of fib_iter', n)
def get_func_handler(mname, fname): need_del_module = [] try: for item in sys.modules: if item not in module_save: need_del_module.append(item) for item in need_del_module: del sys.modules[item] except Exception as ex: runtime.log("del old module err" % str(ex)) module_spec = importlib.util.spec_from_file_location("", mname + ".py") module = importlib.util.module_from_spec(module_spec) module_spec.loader.exec_module(module) return getattr(module, fname)
def run_benchmarks(): # type: () -> None n = 200000 result = 0 i = 0 while i < n: lex = Lexer('a*b*3*4') p = Parser(lex) tree = p.Parse() i += 1 log('result = %d', result) log('iterations = %d', n)
def run_benchmarks(): # type: () -> None n = 500000 x = 33 result = -1 f = cStringIO.StringIO() out = TextOutput(f) i = 0 while i < n: out.write('foo\n') i += 1 log('Ran %d iterations', n) log('Wrote %d bytes', out.num_chars)
def run_benchmarks(): # type: () -> None n = 10000 result = 0 i = 0 while i < n: f = cStringIO.StringIO() for j in xrange(100): f.write(str(j)) result += len(f.getvalue()) i += 1 log('Ran %d iterations', n) log('result = %d', result)
def run_tests(): # type: () -> None lex = Lexer('abc') while True: tok_type, tok_val = lex.Read() if tok_type == tok_e.Eof: break #print('%s %s' % (tok_type, tok_val)) log('tok_val %s', tok_val) CASES = [ '1+2', '1+2*3', '1*2+3', '(1+2)*3', 'a+b+c+d', 'a*b*3*4', # expect errors here: '(', ')', '(a+b', ' ', ' $$ ', ] for expr in CASES: lex = Lexer(expr) p = Parser(lex) log('') log('--') log('%s =>', expr) tree = None # type: Optional[expr_t] try: tree = p.Parse() except ParseError as e: log('Parse error: %s', e.msg) continue #log('%s', tree) pretty_tree = tree.AbbreviatedTree() #ast_f = fmt.TextOutput(sys.stdout) ast_f = fmt.AnsiOutput(sys.stdout) fmt.PrintTree(pretty_tree, ast_f) ast_f.write('\n')
def get_func_handler(mname, fname): need_del_module = [] try: for item in sys.modules: if item not in module_save: need_del_module.append(item) for item in need_del_module: del sys.modules[item] except Exception as ex: runtime.log("del old module err" + str(ex)) # change the current working directory current_path = os.path.dirname(mname + ".py") os.chdir(current_path) runtime.log('working directory: %s' % os.getcwd()) module = imp.load_source("", mname + ".py") return getattr(module, fname)
def get_func_handler(mname, fname): need_del_module = [] try: for item in sys.modules: if item not in module_save: need_del_module.append(item) for item in need_del_module: del sys.modules[item] except Exception as ex: runtime.log("del old module err" % str(ex)) # change the current working directory current_path = os.path.dirname(mname+".py") os.chdir(current_path) runtime.log('working directory: %s' % os.getcwd()) module_spec = importlib.util.spec_from_file_location("", mname+".py") module = importlib.util.module_from_spec(module_spec) module_spec.loader.exec_module(module) return getattr(module, fname)
def run_benchmarks(): # type: () -> None n = 1000000 mystr = 'abcd' mylist = ['w', 'x', 'y', 'z'] result = 0 i = 0 while i < n: # C++ has a big advantage here #result += len(mystr) #result += len(mylist) # C++ shows no real advantage here result += len(mystr[1:]) result += len(mylist[1:]) i += 1 log('result = %d', result) log('iterations = %d', n)
def TestMaybeStrEquals(): # type: () -> None a = 'foo' b = 'bar' x = '' # type: Optional[str] # TODO: Conditionally assigning x = None doesn't work. log('a == b -> %d', a == b) log('a != b -> %d', a != b) log('a == x -> %d', a == x) log('a != x -> %d', a != x)
def func1(): # type: () -> None log('func1')
def Speak(self): # type: () -> None log('%s cat: meow', self.color)
def main(): sys.stdout = CustomIO(sys.stdout) sys.stderr = CustomIO(sys.stderr) # log to stdout for capturing root_logger = logging.getLogger() root_logger.setLevel(logging.DEBUG) handler = logging.StreamHandler(sys.stdout) root_logger.addHandler(handler) if 0 != runtime.init(): print('runtime init failed') return runtime.log('init succ') clean_env() http_handler, event_handler = None, None while True: invoke_info = runtime.wait_for_invoke() mode, sockfd, event, context = invoke_info.cmd, invoke_info.sockfd, invoke_info.event, invoke_info.context if mode == 'RELOAD': runtime.log('get reload request: %s' % context) http_handler, event_handler = init_handler( *(context.split(":")[-2:])) runtime.log('handlers reloaded') continue if event or context: try: event = eval(event) context = eval(context) except Exception as ex: runtime.report_running() runtime.report_fail( 'eval event[%s] or context[%s] failed\n%s' % (event, context, str(ex)), 0, 1) continue for env in context['environ'].split(';'): if not env: continue k, v = env.split('=', 1) os.environ[k] = v runtime.log('request[%s] invoked' % context.get('request_id')) if not http_handler and not event_handler: runtime.report_running() runtime.report_fail(init_handler_fault, 0, 2) continue if mode == 'HTTP': http_handler.handle(sockfd) elif mode == 'EVENT': event_handler.handle(event, context) else: runtime.log('recv unknown task type: %s' % mode) runtime.log('process finished')
def run_tests(): # type: () -> None log('--- tuple unpacking') mylist = [(1, 'one'), (2, 'two')] for i, item in mylist: log("- [%d] %s", i, item) log('--- one arg xrange()') m = 2 n = 3 for j in xrange(m * 2): log("%d", j) log('--- two arg xrange()') # TODO: reuse index variable j for k in xrange(m + 2, n + 5): log("%d", k) log('--- enumerate()') for i, c in enumerate(CATS): log('%d %s', i, c)
log('%d %s', i, c) def run_benchmarks(): # type: () -> None n = 500000 result = 0 """ i = 0 while i < n: for j in xrange(3, 10): result += j for j, c in enumerate(CATS): result += j result += len(c) i += 1 log('result = %d', result) log('Ran %d iterations of xrange/enumerate', n) """ if __name__ == '__main__': if os.getenv('BENCHMARK'): log('Benchmarking...') run_benchmarks() else: run_tests()
def run_tests(): # type: () -> None x = 33 result = fib_iter(x) log('fib_iter(%d) = %d', x, result)