Skip to content

Commit

Permalink
Add benchmark for roundtrip connections
Browse files Browse the repository at this point in the history
  • Loading branch information
domenkozar committed Dec 21, 2023
1 parent 15bb687 commit 79e7fa8
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,9 @@ jobs:
- name: Build documentation
run: cabal haddock all

- name: Benchmark
run: cabal bench all

- name: Install virtualenv
if: matrix.os == 'ubuntu-latest'
run: |
Expand Down
44 changes: 44 additions & 0 deletions benchmarks/connections.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{-# LANGUAGE OverloadedStrings #-}
module Main where

import Control.Monad (forever, unless)
import Data.ByteString (ByteString)
import qualified Network.WebSockets as WS
import qualified Network.WebSockets.Client as WC
import Criterion.Main
import Control.Concurrent.Async (mapConcurrently_, race_, async, wait, Async)


server :: WS.ServerApp
server pending = do
conn <- WS.acceptRequest pending
msg <- WS.receiveData conn
WS.sendBinaryData conn (msg :: ByteString)

client :: WC.ClientApp ()
client conn = do
-- Send and receive a message back
let msg = "Hello, world!" :: ByteString
WS.sendBinaryData conn msg
msg' <- WS.receiveData conn
unless (msg == msg') $ error "Message mismatch"

WS.sendClose conn ("Bye!" :: ByteString)

run :: Int -> Async () -> IO ()
run n server = race_ runServer runClient
where
runClient = mapConcurrently_ (\_ -> WC.runClient "127.0.0.1" 8089 "/" client) [1..n]
runServer = wait server

main :: IO ()
main = do
server <- async $ WS.runServer "127.0.0.1" 8089 server
defaultMain [
bgroup "connections"
[ bench "100" $ nfIO $ run 100 server
, bench "1000" $ nfIO $ run 1000 server
, bench "10000" $ nfIO $ run 10000 server
, bench "100000" $ nfIO $ run 100000 server
]
]
14 changes: 14 additions & 0 deletions websockets.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,20 @@ Benchmark bench-mask
text >= 0.10 && < 2.2,
entropy >= 0.2.1 && < 0.5

Benchmark bench-connections
type: exitcode-stdio-1.0
main-is: connections.hs
Hs-source-dirs: benchmarks
ghc-options: -threaded -O2 -rtsopts "-with-rtsopts=-N"
Default-language: Haskell2010
Build-depends:
async,
base,
bytestring,
criterion,
async,
websockets

Executable websockets-website
If !flag(Website)
Buildable: False
Expand Down

0 comments on commit 79e7fa8

Please sign in to comment.