Permalink
Browse files

Take the number of threads from the command line

  • Loading branch information...
1 parent 37ae6ef commit ef6ec773a3cd6d27c527472cddf24d7a47c0a620 @tibbe committed Jan 21, 2010
Showing with 39 additions and 2 deletions.
  1. +1 −1 benchmarks/Makefile
  2. +38 −1 benchmarks/ThreadDelay.hs
View
@@ -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)
View
@@ -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
@@ -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
@@ -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.