From 0a3e2a826f0f8caa4edc54bbdea588b82f10a146 Mon Sep 17 00:00:00 2001 From: Marty Stumpf Date: Thu, 16 Sep 2021 12:20:04 -0700 Subject: [PATCH] Add fact eg for profiling. --- plutus-tx-plugin/executables/profile/Main.hs | 11 ++++++ .../test/Plugin/Profiling/Spec.hs | 15 +++++++- .../test/Plugin/Profiling/fact4.plc.golden | 36 +++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 plutus-tx-plugin/test/Plugin/Profiling/fact4.plc.golden diff --git a/plutus-tx-plugin/executables/profile/Main.hs b/plutus-tx-plugin/executables/profile/Main.hs index 1dbcc86f49f..8c98629ab54 100644 --- a/plutus-tx-plugin/executables/profile/Main.hs +++ b/plutus-tx-plugin/executables/profile/Main.hs @@ -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 @@ -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 @@ -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)] diff --git a/plutus-tx-plugin/test/Plugin/Profiling/Spec.hs b/plutus-tx-plugin/test/Plugin/Profiling/Spec.hs index 7db4110adf8..b47d678ef67 100644 --- a/plutus-tx-plugin/test/Plugin/Profiling/Spec.hs +++ b/plutus-tx-plugin/test/Plugin/Profiling/Spec.hs @@ -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) @@ -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)] @@ -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 @@ -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)) diff --git a/plutus-tx-plugin/test/Plugin/Profiling/fact4.plc.golden b/plutus-tx-plugin/test/Plugin/Profiling/fact4.plc.golden new file mode 100644 index 00000000000..4752d376629 --- /dev/null +++ b/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 ] \ No newline at end of file