Skip to content

Commit

Permalink
Take the number of threads from the command line
Browse files Browse the repository at this point in the history
  • Loading branch information
tibbe committed Jan 21, 2010
1 parent 37ae6ef commit ef6ec77
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
2 changes: 1 addition & 1 deletion benchmarks/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ simple: $(lib) Args.o Simple.o
ranlib $(lib)
$(ghc) $(ghc-flags) -threaded -o $@ $(filter %.o,$^) $(lib)

thread-delay: $(lib) ThreadDelay.o
thread-delay: $(lib) Args.o ThreadDelay.o
ranlib $(lib)
$(ghc) $(ghc-flags) -threaded -o $@ $(filter %.o,$^) $(lib)

Expand Down
39 changes: 38 additions & 1 deletion benchmarks/ThreadDelay.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,15 @@
-- Benchmark 'threadDelay' by forking N threads which sleep for a
-- number of milliseconds and wait for them all to finish.

import Args (ljust, parseArgs, positive, theLast)
import Control.Concurrent (forkIO)
import Control.Concurrent.MVar (newEmptyMVar, putMVar, takeMVar)
import Control.Monad (when)
import Data.Function (on)
import Data.Monoid (Monoid(..), Last(..))
import Data.IORef (atomicModifyIORef, newIORef)
import System.Console.GetOpt (ArgDescr(ReqArg), OptDescr(..))
import System.Environment (getArgs)
import System.Event.Thread (ensureIOManagerIsRunning)

#if 1
Expand All @@ -16,10 +21,12 @@ import Control.Concurrent (threadDelay)
#endif

main = do
(cfg, _) <- parseArgs defaultConfig defaultOptions =<< getArgs
let numThreads = theLast cfgNumThreads cfg

ensureIOManagerIsRunning
done <- newEmptyMVar
ref <- newIORef 0
let numThreads = 20000
let loop :: Int -> IO ()
loop i = do
when (i < numThreads) $ do
Expand All @@ -30,3 +37,33 @@ main = do
loop (i + 1)
loop 0
takeMVar done

------------------------------------------------------------------------
-- Configuration

data Config = Config {
cfgNumThreads :: Last Int
}

defaultConfig :: Config
defaultConfig = Config
{ cfgNumThreads = ljust 1000
}

instance Monoid Config where
mempty = Config
{ cfgNumThreads = mempty
}

mappend a b = Config
{ cfgNumThreads = app cfgNumThreads a b
}
where app = on mappend

defaultOptions :: [OptDescr (IO Config)]
defaultOptions = [
Option ['n'] ["threads"]
(ReqArg (positive "number of threads" $ \n ->
mempty { cfgNumThreads = n }) "N")
"number of threads"
]

0 comments on commit ef6ec77

Please sign in to comment.