-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds serialization microbenchmark (#37)
- Loading branch information
1 parent
ad02fdd
commit 43f505b
Showing
11 changed files
with
152 additions
and
1 deletion.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
{-# LANGUAGE BangPatterns #-} | ||
{-# LANGUAGE ScopedTypeVariables #-} | ||
{-# LANGUAGE TypeApplications #-} | ||
|
||
module Main (main) where | ||
|
||
import Bitcoin (Block, Tx, decodeHex) | ||
import Control.DeepSeq (NFData) | ||
import Criterion (Benchmark, bench, bgroup, nf) | ||
import Criterion.Main (defaultMain) | ||
import Data.Bifunctor (bimap) | ||
import Data.Binary (Binary) | ||
import qualified Data.Binary as Bin | ||
import Data.ByteString (ByteString) | ||
import qualified Data.ByteString.Lazy as BSL | ||
import Data.Proxy (Proxy (..)) | ||
import Data.Serialize (Serialize) | ||
import qualified Data.Serialize as S | ||
import Data.Text (Text) | ||
import qualified Data.Text as Text | ||
import qualified Data.Text.IO as TIO | ||
|
||
|
||
main :: IO () | ||
main = do | ||
-- BLOCKS | ||
blockA <- TIO.readFile "data/block-758000.dat" | ||
blockB <- TIO.readFile "data/block-758100.dat" | ||
blockC <- TIO.readFile "data/block-758200.dat" | ||
let benchBlocks = | ||
bgroup | ||
"blocks" | ||
[ roundTrip (Proxy @Block) "block 758000" blockA | ||
, roundTrip (Proxy @Block) "block 758100" blockB | ||
, roundTrip (Proxy @Block) "block 758200" blockC | ||
] | ||
|
||
-- TRANSACTIONS | ||
txA <- TIO.readFile "data/tx-A.dat" | ||
txB <- TIO.readFile "data/tx-B.dat" | ||
txC <- TIO.readFile "data/tx-C.dat" | ||
let benchTxs = | ||
bgroup | ||
"transactions" | ||
[ roundTrip (Proxy @Tx) "tx A" txA | ||
, roundTrip (Proxy @Tx) "tx B" txB | ||
, roundTrip (Proxy @Tx) "tx C" txC | ||
] | ||
|
||
defaultMain | ||
[ bgroup | ||
"serialization" | ||
[ benchBlocks | ||
, benchTxs | ||
] | ||
] | ||
|
||
|
||
roundTrip :: | ||
forall a. | ||
(NFData a, Binary a, Serialize a) => | ||
Proxy a -> | ||
String -> | ||
Text -> | ||
Benchmark | ||
roundTrip _ label xHex = | ||
bgroup | ||
label | ||
[ bgroup | ||
"binary" | ||
[ bench "encode" $ nf Bin.encode x | ||
, bench "decode" $ nf binDecode xBytes | ||
] | ||
, bgroup | ||
"cereal" | ||
[ bench "encode" $ nf S.encode x | ||
, bench "decode" $ nf (S.decode @a) xBytes | ||
] | ||
] | ||
where | ||
Just !xBytes = decodeHex $ Text.filter (/= '\n') xHex | ||
Right !x = binDecode xBytes | ||
|
||
binDecode :: Binary a => ByteString -> Either String a | ||
binDecode = bimap pr3 pr3 . Bin.decodeOrFail . BSL.fromStrict | ||
pr3 (_, _, z) = z |
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
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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 @@ | ||
02000000000102dd1577db73c41101e7c67cd1b0a8bcd878d501c64640c6813c4deabf9cde073502000000171600140736f801dfe253a5fbcae757740312f5767e6a70feffffffdb7c5d79bb8c48585e63376438daf7b178cb7c8680bbab154470a4469b07236003000000171600140736f801dfe253a5fbcae757740312f5767e6a70feffffff02c004770000000000160014e4e2ba9fd8306fb0cd0de18c6392352e9f0381a5845b22000000000017a914f1566e36b3f375ab5c789a587ed680bd0bf48b448702483045022100d5c815bec8731aa488d17d6be95b6a1e83514187d81a75989ce334034046d2e002200c2b9cbe83719065f7cc2818d13c74ed0d486dfede24dc6a8d1f37c96650500401210265073eafd8007a4facd5b1fc7eb39d0a6ad9bd476389a50b9a83367b7e71274c02483045022100b85d5f2f5c8ecf72693fe10aa5627d0cb5d5d5cb4dbf88c437182bed7b24f75e022042ef1c1391ad03f6e3bc64cd82ef41538bea52339936c856f894f4a4f565cfa901210265073eafd8007a4facd5b1fc7eb39d0a6ad9bd476389a50b9a83367b7e71274c00000000 |
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 @@ | ||
010000000001074ff9ef75e62d9d5c5cb89edf430b236b02ba7c0291a5f8a350baed1d0375791007000000171600148130903f392f800fbe683183a514943ca7165ae2ffffffff4ff9ef75e62d9d5c5cb89edf430b236b02ba7c0291a5f8a350baed1d037579100900000017160014a83ef0cffa062c897e0a9f853b94623c5148fdd2ffffffff4ff9ef75e62d9d5c5cb89edf430b236b02ba7c0291a5f8a350baed1d03757910050000001716001404da1e9c634bc5a4abbbef08683d602826eb7bbcffffffff4ff9ef75e62d9d5c5cb89edf430b236b02ba7c0291a5f8a350baed1d037579100800000017160014f96cb4f912a13434f81714379c5f7e326d9d6030ffffffff4ff9ef75e62d9d5c5cb89edf430b236b02ba7c0291a5f8a350baed1d037579100600000017160014c647817ed0ceb4129adbb67ce46fbb5b0891d861ffffffff4ff9ef75e62d9d5c5cb89edf430b236b02ba7c0291a5f8a350baed1d037579100200000017160014c1d2de3c6eb2f5d759738e0c93c3de4e1be376d8ffffffff4ff9ef75e62d9d5c5cb89edf430b236b02ba7c0291a5f8a350baed1d037579100400000017160014bab3b67e97cbdd59f4df806adcc2bc6183b1e191ffffffff10c38b59020000000017a9141fc1d097f43ea12401a795d2c66ba6568e2a847587425ec8050000000017a914ed3bea4863b171256d3ccae5345220837a891c5d8768ee55020000000017a91485ca478d5572272317ea27f66097d461c10e3e638714e2dd040000000017a91442e76050b861184a66459e26452f30e1a686e73387fb0dce040000000017a914f3b67fdab3d07baacb2dbd3f6285c858fdddd43a8737ba37040000000017a9140238447f250b44bc1163c297fce20b42ad4afa6787f23bdc040000000017a9147a7e1fd349fa023a91d3a02f4e10327250a45b1d87c9aed8030000000017a914fce24e7040c4ae5984f2356e0392a4cecbf23f8d87c4fa0a040000000017a914b0cdd2a8bb79a17db07b7ff593eb282a3360688b87eda9ee360000000017a914b8fa42bfe175cf5eacc6f61a720104437e8d1ddc8759e9fb310000000017a9141da46cbe94e616138225f090b0b2c2af2d63d96087a9e63a1e0000000017a914a70d96a25ddb647c95e7786083640ad4a619f17b870a3458350000000017a91411c9d4b370aa4f48c4249c59ffe37cc37d0fe3c387803070370000000017a914334b7940dae0b25417eadaaf1d27cc23d5f76a7f87e7985c360000000017a91454a0d53c1f6d7fc2bc4634577089ab1cec098bc887296572050000000017a91401d73a0cb01d412689b52b6e5d3c9d1f5b58a0798702483045022100e497292ca9dfd997a19db698c2f8b6f58ae0ed5354231edc733171394d4aa6b3022058ebea3ccfdd2ee1fa8ad792d2a4ce4e926495afa0768d7cb6051502ebf0ff78012102642087634a79dc9c90776132389cf1fe16cec90b23d55624a88c5b1ca5c796e40247304402202c00053b555e29f815f1845bd77077fd40cb68372b3f9e568fcafd12fd1a96a102205504e74f56a8dced807a0b6d5f3dea520108032c05df1dede3c04829aa4e78bf012103ceb2eaa9de62f1ff76747d7e2ff54a76c0ef0a7c626fa2309fa83d58b920146502473044022045a55c326cb40d4ee9278fc180bed8483632282405d9a6cd9ea26e82c4f9cf6f02201f9b339fd327f509adf93b9a8431c9ec7699dc51caf84c73668456ccebd56cff012102ff5bce27c41f9300797593e27712d0d5edd68a0f56b2efa29ec778342be23edb02483045022100c2ea04394bb89b7b31298837cb17cc21c87c49013c77833759446c8613c588300220124d3180a78c2536fa87c2a66be340f270a771fd10cf4dfccb24811311c9fe5f01210248b63f7050ef75a03921ed52c356230e12e05203b8e070ab14de7aa7b0ffe60502473044022048b58e0b27e3a6b98ba5ba801f43bde125f34d6f54ac1ab70adde2e101ccd62f02200ec87dd4549ef10f931b54c3de7bfb24d2cf744848a23d07617df0a8f37146130121027054326fdb00e2c2e559e9a3c64c83b5b42a5d693c4f69851d013ac9eff5aa1202483045022100c93964054831f57feeb143845b05335a2a5b350893fe75dd64c0e708461b125402202f308cfe170b4d9a443282919f52c9ea863033b3198c3b36c708cf114c6872c101210343ff2f65e9aef2eda1b8ea585c0c0edbfcdc7382cfa0ba04898dd798ec19b3e602483045022100d4b0e075a194ed186cb99c6ce8ca0bf0bf718a307190c2278291891b94e4e9fe02202cd91c05fa5e52c78f0ab14efb1901b502e66bc0841f1cb57abb558696f15b6c012102d1141734418483e3ab7c055ec1f3a5e559d764f6dc23fa055919abb1152c594b00000000 |
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 @@ | ||
010000000001011b17079deaba7b3245adb3ed0c00058134007a8d08ff4aec9d66a3fe703089060f00000000ffffffff1551f907000000000016001493073074d4c37a00251123e4a41ba7fa657d4651803801000000000017a914a5a674b7ec3b7c1144a73adcf9bb3f61c1c37f3887aeef0f000000000022002051413a8370db88bde6324ff184e2cead9faef9d9d7578c0622450e961b13016698e70300000000001976a91443c36f8f19647b811a44ea62d8d1b7107ec736f988ac9a8e08000000000017a914d74abad498d0037c8b04d0d82b97e07b1e1ad2e687d9960800000000001976a914baf05b358eb3c57859058cec0d4d4ae10445087c88ac61151d000000000016001421497d26b74412e222dae5f8abced1b86cc69dddf1ac0a000000000016001427fc96ed2d7b535c5a65b67316410827069a870af2ea0700000000001976a914cbdafbdefd318ad8107799391c82f774a8cad1c788ac89da0000000000001600144c8f308b016367b205915251f693b295ed9d74c59a4601000000000017a914dd0412636441d7fbb9f8b5ad2459d25e6a17dc33875ec20200000000001600146bc6a5c68b006b4f1f13b18efa818a21850d6f4d17ec07000000000017a914d596a9f403579414ed798899c6b2e4e94e4677ac8766fa0a000000000017a9140e4546d83e7bf95370e01dbfb6fa77d00f585de087d8910100000000001976a914390e2c70edfb09886ab2d7e04e0570392e88d6f788ac5aac0100000000001976a914fa85ea5ababc1780c08d1d8faed46b1df3b6953b88ac8f6301000000000017a91414b70b13a9ce04587eb2e664166b37277e5c0a26879fbf0c0000000000160014ae3c3e089e1ee1ff070f73020d83ef8574c8954acff54900000000002200204ce295b1e5f8b22712b488aef5b2c1eb33ea3f475f00cc2e27c5aeaeeab843574fdb0100000000001976a914c4a199ec51fef7072f916e1c47c1b879572f9fde88acca0ffed60200000016001408da93bfad48bddcdaf49e65248c3195a1838caa02473044022038f333a000e1319efcce8ad1b4214703047da1caa364b76c5b947c3c883194c9022055dd71f2f400de5f02b3f32330abdd1bae982d9b6e02b5b560893fb90e20fb980121022a51bd8200f44d72e94fa623ad6f79efb10fa6a873b5a8cf1c63b47f6301729b00000000 |
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