Browse Source

Merge pull request #553 from weltermann17/master

Plain: fixed db update to be compliant with the requirements
Brian Hauer 11 years ago
parent
commit
bf4f829a31
4 changed files with 86 additions and 18 deletions
  1. 1 1
      plain/sbt
  2. 55 0
      plain/sbt.bat
  3. 12 5
      plain/setup.py
  4. 18 12
      plain/src/main/scala/com/ibm/techempower/Db.scala

+ 1 - 1
plain/sbt

@@ -1 +1 @@
-java -Xmx3g -Xms2g -Xmn1g -Xss8M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=384M -Dsbt.global.base=project/.sbtboot -Dsbt.boot.directory=project/.boot -Dsbt.ivy.home=project/.ivy -jar `dirname $0`/sbt-launch.jar "$@"
+java -server -Xnoclassgc -XX:MaxPermSize=1g -XX:ReservedCodeCacheSize=384m -Xmx8g -Xss8m -Xmn4g -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=384M -Dsbt.global.base=project/.sbtboot -Dsbt.boot.directory=project/.boot -Dsbt.ivy.home=project/.ivy -jar `dirname $0`/sbt-launch.jar "$@"

+ 55 - 0
plain/sbt.bat

@@ -0,0 +1,55 @@
+@REM SBT launcher script
+@REM 
+@REM Envioronment:
+@REM JAVA_HOME - location of a JDK home dir (mandatory)
+@REM SBT_OPTS  - JVM options (optional)
+@REM Configuration:
+@REM sbtconfig.txt found in the SBT_HOME.
+
+@REM   ZOMG! We need delayed expansion to build up CFG_OPTS later 
+@setlocal enabledelayedexpansion
+
+@echo off
+set SBT_HOME=%~dp0
+set SBT_OPTS=-Xmx3g -Xms2g -Xmn1g -Xss8M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=384M
+set ERROR_CODE=0
+
+rem FIRST we load the config file of extra options.
+set FN=%SBT_HOME%\..\conf\sbtconfig.txt
+set CFG_OPTS=
+FOR /F "tokens=* eol=# usebackq delims=" %%i IN ("%FN%") DO (
+  set DO_NOT_REUSE_ME=%%i
+  rem ZOMG (Part #2) WE use !! here to delay the expansion of
+  rem CFG_OPTS, otherwise it remains "" for this loop.
+  set CFG_OPTS=!CFG_OPTS! !DO_NOT_REUSE_ME!
+)
+
+rem We use the value of the JAVACMD environment variable if defined
+set _JAVACMD=%JAVACMD%
+
+if "%_JAVACMD%"=="" (
+  if not "%JAVA_HOME%"=="" (
+    if exist "%JAVA_HOME%\bin\java.exe" set "_JAVACMD=%JAVA_HOME%\bin\java.exe"
+  )
+)
+
+if "%_JAVACMD%"=="" set _JAVACMD=java
+
+rem We use the value of the JAVA_OPTS environment variable if defined, rather than the config.
+set _JAVA_OPTS=%JAVA_OPTS%
+if "%_JAVA_OPTS%"=="" set _JAVA_OPTS=%CFG_OPTS%
+
+:run
+
+"%_JAVACMD%" %_JAVA_OPTS% %SBT_OPTS% -cp "%SBT_HOME%sbt-launch.jar" xsbt.boot.Boot %*
+if ERRORLEVEL 1 goto error
+goto end
+
+:error
+set ERROR_CODE=1
+
+:end
+
+@endlocal
+
+exit /B %ERROR_CODE%

+ 12 - 5
plain/setup.py

@@ -4,14 +4,21 @@ import sys
 import time
 import time
 import os
 import os
 
 
-def start(args=None):
-    setup_util.replace_text("plain/src/main/resources/application.conf", "127.0.0.1", args.database_host)
+def start(args):
+  setup_util.replace_text("plain/src/main/resources/application.conf", "127.0.0.1", args.database_host)
+  if os.name == 'nt':
+    subprocess.check_call("./sbt.bat assembly", shell=True, cwd="plain")
+  else:
     subprocess.check_call("./sbt assembly", shell=True, cwd="plain")
     subprocess.check_call("./sbt assembly", shell=True, cwd="plain")
-    subprocess.Popen("java -server -Xnoclassgc -XX:MaxPermSize=1g -XX:ReservedCodeCacheSize=384m -Xmx8g -Xss8m -Xmn4g -jar target/scala-2.10/plain-benchmark-assembly-1.0.1.jar", cwd="plain", shell=True)
-    time.sleep(10)
-    return 0
+      
+  subprocess.Popen("java -server -Xnoclassgc -XX:MaxPermSize=1g -XX:ReservedCodeCacheSize=384m -Xmx8g -Xss8m -Xmn4g -jar target/scala-2.10/plain-benchmark-assembly-1.0.1.jar", cwd="plain", shell=True)
+  time.sleep(10)
+  return 0
 
 
 def stop():
 def stop():
+  if os.name == 'nt':
+    subprocess.call("taskkill /f /im *plain-benchmark* > NUL", shell=True)
+    return 0
   p = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
   p = subprocess.Popen(['ps', 'aux'], stdout=subprocess.PIPE)
   out, err = p.communicate()
   out, err = p.communicate()
   for line in out.splitlines():
   for line in out.splitlines():

+ 18 - 12
plain/src/main/scala/com/ibm/techempower/Db.scala

@@ -4,6 +4,7 @@ import java.util.concurrent.ThreadLocalRandom.{ current => random }
 
 
 import scala.language.implicitConversions
 import scala.language.implicitConversions
 import scala.language.postfixOps
 import scala.language.postfixOps
+import scala.collection.mutable.MutableList
 
 
 import com.ibm.plain.rest.{ Form, Resource }
 import com.ibm.plain.rest.{ Form, Resource }
 import com.ibm.plain.json.{ Json => J }
 import com.ibm.plain.json.{ Json => J }
@@ -33,27 +34,28 @@ sealed abstract class DbResource
     extends Resource {
     extends Resource {
 
 
   @inline protected[this] final def get(form: Option[Form]): J = {
   @inline protected[this] final def get(form: Option[Form]): J = {
-    var list: List[J] = Nil
+    val output = new MutableList[J]
     val q = form match { case None => 1 case Some(f) => queries(f) }
     val q = form match { case None => 1 case Some(f) => queries(f) }
-    withConnection(datasource) {
-      implicit connection => for (i <- 1 to q) { for (j <- selectsql << next <<! asJson) { list = j :: list } }
+    withConnection(datasource) { implicit connection =>
+      for (i <- 1 to q) { for (j <- selectsql << next <<! asJson) { output += j } }
     }
     }
-    form match { case None => list.head case _ => J(list) }
+    form match { case None => output.head case _ => J(output.toList) }
   }
   }
 
 
   @inline protected[this] final def update(form: Form): J = {
   @inline protected[this] final def update(form: Form): J = {
-    var list: List[J] = Nil
+    val input = new MutableList[World]
+    val output = new MutableList[J]
     val q = queries(form)
     val q = queries(form)
-    withConnection(datasource) {
-      implicit connection =>
-        for (i <- 1 to q) {
-          val id = next
+    withConnection(datasource) { implicit connection =>
+      for (i <- 1 to q) { for (j <- selectsql << next <<! asTuple) { input += j } }
+      input.foreach {
+        case (id, _) =>
           val randomNumber = next
           val randomNumber = next
           updatesql << randomNumber << id <<!!;
           updatesql << randomNumber << id <<!!;
-          list = asJson(id, randomNumber) :: list
-        }
+          output += asJson(id, randomNumber)
+      }
     }
     }
-    J(list)
+    J(output.toList)
   }
   }
 
 
   @inline private[this] final def queries(form: Form) = try {
   @inline private[this] final def queries(form: Form) = try {
@@ -70,8 +72,12 @@ sealed abstract class DbResource
 
 
   @inline private[this] final def asJson(id: Int, randomNumber: Int) = J(Map("id" -> id, "randomNumber" -> randomNumber))
   @inline private[this] final def asJson(id: Int, randomNumber: Int) = J(Map("id" -> id, "randomNumber" -> randomNumber))
 
 
+  @inline private[this] final def asTuple = (r: RichResultSet) => (r.nextInt.get, r.nextInt.get)
+
   @inline private[this] final def next = random.nextInt(1, 10001)
   @inline private[this] final def next = random.nextInt(1, 10001)
 
 
+  private[this] final type World = (Int, Int)
+
   private[this] final val selectsql = "select id, randomNumber from World where id = ?"
   private[this] final val selectsql = "select id, randomNumber from World where id = ?"
 
 
   private[this] final val updatesql = "update World set randomNumber = ? where id = ?"
   private[this] final val updatesql = "update World set randomNumber = ? where id = ?"