-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
kafka proto: a slight performance improvements
- Loading branch information
Showing
6 changed files
with
133 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
{-# LANGUAGE BangPatterns #-} | ||
{-# LANGUAGE OverloadedStrings #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
|
||
module Main where | ||
|
||
import Criterion.Main | ||
import qualified Data.ByteString as BS | ||
import Data.Vector (Vector) | ||
import qualified Data.Vector as V | ||
import qualified Kafka.Protocol.Encoding as K | ||
|
||
main :: IO () | ||
main = do | ||
-- Use ./gen_data.py to generate the data file first | ||
!batchBs1K1 <- BS.readFile "/tmp/records_1k_1.data" | ||
!batchBs1K100 <- BS.readFile "/tmp/records_1k_100.data" | ||
!batchBs1K1000 <- BS.readFile "/tmp/records_1k_1000.data" | ||
|
||
defaultMain | ||
[ bgroup "vector" | ||
[ bench "pure empty" $ nfIO @(Vector Int) (pure V.empty) | ||
, bench "replicateM 0" $ nfIO @(Vector Int) (V.replicateM 0 (pure 0)) | ||
] | ||
, bgroup "decode records" | ||
[ bench "1K*1" $ nfIO $ K.decodeBatchRecords' True batchBs1K1 | ||
, bench "1K*100" $ nfIO $ K.decodeBatchRecords' True batchBs1K100 | ||
, bench "1K*1000" $ nfIO $ K.decodeBatchRecords' True batchBs1K1000 | ||
] | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
from kafka.record import MemoryRecordsBuilder | ||
import random | ||
|
||
|
||
# magic: [0, 1, 2] | ||
def encode_records(magic, key, value, n): | ||
# compression_type: [0, 1, 2, 3] | ||
builder = MemoryRecordsBuilder( | ||
magic=magic, compression_type=0, batch_size=len(value) * n | ||
) | ||
for offset in range(n): | ||
builder.append(timestamp=10000 + offset, key=key, value=value) | ||
builder.close() | ||
return builder.buffer() | ||
|
||
|
||
def write_records(file_name, bs): | ||
with open(file_name, "wb") as f: | ||
f.write(bs) | ||
|
||
|
||
if __name__ == "__main__": | ||
write_records( | ||
"/tmp/records_1k_1.data", | ||
encode_records(2, None, random.randbytes(1024), 1), | ||
) | ||
write_records( | ||
"/tmp/records_1k_100.data", | ||
encode_records(2, None, random.randbytes(1024), 100), | ||
) | ||
write_records( | ||
"/tmp/records_1k_1000.data", | ||
encode_records(2, None, random.randbytes(1024), 1000), | ||
) |