Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_timestamp_from_uuid(self):
until = utc_now()
uuid = uuid1()
after = utc_now()
uuid_timestamp = timestamp_from_uuid(uuid)
self.assertLess(until, uuid_timestamp)
self.assertGreater(after, uuid_timestamp)
start_last_n = utc_now()
last_n_stored_events = []
for _ in six.moves.range(repetitions):
last_n_stored_events = repo.get_most_recent_events(stored_entity_id, limit=n)
time_last_n = (utc_now() - start_last_n) / repetitions
num_retrieved_events = len(list(last_n_stored_events))
events_per_second = num_retrieved_events / time_last_n
print(("Time to get last {:>"+str(i+1)+"} events after {} events: {:.6f}s ({:.0f} events/s)"
"").format(n, num_beats + 1, time_last_n, events_per_second))
for j in range(0, i+1):
last_n(10**j)
# Get the entity by replaying all events (which it must since there isn't a snapshot).
start_replay = utc_now()
for _ in six.moves.range(repetitions):
example = self.app.example_repo[example.id]
assert isinstance(example, Example)
heartbeats = example.count_heartbeats()
assert heartbeats == num_beats, (heartbeats, num_beats)
time_replaying = (utc_now() - start_replay) / repetitions
print("Time to replay {} beats: {:.2f}s ({:.0f} beats/s, {:.6f}s each)"
"".format(num_beats, time_replaying, num_beats / time_replaying, time_replaying / num_beats))
# Take snapshot, and beat heart a few more times.
self.app.example_repo.event_player.take_snapshot(example.id, until=uuid1().hex)
extra_beats = 4
for _ in six.moves.range(extra_beats):
example.beat_heart()
events_per_second = num_retrieved_events / time_last_n
print(("Time to get last {:>"+str(i+1)+"} events after {} events: {:.6f}s ({:.0f} events/s)"
"").format(n, num_beats + 1, time_last_n, events_per_second))
for j in range(0, i+1):
last_n(10**j)
# Get the entity by replaying all events (which it must since there isn't a snapshot).
start_replay = utc_now()
for _ in six.moves.range(repetitions):
example = self.app.example_repo[example.id]
assert isinstance(example, Example)
heartbeats = example.count_heartbeats()
assert heartbeats == num_beats, (heartbeats, num_beats)
time_replaying = (utc_now() - start_replay) / repetitions
print("Time to replay {} beats: {:.2f}s ({:.0f} beats/s, {:.6f}s each)"
"".format(num_beats, time_replaying, num_beats / time_replaying, time_replaying / num_beats))
# Take snapshot, and beat heart a few more times.
self.app.example_repo.event_player.take_snapshot(example.id, until=uuid1().hex)
extra_beats = 4
for _ in six.moves.range(extra_beats):
example.beat_heart()
num_beats += extra_beats
# Get the entity using snapshot and replaying events since the snapshot.
start_replay = utc_now()
for _ in six.moves.range(repetitions):
example = self.app.example_repo[example.id]
time_replaying = (utc_now() - start_replay) / repetitions
count_pages += 1
if next_position is None:
break
else:
previous_position, position = position, next_position
# Check we got to the end of the line.
self.assertEqual(count_pages, 11)
self.assertIsNone(next_position)
self.assertTrue(previous_position)
# Page forward through the log in chronological order.
count_pages = 0
position = None
while True:
start_read = utc_now()
page_of_events, next_position = self.get_message_logged_events_and_next_position(log_reader, position, page_size, is_ascending=True)
time_to_read = (utc_now() - start_read)
total_time_to_read += time_to_read
total_num_reads += 1
count_pages += 1
if next_position is None:
break
else:
position = next_position
self.assertEqual(count_pages, 11)
self.assertIsNone(next_position)
self.assertTrue(previous_position)
reads_per_second = total_num_reads / total_time_to_read
messages_per_second = reads_per_second * number_of_messages
break
else:
previous_position, position = position, next_position
# Check we got to the end of the line.
self.assertEqual(count_pages, 11)
self.assertIsNone(next_position)
self.assertTrue(previous_position)
# Page forward through the log in chronological order.
count_pages = 0
position = None
while True:
start_read = utc_now()
page_of_events, next_position = self.get_message_logged_events_and_next_position(log_reader, position, page_size, is_ascending=True)
time_to_read = (utc_now() - start_read)
total_time_to_read += time_to_read
total_num_reads += 1
count_pages += 1
if next_position is None:
break
else:
position = next_position
self.assertEqual(count_pages, 11)
self.assertIsNone(next_position)
self.assertTrue(previous_position)
reads_per_second = total_num_reads / total_time_to_read
messages_per_second = reads_per_second * number_of_messages
print("Time to read {} pages of logged messages: {:.6f}s ({:.0f} pages/s, {:.0f} messages/s))"
"".format(total_num_reads, total_time_to_read, reads_per_second, messages_per_second))
def last_n(n):
n = min(n, num_beats + 1)
stored_entity_id = make_stored_entity_id('Example', example.id)
repo = self.app.example_repo.event_player.event_store.stored_event_repo
start_last_n = utc_now()
last_n_stored_events = []
for _ in six.moves.range(repetitions):
last_n_stored_events = repo.get_most_recent_events(stored_entity_id, limit=n)
time_last_n = (utc_now() - start_last_n) / repetitions
num_retrieved_events = len(list(last_n_stored_events))
events_per_second = num_retrieved_events / time_last_n
print(("Time to get last {:>"+str(i+1)+"} events after {} events: {:.6f}s ({:.0f} events/s)"
"").format(n, num_beats + 1, time_last_n, events_per_second))
def test_timestamp_from_uuid(self):
until = utc_now()
uuid = uuid1()
after = utc_now()
uuid_timestamp = timestamp_from_uuid(uuid)
self.assertLess(until, uuid_timestamp)
self.assertGreater(after, uuid_timestamp)
def test_log_performance(self):
log = start_new_log('example', bucket_size='year')
logger = get_logger(log)
log_reader = get_log_reader(log, self.app.event_store)
# Write a load of messages.
start_write = utc_now()
number_of_messages = 111
events = []
for i in range(number_of_messages):
event = logger.info('Logger message number {}'.format(i))
events.append(event)
time_to_write = (utc_now() - start_write)
print("Time to log {} messages: {:.2f}s ({:.0f} messages/s, {:.6f}s each)"
"".format(number_of_messages, time_to_write, number_of_messages/ time_to_write,
time_to_write / number_of_messages))
# Read pages of messages in descending order.
# - get a limited number until a time, then use the earliest in that list as the position
position = events[-1].domain_event_id
page_size = 10
def last_n(n):
n = min(n, num_beats + 1)
stored_entity_id = make_stored_entity_id('Example', example.id)
repo = self.app.example_repo.event_player.event_store.stored_event_repo
start_last_n = utc_now()
last_n_stored_events = []
for _ in six.moves.range(repetitions):
last_n_stored_events = repo.get_most_recent_events(stored_entity_id, limit=n)
time_last_n = (utc_now() - start_last_n) / repetitions
num_retrieved_events = len(list(last_n_stored_events))
events_per_second = num_retrieved_events / time_last_n
print(("Time to get last {:>"+str(i+1)+"} events after {} events: {:.6f}s ({:.0f} events/s)"
"").format(n, num_beats + 1, time_last_n, events_per_second))