summaryrefslogtreecommitdiff
path: root/testsuite/tests/lib/base/T16916.hs
blob: a5dafa515b8d996f6a3a2f0881d25a8b2f7d2567 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
module Main where

import Control.Concurrent
import Foreign.C
import GHC.Event
import System.CPUTime
import System.Posix.Types

foreign import ccall unsafe "socket" c_socket ::
               CInt -> CInt -> CInt -> IO CInt

makeTestSocketFd :: IO Fd
makeTestSocketFd = do
    sockNum <-
        c_socket
            1 -- PF_LOCAL
            2 -- SOCK_DGRAM
            0
    return $ (fromIntegral sockNum :: Fd)

callback :: FdKey -> Event -> IO ()
callback _ _ = return ()

idleCpuUsage :: IO Integer
idleCpuUsage = do
  startCPUTime <- getCPUTime
  threadDelay 500000
  endCPUTime <- getCPUTime
  return $ endCPUTime - startCPUTime

main :: IO ()
main = do
  (Just eventMgr) <- getSystemEventManager
  fd <- makeTestSocketFd

  noEventUsage <- idleCpuUsage

  registerFd eventMgr callback fd evtRead OneShot
  registerFd eventMgr callback fd evtWrite OneShot

  eventTriggeredUsage <- idleCpuUsage

  -- CPU consumption should roughly be the same when just idling vs
  -- when idling after the event been triggered
  print $ (fromIntegral eventTriggeredUsage / fromIntegral noEventUsage) < 2.0