Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add benchmark for roundtrip connections #235

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading