Skip to content

Commit

Permalink
Merge pull request #884 from input-output-hk/abailly-iohk/micro-bench…
Browse files Browse the repository at this point in the history
…marks

Add micro benchmarks for serialising and applying Transactions
  • Loading branch information
pgrange authored May 25, 2023
2 parents b7e90a6 + 2119d9c commit ed0c22d
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 6 deletions.
5 changes: 4 additions & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ jobs:
shell: 'script -q -e -c "bash {0}"'
env:
TERM: "xterm"
# TUI specs are flaky. They are failing because of SIGSEGV.
# TUI specs are flaky. They are failing because of SIGSEGV.
# There is an open issue to tackle this problem. https://github.com/input-output-hk/hydra/issues/590
continue-on-error: true
run: |
Expand Down Expand Up @@ -220,6 +220,9 @@ jobs:
- package: hydra-node
bench: tx-cost
options: '--output-directory $(pwd)/../docs/benchmarks'
- package: hydra-node
bench: micro
options: '-o $(pwd)/../docs/static/ledger-bench.html'
- package: hydra-cluster
bench: bench-e2e
options: '--scaling-factor 1'
Expand Down
6 changes: 6 additions & 0 deletions docs/benchmarks/ledger.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Ledger micro-benchmarks

These micro-benchmarks details the time for various key aspects of applying transactions in the Cardano ledger in a Hydra Head.
The absolute values are relatively meaningless, what's important is the relative cost of the various operations involved in the end-to-end process of submitting a new transaction to a Hydra Head and applying it to the ledger.

* :chart_with_upwards_trend: [Ledger Benchmarks](pathname:///ledger-bench.html)
47 changes: 47 additions & 0 deletions hydra-node/bench/micro-bench/Main.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE TypeApplications #-}

module Main where

import Hydra.Prelude

import qualified Data.Aeson as Aeson
import Criterion (bench, bgroup, whnf, nf)
import Criterion.Main (defaultMain)
import Hydra.Cardano.Api (
UTxO, serialiseToCBOR,
)
import Hydra.Ledger (ChainSlot (ChainSlot), Ledger (applyTransactions), ValidationError)
import Hydra.Ledger.Cardano (Tx, cardanoLedger, genFixedSizeSequenceOfSimplePaymentTransactions)
import Test.QuickCheck (generate)
import Hydra.API.ClientInput (ClientInput(NewTx))
import Data.Aeson ((.=), object, Value(String))
import qualified Data.List as List
import Hydra.Chain.Direct.Fixture (defaultGlobals, defaultLedgerEnv)

main :: IO ()
main = do
(utxo, tx) <- prepareTx
let jsonNewTx = (Aeson.encode . NewTx) tx
toNewTx bs = object [ "tag" .= ("NewTx" :: Text), "transaction" .= String (decodeUtf8 bs) ]
cborNewTx = (Aeson.encode . toNewTx . serialiseToCBOR) tx
defaultMain
[ bgroup
"Cardano Ledger"
[ bench "Apply Tx" $ whnf benchApplyTxs (utxo, tx)
, bench "Serialize NewTx (JSON)" $ nf (Aeson.encode . NewTx) tx
, bench "Serialize NewTx (CBOR)" $ nf serialiseToCBOR tx
, bench "Deserialize NewTx (JSON)" $ whnf (Aeson.decode @(ClientInput Tx)) jsonNewTx
, bench "Deserialize NewTx (CBOR-in-JSON)" $ whnf (Aeson.decode @(ClientInput Tx)) cborNewTx
]
]

prepareTx :: IO (UTxO, Tx)
prepareTx =
second List.head <$> generate (genFixedSizeSequenceOfSimplePaymentTransactions 1)

benchApplyTxs :: (UTxO, Tx) -> Either (Tx, ValidationError) UTxO
benchApplyTxs (utxo, tx) = applyTransactions defaultLedger (ChainSlot 1) utxo [tx]

defaultLedger :: Ledger Tx
defaultLedger = cardanoLedger defaultGlobals defaultLedgerEnv
File renamed without changes.
File renamed without changes.
21 changes: 18 additions & 3 deletions hydra-node/hydra-node.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ library
Hydra.Chain
Hydra.Chain.CardanoClient
Hydra.Chain.Direct
Hydra.Chain.Direct.Fixture
Hydra.Chain.Direct.Handlers
Hydra.Chain.Direct.ScriptRegistry
Hydra.Chain.Direct.State
Expand Down Expand Up @@ -130,7 +131,6 @@ library
, cardano-ledger-alonzo-test
, cardano-ledger-babbage
, cardano-ledger-babbage-test
, cardano-ledger-byron
, cardano-ledger-core
, cardano-ledger-shelley
, cardano-ledger-shelley-ma
Expand Down Expand Up @@ -214,7 +214,7 @@ executable hydra-tools

benchmark tx-cost
import: project-config
hs-source-dirs: exe/tx-cost
hs-source-dirs: bench/tx-cost
main-is: Main.hs
other-modules: TxCost
type: exitcode-stdio-1.0
Expand All @@ -240,6 +240,22 @@ benchmark tx-cost
-- NOTE(SN): should fix HLS choking on PlutusTx plugin
ghc-options: -fplugin-opt PlutusTx.Plugin:defer-errors

benchmark micro
import: project-config
hs-source-dirs: bench/micro-bench
main-is: Main.hs
type: exitcode-stdio-1.0
build-depends:
QuickCheck
, aeson
, base
, criterion
, hydra-cardano-api
, hydra-node
, hydra-prelude

ghc-options: -threaded -rtsopts

test-suite tests
import: project-config
ghc-options: -threaded -rtsopts -with-rtsopts=-N
Expand All @@ -259,7 +275,6 @@ test-suite tests
Hydra.Chain.Direct.Contract.Init
Hydra.Chain.Direct.Contract.Mutation
Hydra.Chain.Direct.ContractSpec
Hydra.Chain.Direct.Fixture
Hydra.Chain.Direct.HandlersSpec
Hydra.Chain.Direct.ScriptRegistrySpec
Hydra.Chain.Direct.StateSpec
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
{-# OPTIONS_GHC -Wno-incomplete-uni-patterns #-}
{-# OPTIONS_GHC -Wno-orphans #-}

-- | Generic Cardano constants for use in testing.
module Hydra.Chain.Direct.Fixture (
module Hydra.Chain.Direct.Fixture,
pparams,
Expand Down
6 changes: 4 additions & 2 deletions nix/hydra/packages.nix
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,10 @@ rec {
};
benchs = {
hydra-node = pkgs.mkShellNoCC {
name = "bench-tx-cost";
buildInputs = [ nativePkgs.hydra-node.components.benchmarks.tx-cost ];
name = "bench-hydra-node";
buildInputs = [ nativePkgs.hydra-node.components.benchmarks.tx-cost
nativePkgs.hydra-node.components.benchmarks.micro
];
};
hydra-cluster = pkgs.mkShellNoCC {
name = "bench-hydra-cluster";
Expand Down

0 comments on commit ed0c22d

Please sign in to comment.