Skip to content

Draft: Only read stdout when running with --more-verbose #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 24 additions & 8 deletions src/QuickBench.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ import Data.Maybe
import Data.Time.Clock
import Data.Time.Format
import Data.Time.LocalTime
import GHC.IO.Exception (IOErrorType(..))
import Safe
import System.Console.Docopt
import System.Directory
import System.Environment
import System.Exit
import System.IO
import System.IO.Error (mkIOError)
import System.Process
import Text.Megaparsec (ParsecT, Stream (Token), between, many, noneOf, runParser, satisfy, sepBy, takeWhile1P, (<|>))
import Text.Megaparsec.Char (char)
Expand Down Expand Up @@ -219,17 +221,31 @@ time :: Opts -> String -> [String] -> IO Float
time opts exe args = do
dbg opts $ printf "running: %s\n" (show (exe,args))
t1 <- getCurrentTime
(c, o, e) <- readProcessWithExitCode' exe args ""
maybeOutput <- if (moreVerbose opts)
then Just <$> readCreateProcess (proc exe args) ""
else const Nothing <$> callProcessIgnoreOutput exe args
t2 <- getCurrentTime
when (not $ null o) $ outvv opts $ (if verbose opts then "\n" else "") ++ o
unless (c == ExitSuccess) $ out opts $ " (error: " ++ strip e ++ ") "
case maybeOutput of
Just o -> when (not $ null o) $ outvv opts $ (if verbose opts then "\n" else "") ++ o
Nothing -> return ()
return $ realToFrac $ diffUTCTime t2 t1

-- ^ This variant also returns a failure when the executable is missing.
readProcessWithExitCode' :: FilePath -> [String] -> String -> IO (ExitCode, String, String)
readProcessWithExitCode' exe args inp =
readProcessWithExitCode exe args inp
`catch` \(e :: IOException) -> return (ExitFailure 1, "", show e)
callProcessIgnoreOutput :: FilePath -> [String] -> IO ()
callProcessIgnoreOutput cmd args =
withBinaryFile "/dev/null" WriteMode $ \devNull ->
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is a unixism, unfortunately ? quickbench should be robust on Windows too.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be sure: supporting only WSL is not sufficient?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, supporting only WSL is not sufficient.

withCreateProcess (proc cmd args){std_out = UseHandle devNull} $ \_ _ _ ph -> do
exit_code <- waitForProcess ph
case exit_code of
ExitSuccess -> return ()
ExitFailure r -> processFailedException "callProcessIgnoreOutput" cmd args r

-- Copy/paste from "process" System.Process.
processFailedException :: String -> String -> [String] -> Int -> IO a
processFailedException fun cmd args exit_code =
ioError (mkIOError OtherError (fun ++ ": " ++ cmd ++
concatMap ((' ':) . show) args ++
" (exit " ++ show exit_code ++ ")")
Nothing Nothing)

printSummary :: Opts -> [String] -> [String] -> Int -> [[[Float]]] -> IO ()
printSummary opts cmds exes cyc results = do
Expand Down