1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
module Main where
import qualified Codec.Archive.Tar as Tar
import qualified Codec.Archive.Tar.Index as TarIndex
import qualified Data.ByteString.Lazy as BS
import Data.Maybe
import Control.Exception
import System.Directory
import System.Environment
import System.IO.Temp
import Test.Tasty.Bench
main = defaultMain benchmarks
benchmarks :: [Benchmark]
benchmarks =
[ env loadTarFile $ \tarfile ->
bench "read" (nf Tar.read tarfile)
, env loadTarEntriesList $ \entries ->
bench "write" (nf Tar.write entries)
, env loadTarEntries $ \entries ->
bench "index build" (nf TarIndex.build entries)
, env loadTarIndex $ \entries ->
bench "index rebuild" (nf (TarIndex.finalise . TarIndex.unfinalise) entries)
, env loadTarEntries $ \entries ->
bench "unpack" (nfIO $ withSystemTempDirectory "tar-bench" $ \baseDir -> Tar.unpack baseDir entries)
, env (fmap TarIndex.serialise loadTarIndex) $ \tarfile ->
bench "deserialise index" (nf TarIndex.deserialise tarfile)
]
loadTarFile :: IO BS.ByteString
loadTarFile = do
mTarFile <- lookupEnv "TAR_TEST_FILE"
let tarFile = fromMaybe "01-index.tar" mTarFile
exists <- doesFileExist tarFile
if exists
then BS.readFile tarFile
else case mTarFile of
Just _ -> error $ tarFile <> " does not exist"
Nothing -> error "01-index.tar does not exist, copy it from ~/.cabal/packages/hackage.haskell.org/01-index.tar"
loadTarEntries :: IO (Tar.Entries Tar.FormatError)
loadTarEntries =
fmap Tar.read loadTarFile
loadTarEntriesList :: IO [Tar.Entry]
loadTarEntriesList =
fmap (Tar.foldEntries (:) [] throw) loadTarEntries
loadTarIndex :: IO TarIndex.TarIndex
loadTarIndex =
fmap (either throw id . TarIndex.build)
loadTarEntries
|