Skip to content

Commit

Permalink
Add fact eg for profiling.
Browse files Browse the repository at this point in the history
  • Loading branch information
thealmarty committed Sep 17, 2021
1 parent cdb102c commit 0a3e2a8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
11 changes: 11 additions & 0 deletions plutus-tx-plugin/executables/profile/Main.hs
Expand Up @@ -9,6 +9,7 @@
{-# OPTIONS_GHC -fplugin-opt PlutusTx.Plugin:profile-all #-}

-- | Executable for profiling. Add the program you want to profile here.
-- Plugin options only work in this file so you have to define the programs here.

module Main where
import Common
Expand All @@ -29,6 +30,15 @@ import Prettyprinter.Internal (pretty)
import Prettyprinter.Render.Text (hPutDoc)
import System.IO (IOMode (WriteMode), withFile)

fact :: Integer -> Integer
fact n =
if Builtins.equalsInteger n 0
then 1
else Builtins.multiplyInteger n (fact (Builtins.subtractInteger n 1))

factTest :: CompiledCode (Integer -> Integer)
factTest = plc (Proxy @"fact") fact

fib :: Integer -> Integer
fib n = if Builtins.equalsInteger n 0
then 0
Expand Down Expand Up @@ -91,6 +101,7 @@ writeLogToFile fileName values = do
main :: IO ()
main = do
writeLogToFile "fib4" [toUPlc fibTest, toUPlc $ plc (Proxy @"4") (4::Integer)]
writeLogToFile "fact4" [toUPlc factTest, toUPlc $ plc (Proxy @"4") (4::Integer)]
writeLogToFile "addInt" [toUPlc addIntTest]
writeLogToFile "addInt3" [toUPlc addIntTest, toUPlc $ plc (Proxy @"3") (3::Integer)]
writeLogToFile "letInFun" [toUPlc letInFunTest, toUPlc $ plc (Proxy @"1") (1::Integer), toUPlc $ plc (Proxy @"4") (4::Integer)]
Expand Down
15 changes: 14 additions & 1 deletion plutus-tx-plugin/test/Plugin/Profiling/Spec.hs
Expand Up @@ -29,6 +29,9 @@ import Control.Lens.Combinators (_2)
import Control.Lens.Getter (view)
import Data.Proxy
import Data.Text (Text)
import qualified PlutusCore.Default as Builtins
import qualified PlutusTx.Builtins as Builtins
import Prelude
import Prettyprinter.Internal (pretty)
import Prettyprinter.Render.Text (hPutDoc)
import System.IO (IOMode (WriteMode), withFile)
Expand All @@ -37,6 +40,7 @@ profiling :: TestNested
profiling = testNested "Profiling" [
goldenUEvalProfile "fib" [toUPlc fibTest]
, goldenUEvalProfile "fib4" [toUPlc fibTest, toUPlc $ plc (Proxy @"4") (4::Integer)]
, goldenUEvalProfile "fact4" [toUPlc factTest, toUPlc $ plc (Proxy @"4") (4::Integer)]
, goldenUEvalProfile "addInt" [toUPlc addIntTest]
, goldenUEvalProfile "addInt3" [toUPlc addIntTest, toUPlc $ plc (Proxy @"3") (3::Integer)]
, goldenUEvalProfile "letInFun" [toUPlc letInFunTest, toUPlc $ plc (Proxy @"1") (1::Integer), toUPlc $ plc (Proxy @"4") (4::Integer)]
Expand All @@ -46,6 +50,15 @@ profiling = testNested "Profiling" [
, goldenUEvalProfile "swap" [toUPlc swapTest]
]

fact :: Integer -> Integer
fact n =
if Builtins.equalsInteger n 0
then 1
else Builtins.multiplyInteger n (fact (Builtins.subtractInteger n 1))

factTest :: CompiledCode (Integer -> Integer)
factTest = plc (Proxy @"fact") fact

fib :: Integer -> Integer
fib n = if Builtins.equalsInteger n 0
then 0
Expand Down Expand Up @@ -86,5 +99,5 @@ idTest = plc (Proxy @"id") (id (1::Integer))
swap :: (a,b) -> (b,a)
swap (a,b) = (b,a)

swapTest :: CompiledCode (Integer,Bool)
swapTest :: CompiledCode (Integer, Bool)
swapTest = plc (Proxy @"swap") (swap (True,1))
36 changes: 36 additions & 0 deletions plutus-tx-plugin/test/Plugin/Profiling/fact4.plc.golden
@@ -0,0 +1,36 @@
[ entering fact
, entering equalsInteger
, exiting equalsInteger
, entering subtractInteger
, exiting subtractInteger
, entering fact
, entering equalsInteger
, exiting equalsInteger
, entering subtractInteger
, exiting subtractInteger
, entering fact
, entering equalsInteger
, exiting equalsInteger
, entering subtractInteger
, exiting subtractInteger
, entering fact
, entering equalsInteger
, exiting equalsInteger
, entering subtractInteger
, exiting subtractInteger
, entering fact
, entering equalsInteger
, exiting equalsInteger
, exiting fact
, entering multiplyInteger
, exiting multiplyInteger
, exiting fact
, entering multiplyInteger
, exiting multiplyInteger
, exiting fact
, entering multiplyInteger
, exiting multiplyInteger
, exiting fact
, entering multiplyInteger
, exiting multiplyInteger
, exiting fact ]

0 comments on commit 0a3e2a8

Please sign in to comment.