Benchmark.scala 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package code.lib
  2. import net.liftweb._
  3. import net.liftweb.http._
  4. import net.liftweb.util.Helpers._
  5. import net.liftweb.common._
  6. import js._
  7. import json._
  8. import JsCmds._
  9. import code.db._
  10. import java.util.concurrent.ThreadLocalRandom
  11. import scala.slick.driver.MySQLDriver.simple._
  12. import Database.threadLocalSession
  13. object JsonBenchmark {
  14. def init() =
  15. LiftRules.statelessDispatch.append{
  16. case r @ Req("json" :: Nil, _, _) => () => sayHello()
  17. }
  18. def sayHello() = Full(JsonResponse(
  19. JObject(List(JField("message", JString("Hello World!"))))
  20. ))
  21. }
  22. object DbBenchmark {
  23. private val DB_ROWS = 10000
  24. def init() =
  25. LiftRules.statelessDispatch.append{
  26. case r @ Req("db" :: Nil, _, _) => () => singleQuery()
  27. case r @ Req("db" :: queries :: Nil, _ , _) => () => customQuery(queries.toInt)
  28. }
  29. def customQuery(count: Int) : Box[LiftResponse] = {
  30. val tlc = ThreadLocalRandom.current()
  31. val randoms = for(i <- (1 to count)) yield tlc.nextLong(DB_ROWS)
  32. val result = DB.exec {
  33. (for {
  34. w <- WorldTable
  35. if w.id inSetBind randoms
  36. } yield w).list
  37. }
  38. Full(JsonResponse(JArray(result.map(_.toJson))))
  39. }
  40. def singleQuery() : Box[LiftResponse] = {
  41. val tlc = ThreadLocalRandom.current()
  42. val random = tlc.nextLong(DB_ROWS)
  43. val result = DB.exec {
  44. (for {
  45. w <- WorldTable if w.id === random
  46. } yield w).list
  47. }
  48. Full(JsonResponse(result(0).toJson))
  49. }
  50. }