diff options
-rw-r--r-- | bench/Main.hs | 35 | ||||
-rw-r--r-- | src/Network/BitTorrent/PeerWire/Message.hs | 3 | ||||
-rw-r--r-- | src/Network/BitTorrent/PeerWire/Selection.hs | 6 |
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 | |||
5 | import Control.DeepSeq | 5 | import Control.DeepSeq |
6 | import Criterion.Main | 6 | import Criterion.Main |
7 | import Data.ByteString (ByteString) | 7 | import Data.ByteString (ByteString) |
8 | import qualified Data.ByteString as B | ||
8 | import Data.Serialize | 9 | import Data.Serialize |
9 | import Network.BitTorrent as BT | 10 | import Network.BitTorrent as BT |
10 | 11 | ||
@@ -51,22 +52,38 @@ bitfieldUnion n = BT.empty n `union` BT.empty n | |||
51 | selectionStrictFirst :: Int -> Maybe Int | 52 | selectionStrictFirst :: Int -> Maybe Int |
52 | selectionStrictFirst n = strictFirst (BT.empty n) (BT.empty n) [] | 53 | selectionStrictFirst n = strictFirst (BT.empty n) (BT.empty n) [] |
53 | 54 | ||
55 | selectionStrictLast :: Int -> Maybe Int | ||
56 | selectionStrictLast n = strictLast (BT.empty n) (BT.empty n) [] | ||
57 | |||
54 | main :: IO () | 58 | main :: IO () |
55 | main = do | 59 | main = 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 | |||
12 | import Network.BitTorrent.PeerWire.Block | 12 | import Network.BitTorrent.PeerWire.Block |
13 | import Network.BitTorrent.PeerWire.Bitfield | 13 | import Network.BitTorrent.PeerWire.Bitfield |
14 | 14 | ||
15 | import 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 | |||
78 | instance Serialize Message where | 79 | instance 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 | -- |
24 | module Network.BitTorrent.PeerWire.Selection | 24 | module 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 | ||
29 | import Network.BitTorrent.PeerWire.Block | 30 | import Network.BitTorrent.PeerWire.Block |
@@ -41,6 +42,9 @@ type Selector = Bitfield -- ^ Indices of client "have" pieces. | |||
41 | strictFirst :: Selector | 42 | strictFirst :: Selector |
42 | strictFirst h a _ = findMin (difference a h) | 43 | strictFirst h a _ = findMin (difference a h) |
43 | 44 | ||
45 | -- | Select the last available piece. | ||
46 | strictLast :: Selector | ||
47 | strictLast h a _ = findMax (difference a h) | ||
44 | 48 | ||
45 | -- | | 49 | -- | |
46 | rarestFirst :: Selector | 50 | rarestFirst :: Selector |