Permalink
Browse files

first version

  • Loading branch information...
1 parent bb1c8d5 commit 76095ccc2737bb50747067a7b57fa5775400b1a2 @fernandezpablo85 committed Apr 4, 2013
Showing with 109 additions and 0 deletions.
  1. +15 −0 build.sbt
  2. +13 −0 src/main/resources/logback.xml
  3. +29 −0 src/main/scala/DefaultExample.scala
  4. +52 −0 src/main/scala/util/Time.scala
View
15 build.sbt
@@ -0,0 +1,15 @@
+name := "slick-connection-pool-example"
+
+organization := "org.example"
+
+version := "1.0"
+
+scalaVersion := "2.10.0"
+
+libraryDependencies ++= List(
+ "com.typesafe.slick" %% "slick" % "1.0.0",
+ "org.slf4j" % "slf4j-api" % "1.6.4",
+ "ch.qos.logback" % "logback-classic" % "1.0.1",
+ "ch.qos.logback" % "logback-core" % "1.0.1",
+ "postgresql" % "postgresql" % "9.1-901.jdbc4"
+)
View
13 src/main/resources/logback.xml
@@ -0,0 +1,13 @@
+<configuration>
+ <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
+ <encoder>
+ <!-- <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern> -->
+ <pattern>SQL: %msg %n</pattern>
+ </encoder>
+ </appender>
+
+ <logger name="scala.slick.session" level="DEBUG">
+ <appender-ref ref="STDOUT" />
+ </logger>
+
+</configuration>
View
29 src/main/scala/DefaultExample.scala
@@ -0,0 +1,29 @@
+package org.example
+
+import scala.slick.driver.PostgresDriver.simple._
+import Database.threadLocalSession
+
+object DefaultExample {
+
+ val database = Database.forURL("jdbc:postgresql://localhost:5432/demo", driver = "org.postgresql.Driver")
+
+ case class User(id: Option[Int], name: String, pass: String)
+ object User {
+ def dbCreate(name: String, pass: String) = User(None, name, pass)
+ def dbRetrieve(user: User) = Some((user.name, user.pass))
+ }
+
+ object Users extends Table[User]("users") {
+ def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
+ def name = column[String]("name")
+ def pass = column[String]("pass")
+
+ def * = name ~ pass <> (User.dbCreate _, User.dbRetrieve _)
+ }
+
+ def insert(name: String, pass: String) = {
+ database withSession {
+ Users.insert(User(None, name, pass))
+ }
+ }
+}
View
52 src/main/scala/util/Time.scala
@@ -0,0 +1,52 @@
+package org.example
+
+import java.util.concurrent.TimeUnit
+import java.util.concurrent.TimeUnit.{NANOSECONDS => Nanos, MILLISECONDS => Millis}
+import scala.math.ceil
+
+package object util {
+
+ private def measure[A](thunk: => A): (A, Long) = {
+ val before = System.nanoTime
+ val result = thunk
+ val delta = (System.nanoTime - before)
+
+ (result, delta)
+ }
+
+ private def stats(times: Seq[Long]) = {
+ val sortedTimes = times.sorted
+ val mean = ("mean", (times.sum / times.size))
+ val fifty = ("50%", sortedTimes(ceil(times.size * 0.5).toInt))
+ val seventyFive = ("75%", sortedTimes(ceil(times.size * 0.75).toInt))
+ val ninety = ("90%", sortedTimes(ceil(times.size * 0.90).toInt))
+ val ninetyNine = ("99%", sortedTimes(ceil(times.size * 0.99).toInt))
+
+ mean :: fifty :: seventyFive :: ninety :: ninetyNine :: Nil
+ }
+
+ def time[A](unit: TimeUnit = Millis)(thunk: => A): A = {
+ val (result, time) = measure(thunk)
+ println(s"snippet took: ${unit.convert(time, Nanos)} ${unit.toString.toLowerCase}")
+
+ result
+ }
+
+ def benchmark[A](unit: TimeUnit = Millis, times: Int = 100)(thunk: => A) = {
+
+ // warm up.
+ (1 to 100) foreach (i => thunk)
+
+ // measure.
+ val results = (1 to times) map (i => measure(thunk)) map (_._2)
+ val info = stats(results)
+
+ // inform.
+ println("RESULTS:")
+ println("--------")
+ println()
+ info foreach { case (label, value) =>
+ printf("%-4s: %-6s %s %n", label, unit.convert(value, Nanos), unit.toString.toLowerCase)
+ }
+ }
+}

0 comments on commit 76095cc

Please sign in to comment.