module Benchmark where import Sequenic.CTy.Combinations import Sequenic.CTy.Concretization import qualified ExampleCT1 import qualified ExampleCT2 import Sequenic.CTy.Types import Time import List import System.Directory -- Benchmark testing main :: IO() main = do { -- dir <- getCurrentDirectory ; bmdir <- return "e:/workshop/projects/CTy/v1/benchmark" ; -- bmdir <- return "d:/workshop/PROJECTS/CTy/v1/benchmark" ; putStrLn ("Output dir. = " ++ bmdir) ; alreadyExists <- doesDirectoryExist bmdir ; if alreadyExists then return () else createDirectory bmdir ; benchmark "benchmark-1" (bmdir ++ "/bm1") ExampleCT1.ct ExampleCT1.rules ; benchmark "benchmark-2" (bmdir ++ "/bm2") ExampleCT2.ct ExampleCT2.rules } forceCalculation = sizeSuite_ sizeSuite_ suite = sum . map length $ suite benchmark :: String -> String -> CTree -> [Rule] -> IO () benchmark name outputDir ct rules = do { alreadyExists <- doesDirectoryExist outputDir ; if alreadyExists then return () else createDirectory outputDir ; writeFile summaryf "" ; printx summaryf ("** Running Benchmark " ++ name ++ " ...\n") ; printx summaryf (" " ++ show (nCats ct) ++ " categories, " ++ show (nClasses ct) ++ " classes, " ++ show (nCombs ct) ++ " possible combinations") ; runGenerator "Minimal combination" minComb ; -- runGenerator "Full combination" fullComb ; printx summaryf "\n Minimal ruled-combinations: " ; sequence . map (\(k,r)-> runRuledGenerator k minRuledComb r) $ (zip [1..] rules) ; printx summaryf "\n Full ruled-combinations: " ; sequence . map (\(k,r)-> runRuledGenerator k fullRuledComb r) $ (zip [1..] rules) ; putStrLn "\nFinished" } where summaryf = outputDir ++ "/summary.txt" outf = outputDir ++ "/outf.txt" runRuledGenerator k generator rule = runGenerator ttl gen where gen ct = generator ct rule ttl = " Rule-" ++ show k runGenerator title generator = do { printx summaryf ("\n " ++ title ++ " : ") ; t1 <- getClockTime ; suite <- return (generator ct) ; printx summaryf ((show . length $ suite) ++ " combinations, ") ; printx summaryf ((show . forceCalculation $ suite) ++ " sz, ") ; t2 <- getClockTime ; writeFile outf (showSuite suite) ; t3 <- getClockTime ; printx summaryf ("[" ++ showTimeDifference (diffClockTimes t2 t1) ++ "/" ++ showTimeDifference (diffClockTimes t3 t2) ++ "/" ++ showTimeDifference (diffClockTimes t3 t1) ++ " ms]") ; } showTimeDifference :: TimeDiff -> String showTimeDifference (TimeDiff _ _ _ _ _ sec ps) = if (ps<0) then showTimeAux (sec-1,1000+(div ps 1000000000)) else showTimeAux (sec,(div ps 1000000000)) showTimeAux :: (Int,Integer) -> String -- showTimeAux (sec,ps) = (show sec)++" sec, "++(show ps)++" msec" showTimeAux (sec,ps) = show (1000*(toInteger sec) + ps) -- in milisec printx f s = do { putStr s ; appendFile f s ; }