{-# LANGUAGE RankNTypes #-} module Data.Conduit.Timed (timedConduit) where import Data.Time.Clock import Data.Conduit import Control.Applicative import Control.Monad.IO.Class -- | The timedConduit measures the elapsed time between the first -- input being received on this pipe and the upstream close. The -- amount of elapsed time is added to the Pipe result time in a tuple -- along with the upstream result time. -- -- Example: -- > sourceFile "testfile.txt" >+> timedConduit >+> sinkFile "testfile.cpy" >+> (await >>= print . snd) -- timedConduit :: MonadResource m => forall l o u . Pipe l o o u m (u, NominalDiffTime) timedConduit = bracketP getCurrentTime (\_ -> return ()) inner where inner st = do r <- awaitE case r of Right x -> yield x >> inner st Left r -> deltaTime st >>= \t -> return (r,t) deltaTime st = liftIO $ flip diffUTCTime st <$> getCurrentTime