From e7879b403080b1437bdf602fa9f72c24d5f13026 Mon Sep 17 00:00:00 2001 From: Daniel Firth Date: Tue, 22 Oct 2024 12:49:06 +0000 Subject: [PATCH] Add tx-cost-diff --- .github/workflows/ci-nix.yaml | 35 +------- flake.nix | 14 ++++ .../cost-differences => scripts}/diff.py | 0 scripts/tx-cost-diff.hs | 80 +++++++++++++++++++ 4 files changed, 96 insertions(+), 33 deletions(-) rename {.github/workflows/cost-differences => scripts}/diff.py (100%) create mode 100755 scripts/tx-cost-diff.hs diff --git a/.github/workflows/ci-nix.yaml b/.github/workflows/ci-nix.yaml index d5b3e2f16b3..f14d31e4995 100644 --- a/.github/workflows/ci-nix.yaml +++ b/.github/workflows/ci-nix.yaml @@ -371,15 +371,6 @@ jobs: steps: - name: "Checkout the PR as the 'new' source" uses: actions/checkout@v4 - with: - path: ./new - ref: ${{ github.event.pull_request.head.sha }} - - - name: "Checkout `master` as the 'old' source" - uses: actions/checkout@v4 - with: - path: ./old - ref: master - name: ❄ Prepare nix uses: cachix/install-nix-action@v30 @@ -397,33 +388,11 @@ jobs: - name: Set up and use the "ci" devShell uses: nicknovitski/nix-develop@v1 with: - arguments: "./new#costDifferences" - - - name: "Compute costs on both old and new" - run: | - SEED=$RANDOM - echo "Using random seed $SEED" - nix run ./new/#tx-cost -- --seed $SEED >new.md - nix run ./old/#tx-cost -- --seed $SEED >old.md + arguments: ".#costDifferences" - name: "Compute the difference markdown" run: | - # Convert to HTML, as that's the easiest way to read the markdown - # tables via pandas (!!!) - pandoc -i new.md -o new.html - pandoc -i old.md -o old.html - - cat new.md | grep '##' >new-headers.txt - cat old.md | grep '##' >old-headers.txt - - # Stop if the heading columns aren't the same. - cmp old-headers.txt new-headers.txt - - # Run the diff script; note that it's located in the "new" folder. - ./new/.github/workflows/cost-differences/diff.py \ - old-headers.txt \ - old.html \ - new.html >diff.md + nix run ".#tx-cost-diff" - name: 🔎 Find Comment uses: peter-evans/find-comment@v3 diff --git a/flake.nix b/flake.nix index 596a53ceb5a..043472f758c 100644 --- a/flake.nix +++ b/flake.nix @@ -127,6 +127,19 @@ value = addWerror v; }) x.components."${y}") [ "benchmarks" "exes" "sublibs" "tests" ]); + + tx-cost-diff = + let + pyEnv = (pkgs.python3.withPackages (ps: with ps; [ pandas html5lib beautifulsoup4 tabulate ])); + in + pkgs.writers.writeHaskellBin + "tx-cost-diff" + { + libraries = + with pkgs.haskellPackages; + [ aeson text bytestring lens lens-aeson shh ]; + } ''${builtins.readFile scripts/tx-cost-diff.hs}''; + in { legacyPackages = pkgs // hsPkgs; @@ -135,6 +148,7 @@ hydraPackages // (if pkgs.stdenv.isLinux then (prefixAttrs "docker-" hydraImages) else { }) // { spec = inputs.hydra-spec.packages.${system}.default; + inherit tx-cost-diff; }; process-compose."demo" = import ./nix/hydra/demo.nix { inherit system pkgs inputs self; diff --git a/.github/workflows/cost-differences/diff.py b/scripts/diff.py similarity index 100% rename from .github/workflows/cost-differences/diff.py rename to scripts/diff.py diff --git a/scripts/tx-cost-diff.hs b/scripts/tx-cost-diff.hs new file mode 100755 index 00000000000..810d972c2fb --- /dev/null +++ b/scripts/tx-cost-diff.hs @@ -0,0 +1,80 @@ +{-# LANGUAGE ExtendedDefaultRules #-} +{-# LANGUAGE OverloadedStrings #-} + +import Control.Lens (asIndex, to, (&), (^.), (^?)) +import Data.Aeson +import qualified Data.Aeson.Key as Key +import Data.Aeson.Lens +import Data.ByteString.Lazy (ByteString) +import qualified Data.ByteString.Lazy as LBS +import Data.Maybe (fromJust) +import Data.String +import Data.Text (Text) +import qualified Data.Text.Lazy as Text +import Data.Text.Lazy.Encoding (decodeLatin1) +import Shh +import System.Environment + +runRemoteTxCost :: ByteString -> Integer -> IO ByteString +runRemoteTxCost revision seed = do + exe + "nix" + "run" + ("git+https://github.com/cardano-scaling/hydra?" <> revision <> "#tx-cost") + "--" + "--seed" + seed + |> capture + +runLocalTxCost :: Integer -> IO ByteString +runLocalTxCost seed = do + exe + "nix" + "run" + ".#tx-cost" + "--" + "--seed" + seed + |> capture + +runPandoc :: FilePath -> FilePath -> IO ByteString +runPandoc i o = do + exe + "pandoc" + "-i" + i + "-o" + o + |> capture + +extractHeaders :: ByteString -> IO ByteString +extractHeaders md = do + exe "echo" md |> exe "grep" "##" |> capture + +runPandasDiff :: FilePath -> FilePath -> FilePath -> IO ByteString +runPandasDiff headers old new = do + exe + "./scripts/diff.py" + headers + old + new + |> capture + +main :: IO () +main = do + args <- getArgs + let revision = + case args of + [x] -> "rev=" <> x + _ -> "ref=master" + a <- runLocalTxCost 0 + b <- runRemoteTxCost (fromString revision) 0 + LBS.writeFile "new.md" a + LBS.writeFile "old.md" b + runPandoc "new.md" "new.html" + runPandoc "old.md" "old.html" + x <- extractHeaders a + y <- extractHeaders b + LBS.writeFile "new-headers.txt" x + k <- runPandasDiff "new-headers.txt" "old.html" "new.html" + LBS.writeFile "diff.md" k