Skip to content

Commit

Permalink
Add tx-cost-diff
Browse files Browse the repository at this point in the history
  • Loading branch information
locallycompact committed Oct 23, 2024
1 parent b718600 commit e7879b4
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 33 deletions.
35 changes: 2 additions & 33 deletions .github/workflows/ci-nix.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
14 changes: 14 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down
File renamed without changes.
80 changes: 80 additions & 0 deletions scripts/tx-cost-diff.hs
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e7879b4

Please sign in to comment.