glue-example (empty) → 0.4.1
raw patch · 5 files changed
+124/−0 lines, 5 filesdep +asyncdep +basedep +ekg-coresetup-changed
Dependencies added: async, base, ekg-core, glue-common, glue-core, glue-ekg, hashable, lifted-base, monad-control, monad-loops, text, time, transformers, transformers-base, unordered-containers
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- glue-example.cabal +36/−0
- src/Glue/Example/BatcherExample.hs +49/−0
- src/Main.hs +7/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2015, Sean Parsons++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of Sean Parsons nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ glue-example.cabal view
@@ -0,0 +1,36 @@+name: glue-example+version: 0.4.1+synopsis: Make better services.+description: Examples of the use of glue library functionality.+license: BSD3+license-file: LICENSE+author: Sean Parsons+maintainer: github@futurenotfound.com+category: Network+build-type: Simple+cabal-version: >=1.10++source-repository head+ type: git+ location: git://github.com/seanparsons/glue.git++executable glue-example+ main-is: Main.hs+ hs-source-dirs: src+ other-modules: Glue.Example.BatcherExample+ build-depends: base ==4.*,+ glue-common ==0.4.1,+ glue-core ==0.4.1,+ glue-ekg ==0.4.1,+ transformers,+ transformers-base,+ lifted-base,+ time,+ monad-control,+ unordered-containers,+ hashable,+ ekg-core,+ text,+ monad-loops,+ async+ default-language: Haskell2010
+ src/Glue/Example/BatcherExample.hs view
@@ -0,0 +1,49 @@+{-# LANGUAGE OverloadedStrings #-}+{-# LANGUAGE FlexibleContexts #-}++module Glue.Example.BatcherExample(+ batcherExample+) where++import Glue.Batcher+import Glue.Ekg+import Control.Concurrent.Async+import Control.Concurrent.Lifted+import Data.IORef.Lifted+import qualified Data.List as L+import Data.Text+import Data.Traversable+import System.Metrics+import qualified System.Metrics.Distribution as D+import qualified Data.HashSet as S+import qualified Data.HashMap.Strict as M+import Text.Printf++printStats :: Store -> Text -> IO ()+printStats store name = do+ samples <- sampleAll store+ let possibleValue = M.lookup name samples+ case possibleValue of+ (Just (Distribution stats)) -> printf "%s - sum : %f - count : %d" (unpack name) (D.sum stats) (D.count stats) >> putStrLn ""+ otherwise -> return ()++runTest :: Store -> Text -> Bool -> IO ()+runTest store name shouldBatch = do+ counter <- newIORef 0+ let listOfNums = [1..50]+ let requests = fmap (S.fromList . L.take 10) $ L.tails listOfNums :: [S.HashSet Int]+ let service request = do+ atomicModifyIORef' counter (\c -> (c + (S.size request), ()))+ threadDelay (500 * S.size request)+ return $ M.fromList $ fmap (\r -> (r, r * 2)) $ S.toList request+ statsWrappedService <- recordDistribution store name service+ possiblyBatchedService <- if shouldBatch then return statsWrappedService else fmap (\(_, b) -> b) $ batchingService defaultBatchingOptions statsWrappedService+ (mapConcurrently possiblyBatchedService requests)+ printStats store name++-- Tests the batchingService by comparing the stats with and without it.+batcherExample :: IO ()+batcherExample = do+ store <- newStore+ runTest store "Unbatched Service" False+ runTest store "Batched Service" True
+ src/Main.hs view
@@ -0,0 +1,7 @@+module Main where++import Glue.Example.BatcherExample++main :: IO ()+main = do+ batcherExample