From 77972fefc319464b2a2297c3c19962e4a1c029b1 Mon Sep 17 00:00:00 2001 From: Havoc Pennington Date: Fri, 10 Oct 2014 17:22:53 -0400 Subject: [PATCH] Add profiling of getting values from a Config To run these, do > project config > test:runMain GetExistingPath > test:runMain HasPathOnMissing > test:runMain CatchExceptionOnMissing --- config/src/test/scala/Profiling.scala | 54 +++++++++++++++++++++++++-- 1 file changed, 51 insertions(+), 3 deletions(-) diff --git a/config/src/test/scala/Profiling.scala b/config/src/test/scala/Profiling.scala index 289aa577..7894a0d2 100644 --- a/config/src/test/scala/Profiling.scala +++ b/config/src/test/scala/Profiling.scala @@ -3,10 +3,12 @@ */ import com.typesafe.config.Config import com.typesafe.config.ConfigFactory +import com.typesafe.config.ConfigException + object Util { - def time(body: () => Unit, iterations: Int): Long = { + def time(body: () => Unit, iterations: Int): Double = { // warm up - for (i <- 1 to 20) { + for (i <- 1 to Math.max(20, iterations / 10)) { body() } @@ -15,7 +17,7 @@ object Util { body() } val end = System.currentTimeMillis() - (end - start) / iterations + (end - start).toDouble / iterations } def loop(args: Seq[String], body: () => Unit) { @@ -57,3 +59,49 @@ object Resolve extends App { Util.loop(args, task) } + +object GetExistingPath extends App { + val conf = ConfigFactory.parseString("aaaaa.bbbbb.ccccc.d=42").resolve() + + def task() { + if (conf.getInt("aaaaa.bbbbb.ccccc.d") != 42) { + throw new Exception("broken get") + } + } + + val ms = Util.time(task, 100000) + println("GetExistingPath: " + ms + "ms") + + Util.loop(args, task) +} + +object HasPathOnMissing extends App { + val conf = ConfigFactory.parseString("aaaaa.bbbbb.ccccc.d=42,x=10, y=11, z=12").resolve() + + def task() { + if (conf.hasPath("aaaaa.bbbbb.ccccc.e")) { + throw new Exception("we shouldn't have this path") + } + } + + val ms = Util.time(task, 100000) + println("HasPathOnMissing: " + ms + "ms") + + Util.loop(args, task) +} + +object CatchExceptionOnMissing extends App { + val conf = ConfigFactory.parseString("aaaaa.bbbbb.ccccc.d=42,x=10, y=11, z=12").resolve() + + def task() { + try conf.getInt("aaaaa.bbbbb.ccccc.e") + catch { + case e: ConfigException.Missing => + } + } + + val ms = Util.time(task, 100000) + println("CatchExceptionOnMissing: " + ms + "ms") + + Util.loop(args, task) +}