packages feed

filepather-0.6.0: benchmarks/Main.hs

{-# OPTIONS_GHC -Wall -Werror #-}

module Main (main) where

import Data.Functor.Identity (Identity (..))
import Data.Kleisli (Kleisli (..), mkKleisli')
import qualified System.Directory as Dir
import qualified System.FilePath as FP
import System.FilePather.FilePather
  ( FilePatherA',
    addExtension,
    addTrailingPathSeparator,
    combine,
    dropExtension,
    dropTrailingPathSeparator,
    hasExtension,
    isAbsolute,
    isRelative,
    makeRelative,
    makeValid,
    normalise,
    splitDirectories,
    splitExtension,
    splitFileName,
    splitPath,
    takeBaseName,
    takeDirectory,
    takeExtension,
    takeFileName,
  )
import qualified System.FilePather.FilePather as FPR
import Test.Tasty.Bench
  ( bench,
    bgroup,
    defaultMain,
    nf,
    nfIO,
    whnfIO,
  )

run :: FilePatherA' a -> FilePath -> a
run (Kleisli f) = runIdentity . f

shortPath :: FilePath
shortPath = "/tmp/foo.hs"

deepPath :: FilePath
deepPath = "/home/user/projects/haskell/filepather/src/System/FilePather/FilePather.hs"

multiExtPath :: FilePath
multiExtPath = "/var/log/archive/system.log.2024-01-15.tar.gz"

main :: IO ()
main =
  defaultMain
    [ bgroup
        "splitExtension"
        [ bench "filepather/short" $ nf (run splitExtension) shortPath,
          bench "filepath/short" $ nf FP.splitExtension shortPath,
          bench "filepather/deep" $ nf (run splitExtension) deepPath,
          bench "filepath/deep" $ nf FP.splitExtension deepPath
        ],
      bgroup
        "takeExtension"
        [ bench "filepather/short" $ nf (run takeExtension) shortPath,
          bench "filepath/short" $ nf FP.takeExtension shortPath,
          bench "filepather/deep" $ nf (run takeExtension) deepPath,
          bench "filepath/deep" $ nf FP.takeExtension deepPath
        ],
      bgroup
        "takeFileName"
        [ bench "filepather/short" $ nf (run takeFileName) shortPath,
          bench "filepath/short" $ nf FP.takeFileName shortPath,
          bench "filepather/deep" $ nf (run takeFileName) deepPath,
          bench "filepath/deep" $ nf FP.takeFileName deepPath
        ],
      bgroup
        "takeDirectory"
        [ bench "filepather/short" $ nf (run takeDirectory) shortPath,
          bench "filepath/short" $ nf FP.takeDirectory shortPath,
          bench "filepather/deep" $ nf (run takeDirectory) deepPath,
          bench "filepath/deep" $ nf FP.takeDirectory deepPath
        ],
      bgroup
        "takeBaseName"
        [ bench "filepather/short" $ nf (run takeBaseName) shortPath,
          bench "filepath/short" $ nf FP.takeBaseName shortPath,
          bench "filepather/deep" $ nf (run takeBaseName) deepPath,
          bench "filepath/deep" $ nf FP.takeBaseName deepPath
        ],
      bgroup
        "splitFileName"
        [ bench "filepather/short" $ nf (run splitFileName) shortPath,
          bench "filepath/short" $ nf FP.splitFileName shortPath,
          bench "filepather/deep" $ nf (run splitFileName) deepPath,
          bench "filepath/deep" $ nf FP.splitFileName deepPath
        ],
      bgroup
        "splitPath"
        [ bench "filepather/short" $ nf (run splitPath) shortPath,
          bench "filepath/short" $ nf FP.splitPath shortPath,
          bench "filepather/deep" $ nf (run splitPath) deepPath,
          bench "filepath/deep" $ nf FP.splitPath deepPath
        ],
      bgroup
        "splitDirectories"
        [ bench "filepather/short" $ nf (run splitDirectories) shortPath,
          bench "filepath/short" $ nf FP.splitDirectories shortPath,
          bench "filepather/deep" $ nf (run splitDirectories) deepPath,
          bench "filepath/deep" $ nf FP.splitDirectories deepPath
        ],
      bgroup
        "normalise"
        [ bench "filepather" $ nf (run normalise) "/tmp/./foo/../bar//baz.hs",
          bench "filepath" $ nf FP.normalise "/tmp/./foo/../bar//baz.hs"
        ],
      bgroup
        "dropExtension"
        [ bench "filepather" $ nf (run dropExtension) multiExtPath,
          bench "filepath" $ nf FP.dropExtension multiExtPath
        ],
      bgroup
        "addExtension"
        [ bench "filepather" $ nf (run (addExtension ".bak")) "/tmp/foo",
          bench "filepath" $ nf (`FP.addExtension` ".bak") "/tmp/foo"
        ],
      bgroup
        "hasExtension"
        [ bench "filepather" $ nf (run hasExtension) shortPath,
          bench "filepath" $ nf FP.hasExtension shortPath
        ],
      bgroup
        "isAbsolute"
        [ bench "filepather" $ nf (run isAbsolute) shortPath,
          bench "filepath" $ nf FP.isAbsolute shortPath
        ],
      bgroup
        "isRelative"
        [ bench "filepather" $ nf (run isRelative) "foo/bar.hs",
          bench "filepath" $ nf FP.isRelative "foo/bar.hs"
        ],
      bgroup
        "makeRelative"
        [ bench "filepather" $ nf (run (makeRelative "/home/user")) "/home/user/projects/foo.hs",
          bench "filepath" $ nf (FP.makeRelative "/home/user") "/home/user/projects/foo.hs"
        ],
      bgroup
        "combine"
        [ bench "filepather" $ nf (run (combine "/tmp")) "foo.hs",
          bench "filepath" $ nf (FP.combine "/tmp") "foo.hs"
        ],
      bgroup
        "makeValid"
        [ bench "filepather" $ nf (run makeValid) "",
          bench "filepath" $ nf FP.makeValid ""
        ],
      bgroup
        "addTrailingPathSeparator"
        [ bench "filepather" $ nf (run addTrailingPathSeparator) "/tmp",
          bench "filepath" $ nf FP.addTrailingPathSeparator "/tmp"
        ],
      bgroup
        "dropTrailingPathSeparator"
        [ bench "filepather" $ nf (run dropTrailingPathSeparator) "/tmp/",
          bench "filepath" $ nf FP.dropTrailingPathSeparator "/tmp/"
        ],
      bgroup
        "composition"
        [ bench "filepather/takeDir-then-addExt" $
            nf (run (addExtension ".bak") . run takeDirectory) deepPath,
          bench "filepath/takeDir-then-addExt" $
            nf ((`FP.addExtension` ".bak") . FP.takeDirectory) deepPath,
          bench "filepather/kleisli-compose" $
            nf (run (mkKleisli' (run (addExtension ".bak") . run takeDirectory))) deepPath
        ],
      bgroup
        "IO/doesDirectoryExist"
        [ bench "filepather" $ nfIO (let Kleisli f = FPR.doesDirectoryExist in f "/tmp"),
          bench "directory" $ nfIO (Dir.doesDirectoryExist "/tmp")
        ],
      bgroup
        "IO/doesFileExist"
        [ bench "filepather" $ nfIO (let Kleisli f = FPR.doesFileExist in f "/tmp/nonexistent_bench_file"),
          bench "directory" $ nfIO (Dir.doesFileExist "/tmp/nonexistent_bench_file")
        ],
      bgroup
        "IO/canonicalizePath"
        [ bench "filepather" $ nfIO (let Kleisli f = FPR.canonicalizePath in f "/tmp"),
          bench "directory" $ nfIO (Dir.canonicalizePath "/tmp")
        ],
      bgroup
        "IO/getPermissions"
        [ bench "filepather" $ whnfIO (let Kleisli f = FPR.getPermissions in f "/tmp"),
          bench "directory" $ whnfIO (Dir.getPermissions "/tmp")
        ]
    ]