gameServer/stresstest.hs
changeset 4588 5ef5415c4ee1
parent 4568 f85243bf890e
child 4905 7842d085acf4
equal deleted inserted replaced
4529:467ab0685890 4588:5ef5415c4ee1
     4 
     4 
     5 import IO
     5 import IO
     6 import System.IO
     6 import System.IO
     7 import Control.Concurrent
     7 import Control.Concurrent
     8 import Network
     8 import Network
     9 import Control.OldException
     9 import Control.Exception
    10 import Control.Monad
    10 import Control.Monad
    11 import System.Random
    11 import System.Random
    12 
    12 
    13 #if !defined(mingw32_HOST_OS)
    13 #if !defined(mingw32_HOST_OS)
    14 import System.Posix
    14 import System.Posix
    15 #endif
    15 #endif
    16 
    16 
    17 session1 nick room = ["NICK", nick, "", "PROTO", "32", "", "PING", "", "CHAT", "lobby 1", "", "CREATE_ROOM", room, "", "CHAT", "room 1", "", "QUIT", "creator", ""]
    17 session1 nick room = ["NICK", nick, "", "PROTO", "24", "", "CHAT", "lobby 1", "", "CREATE", room, "", "CHAT", "room 1", "", "QUIT", "bye-bye", ""]
    18 session2 nick room = ["NICK", nick, "", "PROTO", "32", "", "LIST", "", "JOIN_ROOM", room, "", "CHAT", "room 2", "", "PART", "", "CHAT", "lobby after part", "", "QUIT", "part-quit", ""]
    18 session2 nick room = ["NICK", nick, "", "PROTO", "24", "", "LIST", "", "JOIN", room, "", "CHAT", "room 2", "", "PART", "", "CHAT", "lobby after part", "", "QUIT", "bye-bye", ""]
    19 session3 nick room = ["NICK", nick, "", "PROTO", "32", "", "LIST", "", "JOIN_ROON", room, "", "CHAT", "room 2", "", "QUIT", "quit", ""]
    19 session3 nick room = ["NICK", nick, "", "PROTO", "24", "", "LIST", "", "JOIN", room, "", "CHAT", "room 2", "", "QUIT", "bye-bye", ""]
    20 
    20 
    21 emulateSession sock s = do
    21 emulateSession sock s = do
    22     mapM_ (\x -> hPutStrLn sock x >> hFlush sock >> randomRIO (30000::Int, 59000) >>= threadDelay) s
    22     mapM_ (\x -> hPutStrLn sock x >> hFlush sock >> randomRIO (50000::Int, 90000) >>= threadDelay) s
    23     hFlush sock
    23     hFlush sock
    24     threadDelay 225000
    24     threadDelay 225000
    25 
    25 
    26 testing = Control.OldException.handle print $ do
    26 testing = Control.Exception.handle print $ do
    27     putStrLn "Start"
    27     putStrLn "Start"
    28     sock <- connectTo "127.0.0.1" (PortNumber 46631)
    28     sock <- connectTo "127.0.0.1" (PortNumber 46631)
    29 
    29 
    30     num1 <- randomRIO (70000::Int, 70100)
    30     num1 <- randomRIO (70000::Int, 70100)
    31     num2 <- randomRIO (0::Int, 2)
    31     num2 <- randomRIO (0::Int, 2)
    32     num3 <- randomRIO (0::Int, 5)
    32     num3 <- randomRIO (0::Int, 5)
    33     let nick1 = 'n' : show num1
    33     let nick1 = show num1
    34     let room1 = 'r' : show num2
    34     let room1 = show num2
    35     case num2 of 
    35     case num2 of 
    36         0 -> emulateSession sock $ session1 nick1 room1
    36         0 -> emulateSession sock $ session1 nick1 room1
    37         1 -> emulateSession sock $ session2 nick1 room1
    37         1 -> emulateSession sock $ session2 nick1 room1
    38         2 -> emulateSession sock $ session3 nick1 room1
    38         2 -> emulateSession sock $ session3 nick1 room1
    39     hClose sock
    39     hClose sock
    40     putStrLn "Finish"
    40     putStrLn "Finish"
    41 
    41 
    42 forks = forever $ do
    42 forks = forever $ do
    43     delay <- randomRIO (30000::Int, 59000)
    43     delay <- randomRIO (10000::Int, 19000)
    44     threadDelay delay
    44     threadDelay delay
    45     forkIO testing
    45     forkIO testing
    46 
    46 
    47 main = withSocketsDo $ do
    47 main = withSocketsDo $ do
    48 #if !defined(mingw32_HOST_OS)
    48 #if !defined(mingw32_HOST_OS)