summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--bench/Main.hs35
-rw-r--r--src/Network/BitTorrent/PeerWire/Message.hs3
-rw-r--r--src/Network/BitTorrent/PeerWire/Selection.hs6
3 files changed, 33 insertions, 11 deletions
diff --git a/bench/Main.hs b/bench/Main.hs
index e4f756a3..70eb4822 100644
--- a/bench/Main.hs
+++ b/bench/Main.hs
@@ -5,6 +5,7 @@ import Control.Applicative
5import Control.DeepSeq 5import Control.DeepSeq
6import Criterion.Main 6import Criterion.Main
7import Data.ByteString (ByteString) 7import Data.ByteString (ByteString)
8import qualified Data.ByteString as B
8import Data.Serialize 9import Data.Serialize
9import Network.BitTorrent as BT 10import Network.BitTorrent as BT
10 11
@@ -51,22 +52,38 @@ bitfieldUnion n = BT.empty n `union` BT.empty n
51selectionStrictFirst :: Int -> Maybe Int 52selectionStrictFirst :: Int -> Maybe Int
52selectionStrictFirst n = strictFirst (BT.empty n) (BT.empty n) [] 53selectionStrictFirst n = strictFirst (BT.empty n) (BT.empty n) []
53 54
55selectionStrictLast :: Int -> Maybe Int
56selectionStrictLast n = strictLast (BT.empty n) (BT.empty n) []
57
54main :: IO () 58main :: IO ()
55main = do 59main = do
56 let datas = replicate 10000 (Request (BlockIx 0 0 0)) 60 let blockixs = replicate 5000 (Request (BlockIx 0 0 0))
57 let m = 1024 * 1024 61 let bitfields = replicate 5000 (Bitfield (MkBitfield (B.replicate 1000 0)))
62 let chokes = replicate 5000 Choke
63 let havenones = replicate 5000 HaveNone
58 64
59 defaultMain 65 let m = 1024 * 1024
60 [ datas `deepseq` bench "message/encode" $ nf encodeMessages datas
61 , let binary = encodeMessages datas in
62 binary `deepseq` bench "message/decode" $ nf decodeMessages binary
63 66
64 -- ~ 256KiB * 10M = 2.5TiB 67 defaultMain $
65 , bench "bitfield/min" $ nf bitfieldMin (10 * m) 68 concatMap (uncurry mkMsgBench)
69 [ ("blockIx", blockixs)
70 , ("bitfield", bitfields)
71 , ("choke", chokes)
72 , ("havenone", havenones)
73 ]
74 ++ -- 256KiB * 10M = 2.5TiB
75 [ bench "bitfield/min" $ nf bitfieldMin (10 * m)
66 , bench "bitfield/max" $ nf bitfieldMax (10 * m) 76 , bench "bitfield/max" $ nf bitfieldMax (10 * m)
67 , bench "bitfield/difference" $ nf bitfieldDiff (10 * m) 77 , bench "bitfield/difference" $ nf bitfieldDiff (10 * m)
68 , bench "bitfield/intersection" $ nf bitfieldInter (10 * m) 78 , bench "bitfield/intersection" $ nf bitfieldInter (10 * m)
69 , bench "bitfield/union" $ nf bitfieldUnion (10 * m) 79 , bench "bitfield/union" $ nf bitfieldUnion (10 * m)
70 80
71 , bench "selection/strictFirst" $ nf selectionStrictFirst (10 * m) 81 , bench "selection/strictFirst" $ nf selectionStrictFirst (10 * m)
72 ] \ No newline at end of file 82 , bench "selection/strictFirst" $ nf selectionStrictLast (10 * m)
83 ]
84 where
85 mkMsgBench name msgs =
86 [ msgs `deepseq` bench ("message/" ++ name ++ "/encode") $ nf encodeMessages msgs
87 , let binary = encodeMessages msgs in
88 binary `deepseq` bench ("message/" ++ name ++ "/decode") $ nf decodeMessages binary
89 ]
diff --git a/src/Network/BitTorrent/PeerWire/Message.hs b/src/Network/BitTorrent/PeerWire/Message.hs
index 188f0572..cc771966 100644
--- a/src/Network/BitTorrent/PeerWire/Message.hs
+++ b/src/Network/BitTorrent/PeerWire/Message.hs
@@ -12,6 +12,7 @@ import Data.Serialize
12import Network.BitTorrent.PeerWire.Block 12import Network.BitTorrent.PeerWire.Block
13import Network.BitTorrent.PeerWire.Bitfield 13import Network.BitTorrent.PeerWire.Bitfield
14 14
15import Data.Array
15 16
16-- | Messages used in communication between peers. 17-- | Messages used in communication between peers.
17-- 18--
@@ -78,7 +79,7 @@ data Message = KeepAlive
78instance Serialize Message where 79instance Serialize Message where
79 get = do 80 get = do
80 len <- getInt 81 len <- getInt
81 _ <- lookAhead $ ensure len 82-- _ <- lookAhead $ ensure len
82 if len == 0 then return KeepAlive 83 if len == 0 then return KeepAlive
83 else do 84 else do
84 mid <- getWord8 85 mid <- getWord8
diff --git a/src/Network/BitTorrent/PeerWire/Selection.hs b/src/Network/BitTorrent/PeerWire/Selection.hs
index 04049812..2e412e06 100644
--- a/src/Network/BitTorrent/PeerWire/Selection.hs
+++ b/src/Network/BitTorrent/PeerWire/Selection.hs
@@ -23,7 +23,8 @@
23-- 23--
24module Network.BitTorrent.PeerWire.Selection 24module Network.BitTorrent.PeerWire.Selection
25 ( Selector 25 ( Selector
26 , strictFirst, rarestFirst, randomFirst, endGame, autoSelector 26 , strictFirst, strictLast
27 , rarestFirst, randomFirst, endGame, autoSelector
27 ) where 28 ) where
28 29
29import Network.BitTorrent.PeerWire.Block 30import Network.BitTorrent.PeerWire.Block
@@ -41,6 +42,9 @@ type Selector = Bitfield -- ^ Indices of client "have" pieces.
41strictFirst :: Selector 42strictFirst :: Selector
42strictFirst h a _ = findMin (difference a h) 43strictFirst h a _ = findMin (difference a h)
43 44
45-- | Select the last available piece.
46strictLast :: Selector
47strictLast h a _ = findMax (difference a h)
44 48
45-- | 49-- |
46rarestFirst :: Selector 50rarestFirst :: Selector