def test_should_correctly_report_size(): bucket = [] buffer = CoalescingRingBuffer(2) eq_(buffer.size, 0) ok_(buffer.empty) not_(buffer.full) buffer.offer('asd') eq_(buffer.size, 1) not_(buffer.empty) not_(buffer.full) buffer.offer(1, 'qwe') eq_(buffer.size, 2) not_(buffer.empty) ok_(buffer.full) fetched = buffer.poll(bucket, 1) eq_(fetched, 1) eq_(buffer.size, 1) not_(buffer.empty) not_(buffer.full) fetched = buffer.poll(bucket, 1) eq_(fetched, 1) eq_(buffer.size, 0) ok_(buffer.empty) not_(buffer.full)
def test_should_see_last_prices(): buffer = CoalescingRingBuffer(NUMBER_OF_INSTRUMENTS // 5) consumer_snapshots = [None] * NUMBER_OF_INSTRUMENTS producer_thread = Thread(target=producer, args=(buffer, )) consumer_thread = Thread(target=consumer, args=(buffer, consumer_snapshots)) producer_thread.start() consumer_thread.start() consumer_thread.join() for snapshot in consumer_snapshots: key, bid, ask = snapshot eq_((bid, ask), (SECOND_BID, SECOND_ASK), 'Bid/ask for instrument %r' % (key, ))
def run(run_number, number_of_updates): print('Run number %d...' % (run_number, )) NUMBER_OF_INSTRUMENTS = 10 POISON_PILL = -1 buffer = CoalescingRingBuffer(1 << 20) read_counter = [0] stop_watch = StopWatch() producer_thread = Thread(target=producer, args=(stop_watch, NUMBER_OF_INSTRUMENTS, POISON_PILL, number_of_updates, buffer)) consumer_thread = Thread(target=consumer, args=(stop_watch, NUMBER_OF_INSTRUMENTS, POISON_PILL, read_counter, buffer)) gc.collect() producer_thread.daemon = True consumer_thread.daemon = True producer_thread.start() consumer_thread.start() while consumer_thread.is_alive(): sleep(0.5) nanos_taken = stop_watch.nanos_taken print('Total run time: %.2f s' % (nanos_taken * NANO, )) print('Compression ratio: %.2f' % (number_of_updates / read_counter[0], )) mega_ops_per_second = round(1000 * number_of_updates / nanos_taken, 2) print('MOPS: %.2f' % (mega_ops_per_second, )) return mega_ops_per_second
def create_buffer(capacity): return CoalescingRingBuffer(capacity)
def test_should_hot_have_memory_leaks(): counter = Counter() buffer = CoalescingRingBuffer(16) buffer.offer(CountingValue(counter)) for key in (1, 2, 1): buffer.offer(CountingKey(key, counter), CountingValue(counter)) buffer.offer(CountingValue(counter)) eq_(buffer.size, 4) buffer.poll([], 1) buffer.poll([]) ok_(buffer.empty) # to trigger the clean buffer.offer(None) gc.collect() eq_(counter.value, 8)