Skip to content

Instantly share code, notes, and snippets.

@mzizzi
Created October 18, 2015 20:53
Show Gist options
  • Save mzizzi/fd2924a8d68a33ef0ec2 to your computer and use it in GitHub Desktop.
Save mzizzi/fd2924a8d68a33ef0ec2 to your computer and use it in GitHub Desktop.
import sys
sys.path.append('c:\NatLink\NatLink\MacroSystem')
import time
import aenea
from aenea import Text
from aenea.wrappers import ensure_execution_context
class BatchTextActionExecutor(object):
def __init__(self):
self.batch = []
self.original_write_text = None
def __enter__(self):
batch = self.batch
def write_text_patch(*args, **kwargs):
batch.append(('write_text', args, kwargs))
self.original_write_text = aenea.communications.server.write_text
aenea.communications.server.write_text = write_text_patch
def __exit__(self, exc_type, exc_val, exc_tb):
aenea.communications.server.multiple_actions(self.batch)
aenea.communications.server.write_text = self.original_write_text
def time_actions(count=20, data=None):
"""
Execute Text('abc') <count> times. Optionally provide action
execution context with <data> (Usually the result of a call to
aenea.wrappers.ensure_execution_context.
"""
s = time.time()
for _ in range(0, count):
Text('abc').execute(data=data)
return time.time() - s
def time_batch_actions(count=20, data=None):
s = time.time()
with BatchTextActionExecutor():
for _ in range(0, count):
Text('abc').execute(data=data)
return time.time() - s
def compare(action_chain_count=20, iterations=100):
without_data_total = 0
for _ in range(0, iterations):
without_data_total += time_actions(count=action_chain_count, data=None)
avg_exec_time_without_data = without_data_total / iterations
with_data_total = 0
for _ in range(0, iterations):
data = ensure_execution_context({})
with_data_total += time_actions(count=action_chain_count, data=data)
avg_exec_time_with_data = with_data_total / iterations
batch_actions_total = 0
for _ in range(0, iterations):
data = ensure_execution_context({})
batch_actions_total += time_batch_actions(count=action_chain_count, data=data)
avg_exec_time_batch = batch_actions_total / iterations
print 'average %d item action chain execution time' % action_chain_count
print ' WITH action.execute(data=...) provided: %2.2f' % avg_exec_time_with_data
print ' WITHOUT action.execute(data=...) provided: %2.2f' % avg_exec_time_without_data
print ' BATCH action.execute(...) provided: %2.2f' % avg_exec_time_batch
if __name__ == '__main__':
compare(20,100)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment