Skip to content

Commit

Permalink
Merge pull request #1433 from input-output-hk/bench-json-datasets
Browse files Browse the repository at this point in the history
Switch bench-e2e to JSON datasets again
  • Loading branch information
ch1bo committed May 14, 2024
2 parents d8d2f93 + 101b770 commit 6427e5c
Show file tree
Hide file tree
Showing 9 changed files with 36 additions and 54 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci-nix.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ jobs:
options: '-o $(pwd)/../benchmarks/ledger-bench.html'
- package: hydra-cluster
bench: bench-e2e
options: 'datasets datasets/3-nodes.cbor datasets/1-node.cbor --output-directory $(pwd)/../benchmarks --timeout 1000s'
options: 'datasets datasets/1-node.json datasets/3-nodes.json --output-directory $(pwd)/../benchmarks --timeout 1000s'
- package: plutus-merkle-tree
bench: on-chain-cost
options: '$(pwd)/../benchmarks'
Expand Down
20 changes: 7 additions & 13 deletions hydra-cluster/bench/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,17 @@ import Test.Hydra.Prelude
import Bench.EndToEnd (bench)
import Bench.Options (Options (..), benchOptionsParser)
import Bench.Summary (Summary (..), markdownReport, textReport)
import Cardano.Binary (decodeFull, serialize)
import Data.Aeson (eitherDecodeFileStrict')
import Data.ByteString.Base16 qualified as Base16
import Data.ByteString.Lazy qualified as LBS
import Data.Aeson (eitherDecodeFileStrict', encodeFile)
import Hydra.Cardano.Api (
ShelleyBasedEra (..),
ShelleyGenesis (..),
fromLedgerPParams,
)
import Hydra.Generator (Dataset (..), generateConstantUTxODataset)
import Options.Applicative (
execParser,
)
import Options.Applicative (execParser)
import System.Directory (createDirectoryIfMissing, doesDirectoryExist)
import System.Environment (withArgs)
import System.FilePath (takeDirectory, takeFileName, (</>))
import System.FilePath (takeFileName, (</>))
import Test.HUnit.Lang (formatFailureReason)
import Test.QuickCheck (generate, getSize, scale)

Expand Down Expand Up @@ -53,12 +48,12 @@ main =
Right shelleyGenesis ->
pure $ fromLedgerPParams ShelleyBasedEraShelley (sgProtocolParams shelleyGenesis)
dataset <- generateConstantUTxODataset pparams (fromIntegral clusterSize) numberOfTxs
let datasetPath = workDir </> "dataset.cbor"
let datasetPath = workDir </> "dataset.json"
saveDataset datasetPath dataset
run outputDirectory timeoutSeconds startingNodeId [datasetPath]

replay outputDirectory timeoutSeconds startingNodeId benchDir = do
let datasetPath = benchDir </> "dataset.cbor"
let datasetPath = benchDir </> "dataset.json"
putStrLn $ "Replaying single dataset from work directory: " <> datasetPath
run outputDirectory timeoutSeconds startingNodeId [datasetPath]

Expand All @@ -83,13 +78,12 @@ main =
loadDataset :: FilePath -> IO Dataset
loadDataset f = do
putStrLn $ "Reading dataset from: " <> f
readFileBS f >>= either (die . show) pure . (decodeFull . LBS.fromStrict . Base16.decodeLenient)
eitherDecodeFileStrict' f >>= either (die . show) pure

saveDataset :: FilePath -> Dataset -> IO ()
saveDataset f dataset = do
putStrLn $ "Writing dataset to: " <> f
createDirectoryIfMissing True $ takeDirectory f
writeFileBS f $ Base16.encode $ LBS.toStrict $ serialize dataset
encodeFile f dataset

data BenchmarkFailed
= TestFailed HUnitFailure
Expand Down
1 change: 0 additions & 1 deletion hydra-cluster/datasets/1-node.cbor

This file was deleted.

1 change: 1 addition & 0 deletions hydra-cluster/datasets/1-node.json

Large diffs are not rendered by default.

1 change: 0 additions & 1 deletion hydra-cluster/datasets/3-nodes.cbor

This file was deleted.

1 change: 1 addition & 0 deletions hydra-cluster/datasets/3-nodes.json

Large diffs are not rendered by default.

7 changes: 2 additions & 5 deletions hydra-cluster/hydra-cluster.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ library
build-depends:
, aeson
, async
, base >=4.7 && <5
, base >=4.7 && <5
, bytestring
, cardano-slotting
, containers
Expand All @@ -103,7 +103,6 @@ library
, lens
, lens-aeson
, optparse-applicative
, plutus-ledger-api:plutus-ledger-api-testlib >=1.9.1.0
, process
, QuickCheck
, req
Expand Down Expand Up @@ -164,6 +163,7 @@ test-suite tests
, directory
, filepath
, hspec
, hspec-golden-aeson
, http-client
, http-conduit
, hydra-cardano-api
Expand Down Expand Up @@ -202,9 +202,6 @@ benchmark bench-e2e
build-depends:
, aeson
, base >=4.7 && <5
, base16-bytestring
, bytestring
, cardano-binary
, containers
, directory
, filepath
Expand Down
55 changes: 22 additions & 33 deletions hydra-cluster/src/Hydra/Generator.hs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Hydra.Prelude hiding (size)
import Cardano.Api.UTxO qualified as UTxO
import CardanoClient (mkGenesisTx)
import Control.Monad (foldM)
import Data.Aeson (object, withObject, (.:), (.=))
import Data.Default (def)
import Hydra.Cluster.Fixture (Actor (Faucet), availableInitialFunds)
import Hydra.Cluster.Util (keysFor)
Expand All @@ -26,18 +27,7 @@ data Dataset = Dataset
, description :: Maybe Text
}
deriving stock (Show, Generic)

instance ToCBOR Dataset where
toCBOR Dataset{fundingTransaction, clientDatasets, title, description} =
mconcat
[ toCBOR fundingTransaction
, toCBOR clientDatasets
, toCBOR title
, toCBOR description
]

instance FromCBOR Dataset where
fromCBOR = Dataset <$> fromCBOR <*> fromCBOR <*> fromCBOR <*> fromCBOR
deriving anyclass (ToJSON, FromJSON)

instance Arbitrary Dataset where
arbitrary = sized $ \n -> do
Expand All @@ -52,36 +42,35 @@ data ClientKeys = ClientKeys
}
deriving stock (Show)

instance Arbitrary ClientKeys where
arbitrary = ClientKeys <$> genSigningKey <*> genSigningKey

instance ToCBOR ClientKeys where
toCBOR ClientKeys{signingKey, externalSigningKey} =
mconcat
[ toCBOR signingKey
, toCBOR externalSigningKey
instance ToJSON ClientKeys where
toJSON ClientKeys{signingKey, externalSigningKey} =
object
[ "signingKey" .= serialiseToTextEnvelope (Just "signingKey") signingKey
, "externalSigningKey" .= serialiseToTextEnvelope (Just "externalSigningKey") externalSigningKey
]

instance FromCBOR ClientKeys where
fromCBOR = ClientKeys <$> fromCBOR <*> fromCBOR
instance FromJSON ClientKeys where
parseJSON =
withObject "ClientKeys" $ \o ->
ClientKeys
<$> (decodeSigningKey =<< o .: "signingKey")
<*> (decodeSigningKey =<< o .: "externalSigningKey")
where
decodeSigningKey v = do
envelope <- parseJSON v
deserialiseFromTextEnvelope (AsSigningKey AsPaymentKey) envelope
& either (fail . show) pure

instance Arbitrary ClientKeys where
arbitrary = ClientKeys <$> genSigningKey <*> genSigningKey

data ClientDataset = ClientDataset
{ clientKeys :: ClientKeys
, initialUTxO :: UTxO
, txSequence :: [Tx]
}
deriving stock (Show, Generic)

instance ToCBOR ClientDataset where
toCBOR ClientDataset{clientKeys, initialUTxO, txSequence} =
mconcat
[ toCBOR clientKeys
, toCBOR initialUTxO
, toCBOR txSequence
]

instance FromCBOR ClientDataset where
fromCBOR = ClientDataset <$> fromCBOR <*> fromCBOR <*> fromCBOR
deriving anyclass (ToJSON, FromJSON)

defaultProtocolParameters :: ProtocolParameters
defaultProtocolParameters = fromLedgerPParams ShelleyBasedEraShelley def
Expand Down
2 changes: 2 additions & 0 deletions hydra-cluster/test/Test/GeneratorSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import Hydra.Ledger.Cardano.Configuration (
LedgerEnv,
newLedgerEnv,
)
import Test.Aeson.GenericSpecs (roundtripSpecs)
import Test.QuickCheck (
Positive (Positive),
Property,
Expand All @@ -33,6 +34,7 @@ import Test.QuickCheck (

spec :: Spec
spec = parallel $ do
roundtripSpecs (Proxy @Dataset)
prop "generates a Dataset that keeps UTXO constant" prop_keepsUTxOConstant

prop_keepsUTxOConstant :: Property
Expand Down

0 comments on commit 6427e5c

Please sign in to comment.