예제 #1
0
    def test_log_performance(self):

        with self.construct_application() as app:
            example_id = uuid4()
            log = start_new_timebucketedlog(example_id, bucket_size="year")
            log_reader = get_timebucketedlog_reader(log, app.log_event_store)

            # Write a load of messages.
            start_write = time.time()
            number_of_messages = 111
            events = []
            for i in range(number_of_messages):
                event = log.log_message("Logger message number {}".format(i))
                events.append(event)
            time_to_write = time.time() - 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].timestamp

            page_size = 10

            # Page back through the log in reverse chronological order.
            previous_position = None
            count_pages = 0
            total_time_to_read = 0
            total_num_reads = 0
            while True:
                start_read = time.time()
                page_of_events, next_position = self.get_message_logged_events_and_next_position(
                    log_reader, position, page_size)
                time_to_read = time.time() - start_read
                total_time_to_read += time_to_read
                total_num_reads += 1
                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 = time.time()
                page_of_events, next_position = self.get_message_logged_events_and_next_position(
                    log_reader, position, page_size, is_ascending=True)
                time_to_read = time.time() - 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 test_log_performance(self):

        with self.construct_application() as app:
            example_id = uuid4()
            log = start_new_timebucketedlog(example_id, bucket_size='year')
            log_reader = get_timebucketedlog_reader(log, app.log_event_store)

            # Write a load of messages.
            start_write = time.time()
            number_of_messages = 111
            events = []
            for i in range(number_of_messages):
                event = log.append_message('Logger message number {}'.format(i))
                events.append(event)
            time_to_write = (time.time() - 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].timestamp

            page_size = 10

            # Page back through the log in reverse chronological order.
            previous_position = None
            count_pages = 0
            total_time_to_read = 0
            total_num_reads = 0
            while True:
                start_read = time.time()
                page_of_events, next_position = self.get_message_logged_events_and_next_position(log_reader, position,
                                                                                                 page_size)
                time_to_read = (time.time() - start_read)
                total_time_to_read += time_to_read
                total_num_reads += 1
                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 = time.time()
                page_of_events, next_position = self.get_message_logged_events_and_next_position(log_reader, position,
                                                                                                 page_size,
                                                                                                 is_ascending=True)
                time_to_read = (time.time() - 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))