diff --git a/trace-resources/bench/trace-resources-bench.hs b/trace-resources/bench/trace-resources-bench.hs new file mode 100644 index 00000000000..57509904db9 --- /dev/null +++ b/trace-resources/bench/trace-resources-bench.hs @@ -0,0 +1,22 @@ +{-# LANGUAGE CPP #-} + +import Criterion.Main +import Criterion.Types + + +#if defined(linux_HOST_OS) +import qualified Cardano.Logging.Resources.Linux as Platform +#elif defined(mingw32_HOST_OS) +import qualified Cardano.Logging.Resources.Windows as Platform +#elif defined(darwin_HOST_OS) +import qualified Cardano.Logging.Resources.Darwin as Platform +#else +import qualified Cardano.Logging.Resources.Dummy as Platform +#endif + + +main :: IO () +main = + defaultMainWith defaultConfig{ timeLimit = 15 } + [ bench "create record ResourceStats" (whnfIO Platform.readResourceStatsInternal) + ] diff --git a/trace-resources/src/Cardano/Logging/Resources/Linux.hs b/trace-resources/src/Cardano/Logging/Resources/Linux.hs index c18e6fe6701..0c02a9be047 100644 --- a/trace-resources/src/Cardano/Logging/Resources/Linux.hs +++ b/trace-resources/src/Cardano/Logging/Resources/Linux.hs @@ -8,10 +8,12 @@ module Cardano.Logging.Resources.Linux import Cardano.Logging.Resources.Types import Data.Maybe (fromMaybe) +import qualified Data.Text as T +import qualified Data.Text.IO as T (readFile) +import qualified Data.Text.Read as T (decimal) import Data.Word import qualified GHC.Stats as GhcStats import System.Posix.Files (fileMode, getFileStatus, intersectFileModes, ownerReadMode) -import Text.Read (readMaybe) -- * Disk IO stats: -- /proc/[pid]/io (since kernel 2.6.20) @@ -146,9 +148,15 @@ readProcList fp = do fs <- getFileStatus fp if readable fs then do - cs <- readFile fp - return $ map (\s -> fromMaybe 0 (readMaybe s :: Maybe Integer)) (words cs) + cs <- T.readFile fp + return $ map (fromMaybe 0 . readMaybeText) (T.words cs) else return [] where readable fs = intersectFileModes (fileMode fs) ownerReadMode == ownerReadMode + +readMaybeText :: Integral a => T.Text -> Maybe a +readMaybeText t = + case T.decimal t of + Right (v, _) -> Just v + _ -> Nothing diff --git a/trace-resources/trace-resources.cabal b/trace-resources/trace-resources.cabal index e48db579c60..fe3e6deac01 100644 --- a/trace-resources/trace-resources.cabal +++ b/trace-resources/trace-resources.cabal @@ -1,7 +1,7 @@ cabal-version: 3.0 name: trace-resources -version: 0.2.0.2 +version: 0.2.1.0 synopsis: Package for tracing resources for linux, mac and windows description: Package for tracing resources for linux, mac and windows. category: Cardano, @@ -84,3 +84,17 @@ test-suite trace-resources-test -Wredundant-constraints -Wmissing-export-lists -Wno-incomplete-patterns + +benchmark bench + import: project-config + type: exitcode-stdio-1.0 + main-is: trace-resources-bench.hs + hs-source-dirs: bench + build-depends: base >=4.12 && <5 + , criterion + , trace-resources + ghc-options: + -Wall -Wcompat -Wincomplete-record-updates + -Wincomplete-uni-patterns -Wpartial-fields -Wredundant-constraints + -Wunused-packages -threaded -rtsopts -O2 + "-with-rtsopts=-T"