Пример #1
0
def prepare(magic: int):
    samples = []
    for _ in range(BATCH_SAMPLES):
        batch = BatchBuilder(
            magic, batch_size=DEFAULT_BATCH_SIZE, compression_type=0)
        for offset in range(MESSAGES_PER_BATCH):
            size = batch.append(
                timestamp=None,  # random.randint(*TIMESTAMP_RANGE)
                key=random_bytes(KEY_SIZE),
                value=random_bytes(VALUE_SIZE))
            assert size
        samples.append(bytes(batch._builder.build()))

    return iter(itertools.cycle(samples))
Пример #2
0
def prepare(magic: int):
    samples = []
    for _ in range(BATCH_SAMPLES):
        batch = BatchBuilder(magic,
                             batch_size=DEFAULT_BATCH_SIZE,
                             compression_type=0)
        for offset in range(MESSAGES_PER_BATCH):
            size = batch.append(
                timestamp=None,  # random.randint(*TIMESTAMP_RANGE)
                key=random_bytes(KEY_SIZE),
                value=random_bytes(VALUE_SIZE))
            assert size
        samples.append(bytes(batch._builder.build()))

    return iter(itertools.cycle(samples))
Пример #3
0
def func(loops: int, magic: int):
    # Jit can optimize out the whole function if the result is the same each
    # time, so we need some randomized input data )
    precomputed_samples = prepare()
    results = []

    # Main benchmark code.
    t0 = perf.perf_counter()
    for _ in range(loops):
        batch = BatchBuilder(magic, batch_size=DEFAULT_BATCH_SIZE,
                             compression_type=0, is_transactional=False)
        for _ in range(MESSAGES_PER_BATCH):
            key, value, timestamp = next(precomputed_samples)
            size = batch.append(timestamp=None, key=key, value=value)
            assert size
        results.append(batch._build())

    res = perf.perf_counter() - t0

    finalize(results)

    return res
Пример #4
0
    async def test_message_batch_builder_basic(self):
        magic = 0
        batch_size = 1000
        msg_count = 3
        key = b"test key"
        value = b"test value"
        builder = BatchBuilder(magic, batch_size, 0)
        self.assertEqual(builder._relative_offset, 0)
        self.assertIsNone(builder._buffer)
        self.assertFalse(builder._closed)
        self.assertEqual(builder.size(), 0)
        self.assertEqual(builder.record_count(), 0)

        # adding messages returns size and increments appropriate values
        for num in range(1, msg_count + 1):
            old_size = builder.size()
            metadata = builder.append(key=key, value=value, timestamp=None)
            self.assertIsNotNone(metadata)
            msg_size = metadata.size
            self.assertTrue(msg_size > 0)
            self.assertEqual(builder.size(), old_size + msg_size)
            self.assertEqual(builder.record_count(), num)
        old_size = builder.size()
        old_count = builder.record_count()

        # close the builder
        buf = builder._build()
        self.assertIsNotNone(builder._buffer)
        self.assertEqual(buf, builder._buffer)
        self.assertTrue(builder._closed)

        # nothing can be added after the builder has been closed
        old_size = builder.size()
        metadata = builder.append(key=key, value=value, timestamp=None)
        self.assertIsNone(metadata)
        self.assertEqual(builder.size(), old_size)
        self.assertEqual(builder.record_count(), old_count)