Browse Source

WAI implementation for json

Greg Weber 12 years ago
parent
commit
cf115b520b
6 changed files with 90 additions and 0 deletions
  1. 2 0
      wai/.gitignore
  2. 0 0
      wai/__init__.py
  3. 21 0
      wai/bench/bench.cabal
  4. 27 0
      wai/bench/wai.hs
  5. 11 0
      wai/benchmark_config
  6. 29 0
      wai/setup.py

+ 2 - 0
wai/.gitignore

@@ -0,0 +1,2 @@
+bench/cabal-dev
+bench/dist

+ 0 - 0
wai/__init__.py


+ 21 - 0
wai/bench/bench.cabal

@@ -0,0 +1,21 @@
+name:              bench-wai
+version:           0.0.0
+cabal-version:     >= 1.8
+build-type:        Simple
+
+executable         bench
+    main-is:           wai.hs
+    hs-source-dirs:    .
+
+    ghc-options:       -Wall -threaded -O2 -rtsopts
+
+    extensions: OverloadedStrings
+
+    build-depends: base                          >= 4          && < 5
+                 , warp                          >= 1.3        && < 1.4
+                 , wai                           >= 1.4
+                 , text                          >= 0.11       && < 0.12
+                 , aeson                         >= 0.6.1.0
+                 , unix                          >= 2.5
+                 , network-conduit               >= 1.0
+                 , http-types

+ 27 - 0
wai/bench/wai.hs

@@ -0,0 +1,27 @@
+{-# LANGUAGE OverloadedStrings #-}
+import Data.Aeson
+import Data.Text (Text)
+
+import Control.Monad (replicateM_)
+import Network.HTTP.Types (status200)
+import qualified Network.Wai.Handler.Warp as Warp
+import System.Posix.Process (forkProcess)
+import Data.Conduit.Network (bindPort)
+import Network.Wai
+import System.Environment (getArgs)
+
+main :: IO ()
+main = do
+    socket <- bindPort 8001 "*"
+    [cores, _] <- getArgs
+    let run = Warp.runSettingsSocket Warp.defaultSettings
+                { Warp.settingsPort = 8001
+                , Warp.settingsHost = "*"
+                , Warp.settingsOnException = const $ return ()
+                } socket app
+    replicateM_ (read cores - 1) $ forkProcess run
+    run
+  where
+    app _ = return $ responseLBS
+      status200 [("Content-Type", "application/json")] $
+      encode $ object ["message" .= ("Hello, World!" :: Text)]

+ 11 - 0
wai/benchmark_config

@@ -0,0 +1,11 @@
+{
+  "framework": "wai",
+  "tests": [{
+    "default": {
+      "setup_file": "setup",
+      "json_url": "/json",
+      "port": 8001,
+      "sort": 37
+    }
+  }]
+}

+ 29 - 0
wai/setup.py

@@ -0,0 +1,29 @@
+
+import subprocess
+import sys
+import setup_util
+import os
+
+def start(args):
+  subprocess.check_call("cabal update", shell=True, cwd="wai/bench")
+  subprocess.check_call("cabal install --only-dependencies", shell=True, cwd="wai/bench")
+  subprocess.check_call("cabal configure", shell=True, cwd="wai/bench")
+  subprocess.check_call("cabal build", shell=True, cwd="wai/bench")
+
+  db_host = args.database_host
+  threads = str(args.max_threads)
+  subprocess.Popen("dist/build/bench/bench " + threads + " " + db_host + " +RTS -A4M -N -qg2 -I0 -G2 > /dev/null", shell=True, cwd="wai/bench")
+  return 0
+
+def stop():
+  p = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
+  out, err = p.communicate()
+  for line in out.splitlines():
+    if 'bench' in line:
+      try:
+        pid = int(line.split(None, 2)[1])
+        os.kill(pid, 9)
+      except OSError:
+        pass
+
+  return 0