Skip to content

Instantly share code, notes, and snippets.

@seanparsons
Created March 21, 2015 01:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save seanparsons/8966b2622d3cb4be8ebe to your computer and use it in GitHub Desktop.
Save seanparsons/8966b2622d3cb4be8ebe to your computer and use it in GitHub Desktop.
Batcher test.
import Control.Concurrent.Lifted
import Control.Monad.Base
import Data.IORef.Lifted
import Data.Traversable
import qualified Data.HashSet as S
import qualified Data.HashMap.Strict as M
import System.IO.Unsafe
import Debug.Trace
:set prompt "ghci> "
let coordinationRef = unsafePerformIO $ newIORef True
let service = (\rs -> (putStr "Looking up: " >> print rs >> putStrLn "") >> (return $ M.fromList $ fmap (\r -> (r, r * 2)) $ S.toList rs)) :: MultiGetService IO Int Int
let temp = batchingService defaultBatchingOptions service
let (singleService, multiService) = unsafePerformIO temp
let test keys = fork (multiService keys >>= (\r -> atomicModifyIORef' coordinationRef (\_ -> (True, traceShow r ()))))
traverse test [S.fromList [1, 2, 3], S.fromList [2, 3, 4], S.fromList [3, 4, 5], S.fromList [1, 3, 5, 7, 9, 11]]
{-
The above prints out this:
Looking up: fromList [1,2,3,4,5,7,9,11]
fromList [(1,2),(3,6),(5,10),(7,14),(9,18),(11,22)]
fromList [(3,6),(4,8),(5,10)]
fromList [(2,4),(3,6),(4,8)]
fromList [(1,2),(2,4),(3,6)]
-}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment