[Haskell-cafe] mine sockets lib vs yesod

Branimir Maksimovic branimir.maksimovic at gmail.com
Sat Sep 25 07:22:06 UTC 2021


Let’s compare:
1000 parallel connections 100k requests

yesod :
7658 recs/sec
Errors 85808
Timeouted 0

Mine::
12699 recs/sec
Errors 0
Timeouted 0

10k connections, 100k requests:
yesod:
5205 recs/sec
Errors 81152
Timeouted 3842

Mine:
8160 recs/sec
Errors 0
Timeouted 46

Sources:
Yesod:
/ HomeR GET
|]

instance Yesod HelloWorld

getHomeR :: Handler ()
getHomeR = do
              number <- liftIO (randomIO :: IO Float)
              let txt = pack $ "hello world " ++ show number
              sendResponse (txt:: Text)

main :: IO ()
main = warp 3838 HelloWorld

Mine:
import Sockets
import Foreign.C.String
import Data.IORef
import Control.Concurrent

main = do
    ref <- newIORef 1
    s <- socket defaultCallbacks {
                                    constructor = \ci -> set_cb ci defaultCallbacks {
                                        done_connected = binded ref,
                                        done_reading = process
                                        }
                                 }
    pl <- epoll 1000
    pl1 <- epoll 1000
    pl2 <- epoll 1000
    pl3 <- epoll 1000
    listen s "8080"
    pl_accept pl s
    pl_accept pl1 s
    pl_accept pl2 s
    pl_accept pl3 s
    let run pl = do forkIO $
                     run_loop pl (-1)
    run pl
    run pl1
    run pl2
    run_loop pl3 (-1)

binded ref pl s = do
    i <- readIORef ref
    putStrLn $ "connection nr:"++show i
    writeIORef ref (i+1)
    pl_read pl s
    return 0

process pl s buf len = do
        str <- peekCStringLen (buf,fromIntegral len)
        ip <- client s
        str1 <- peekCString ip
        putStrLn $ "from "++str1++" \ngot "++str
        write pl s "Hello World!!!\r\n"
        return 0

if using -threaded performance is *worse*.

Greetings, Branimir.


More information about the Haskell-Cafe mailing list