summaryrefslogtreecommitdiff
path: root/dht/bittorrent/bench
diff options
context:
space:
mode:
authorJames Crayne <jim.crayne@gmail.com>2019-09-28 13:43:29 -0400
committerJoe Crayne <joe@jerkface.net>2020-01-01 19:27:53 -0500
commit11987749fc6e6d3e53ea737d46d5ab13a16faeb8 (patch)
tree5716463275c2d3e902889db619908ded2a73971c /dht/bittorrent/bench
parentadd2c76bced51fde5e9917e7449ef52be70faf87 (diff)
Factor out some new libraries
word64-map: Data.Word64Map network-addr: Network.Address tox-crypto: Crypto.Tox lifted-concurrent: Control.Concurrent.Lifted.Instrument Control.Concurrent.Async.Lifted.Instrument psq-wrap: Data.Wrapper.PSQInt Data.Wrapper.PSQ minmax-psq: Data.MinMaxPSQ tasks: Control.Concurrent.Tasks kad: Network.Kademlia Network.Kademlia.Bootstrap Network.Kademlia.Routing Network.Kademlia.CommonAPI Network.Kademlia.Persistence Network.Kademlia.Search
Diffstat (limited to 'dht/bittorrent/bench')
-rw-r--r--dht/bittorrent/bench/Main.hs75
-rw-r--r--dht/bittorrent/bench/Throughtput.hs46
-rw-r--r--dht/bittorrent/bench/TorrentFile.hs27
3 files changed, 148 insertions, 0 deletions
diff --git a/dht/bittorrent/bench/Main.hs b/dht/bittorrent/bench/Main.hs
new file mode 100644
index 00000000..f04485ab
--- /dev/null
+++ b/dht/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
diff --git a/dht/bittorrent/bench/Throughtput.hs b/dht/bittorrent/bench/Throughtput.hs
new file mode 100644
index 00000000..d0404405
--- /dev/null
+++ b/dht/bittorrent/bench/Throughtput.hs
@@ -0,0 +1,46 @@
1{-# LANGUAGE ViewPatterns #-}
2{-# LANGUAGE PatternGuards #-}
3module Main (main) where
4
5import Control.Concurrent
6import Data.Bitfield
7import Network.BitTorrent
8import System.Environment
9import Control.Monad.Reader
10import Data.IORef
11
12
13main :: IO ()
14main = do
15 [path] <- getArgs
16 torrent <- fromFile path
17
18 print (contentLayout "./" (tInfo torrent))
19
20 client <- newClient 100 []
21 swarm <- newLeecher client torrent
22
23 ref <- liftIO $ newIORef 0
24 discover swarm $ do
25 forever $ do
26 e <- awaitEvent
27 case e of
28 Available bf
29 | Just m <- findMin bf -> yieldEvent (Want (BlockIx m 0 10))
30 | otherwise -> return ()
31 Want bix -> liftIO $ print bix
32 Fragment blk -> do
33
34 sc <- liftIO $ getSessionCount swarm
35 addr <- asks connectedPeerAddr
36
37 liftIO $ do
38 x <- atomicModifyIORef ref (\x -> (succ x, x))
39 if x `mod` 100 == 0
40 then print (x, sc, addr)
41 else return ()
42
43 yieldEvent (Want (BlockIx 0 0 (16 * 1024)))
44
45
46 print "Bye-bye! =_=" \ No newline at end of file
diff --git a/dht/bittorrent/bench/TorrentFile.hs b/dht/bittorrent/bench/TorrentFile.hs
new file mode 100644
index 00000000..e91a9c10
--- /dev/null
+++ b/dht/bittorrent/bench/TorrentFile.hs
@@ -0,0 +1,27 @@
1{-# LANGUAGE BangPatterns #-}
2module Main (main) where
3
4import Data.BEncode
5import Data.ByteString as BS
6import Data.Torrent
7import Criterion.Main
8
9
10tinyPath :: FilePath
11tinyPath = "res/dapper-dvd-amd64.iso.torrent"
12
13largePath :: FilePath
14largePath = "res/pkg.torrent"
15
16decoder :: ByteString -> Torrent
17decoder bs = let Right r = decode bs in r
18
19main :: IO ()
20main = do
21 !tinyBin <- BS.readFile tinyPath
22 !largeBin <- BS.readFile largePath
23
24 defaultMain
25 [ bench "read/tiny" $ nf decoder tinyBin
26 , bench "read/large" $ nf decoder largeBin
27 ] \ No newline at end of file