summaryrefslogtreecommitdiff
path: root/bittorrent/bench/Main.hs
diff options
context:
space:
mode:
Diffstat (limited to 'bittorrent/bench/Main.hs')
-rw-r--r--bittorrent/bench/Main.hs75
1 files changed, 75 insertions, 0 deletions
diff --git a/bittorrent/bench/Main.hs b/bittorrent/bench/Main.hs
new file mode 100644
index 00000000..f04485ab
--- /dev/null
+++ b/bittorrent/bench/Main.hs
@@ -0,0 +1,75 @@
1{-# LANGUAGE OverloadedStrings #-}
2{-# LANGUAGE MultiParamTypeClasses #-}
3{-# OPTIONS -fno-warn-orphans #-}
4module Main (main) where
5
6import Control.DeepSeq
7import Network
8import Control.Monad
9import Control.Monad.Logger
10import Control.Monad.Reader
11import Criterion.Main
12import Data.ByteString as BS
13import Network.DatagramServer
14
15
16import Network.BitTorrent.Exchange.Protocol as BT
17import Data.Torrent.Block as BT
18import Data.Torrent.Bitfield as BT
19
20instance KRPC ByteString ByteString where
21 method = "echo"
22
23instance MonadLogger IO where
24 monadLoggerLog _ _ _ _ = return ()
25
26
27instance NFData PortNumber where
28 rnf = rnf . (fromIntegral :: PortNumber -> Int)
29
30instance NFData BlockIx where
31 rnf (BlockIx a b c) = a `deepseq` b `deepseq` rnf c
32
33instance NFData Block where
34 rnf (Block a b c) = a `deepseq` b `deepseq` rnf c
35
36instance NFData Bitfield
37
38instance NFData Message where
39 rnf (Have i) = rnf i
40 rnf (Bitfield b) = rnf b
41 rnf (Request b) = rnf b
42 rnf (Piece b) = rnf b
43 rnf (Cancel b) = rnf b
44 rnf (Port i) = rnf i
45 rnf _ = () -- other fields are forced by pattern matching
46
47{-
48encodeMessages :: [Message] -> ByteString
49encodeMessages xs = runPut (mapM_ put xs)
50
51decodeMessages :: ByteString -> Either String [Message]
52decodeMessages = runGet (many get)
53-}
54
55echo :: Handler IO
56echo = handler $ \ _ bs -> return (bs :: ByteString)
57
58addr :: SockAddr
59addr = SockAddrInet 6000 (256 * 256 * 256 + 127)
60
61-- main :: IO ()
62-- main = defaultMain []
63main :: IO ()
64main = withManager def addr [echo] $ \ m -> (`runReaderT` m) $ do
65 listen
66 liftIO $ defaultMain (benchmarks m)
67 where
68 sizes = [10, 100, 1000, 10000, 16 * 1024]
69 repetitions = [1, 10, 100, 1000]
70 benchmarks m = [mkbench m r s | r <- repetitions, s <- sizes]
71 where
72 mkbench action r n =
73 bench (show r ++ "times" ++ "/" ++ show n ++ "bytes") $ nfIO $
74 replicateM r $
75 runReaderT (query addr (BS.replicate n 0)) action