Skip to content

Commit

Permalink
Cleanups and fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
axel22 committed Aug 15, 2014
2 parents 5aafdb0 + 4e3c328 commit 06dd0ee
Show file tree
Hide file tree
Showing 24 changed files with 1,035 additions and 37 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,6 @@ tmp*
.classpath
.project
.settings

.gitattributes

.metadata/
1 change: 0 additions & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@

addSbtPlugin("com.typesafe.sbt" % "sbt-pgp" % "0.8.1")

6 changes: 4 additions & 2 deletions scalameter-core/src/main/scala/org/scalameter/Context.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.scalameter

import language.implicitConversions


import language.implicitConversions
import scala.collection.{Seq, immutable}
import org.scalameter.Key._



case class Context(properties: immutable.Map[Key[_], Any]) {
def +[T](t: (Key[T], T)) = Context(properties + t)
def ++(that: Context) = Context(this.properties ++ that.properties)
Expand All @@ -18,12 +20,12 @@ case class Context(properties: immutable.Map[Key[_], Any]) {
}
def goe[T](key: Key[T], v: T) = properties.getOrElse(key, v).asInstanceOf[T]
def apply[T](key: KeyWithDefault[T]) = properties.get(key).asInstanceOf[Option[T]].getOrElse(key.defaultValue)

def scope = scopeList.mkString(".")
def scopeList = apply(dsl.scope).reverse
def curve = apply(dsl.curve)
}


object Context {
def apply(xs: KeyValue*) = new Context(xs.asInstanceOf[Seq[(Key[_], Any)]].toMap)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
package org.scalameter



import java.util.Date
import scala.collection._



class Key[T](val name: String)(implicit container: KeyContainer) extends Serializable {
container.keys(name) = this

class Key[T](val name: String) extends Serializable {
override def toString = name
override def hashCode = name.hashCode
override def equals(x: Any) = x match {
Expand All @@ -12,18 +18,46 @@ class Key[T](val name: String) extends Serializable {
}
}

class KeyWithDefault[T](name: String, val defaultValue: T) extends Key[T](name)

class KeyWithDefault[T](name: String, val defaultValue: T)(implicit container: KeyContainer)
extends Key[T](name)(container)

object Key extends Keys {
def apply[T](name: String) = new Key[T](name)
def apply[T](name: String, defaultValue: T) = new KeyWithDefault[T](name, defaultValue)

object Key extends Keys {
def apply[T](name: String)(implicit container: KeyContainer) =
new Key[T](name)
def apply[T](name: String, defaultValue: T)(implicit container: KeyContainer) =
new KeyWithDefault[T](name, defaultValue)
implicit val ordering: Ordering[Key[_]] = Ordering.by(_.name)

}


abstract class KeyContainer(val containerName: String, val enclosing: KeyContainer) {
private[scalameter] val subs = mutable.Map[String, KeyContainer]()
private[scalameter] val keys = mutable.Map[String, Key[_]]()

implicit def container: KeyContainer = this

if (enclosing != null) enclosing.subs(containerName) = this

def parseKey(keyName: String): Key[_] = {
val parts = keyName.split(".")
parseKeyRecursive(parts.toList)
}

private[scalameter] def parseKeyRecursive(keyParts: List[String]): Key[_] = keyParts match {
case name :: Nil =>
keys(name)
case name :: remaining =>
subs(name).parseKeyRecursive(remaining)
case Nil =>
sys.error("no key with the given name!")
}
}


class Keys {
class Keys extends KeyContainer("", null) {

// Note: predefined keys need to be lazy
// due to initialization order issue with object Key
Expand All @@ -33,13 +67,13 @@ class Keys {
lazy val preJDK7 = Key[Boolean]("pre-jdk-7", false)
lazy val scopeFilter = Key[String]("scope-filter", "")

object dsl {
object dsl extends KeyContainer("dsl", Keys.this) {
lazy val curve = Key[String]("curve", "")
lazy val scope = Key[List[String]]("scope", Nil)
}

object machine {
object jvm {
object machine extends KeyContainer("machine", Keys.this) {
object jvm extends KeyContainer("jvm", machine) {
lazy val version = Key[String]("jvm-version")
lazy val vendor = Key[String]("jvm-vendor")
lazy val name = Key[String]("jvm-name")
Expand All @@ -51,25 +85,25 @@ class Keys {
lazy val hostname = Key[String]("hostname")
}

object gen {
object gen extends KeyContainer("gen", Keys.this) {
lazy val unit = Key[String]("unit")
}

object reports {
object reports extends KeyContainer("reports", Keys.this) {
lazy val startDate = Key[Option[Date]]("date-start", None)
lazy val endDate = Key[Option[Date]]("date-end", None)
lazy val bigO = Key[String]("big-o")
lazy val resultDir = Key[String]("result-dir", "tmp")
lazy val colors = Key[Boolean]("colors", true)

object regression {
object regression extends KeyContainer("regression", reports) {
lazy val significance = Key[Double]("significance", 1e-10)
lazy val timeIndices = Key[Seq[Long]]("time-indices")
lazy val noiseMagnitude = Key[Double]("regression-noise-magnitude", 0.0)
}
}

object exec {
object exec extends KeyContainer("exec", Keys.this) {
lazy val benchRuns = Key[Int]("runs", 10)
lazy val minWarmupRuns = Key[Int]("min-warmups", 10)
lazy val maxWarmupRuns = Key[Int]("max-warmups", 10)
Expand All @@ -79,18 +113,18 @@ class Keys {
lazy val jvmcmd = Key[String]("jvm-cmd", "java -server")
lazy val requireGC = Key[Boolean]("require-gc", false)

object reinstantiation {
object reinstantiation extends KeyContainer("reinstantiation", exec) {
lazy val frequency = Key[Int]("frequency")
lazy val fullGC = Key[Boolean]("full-gc")
}

object outliers {
object outliers extends KeyContainer("outliers", exec) {
lazy val suspectPercent = Key[Int]("suspect-percent", 25)
lazy val covMultiplier = Key[Double]("cov-multiplier", 2.0)
lazy val retries = Key[Int]("outlier-retries", 8)
}

object noise {
object noise extends KeyContainer("noise", exec) {
lazy val magnitude = Key[Double]("noise-magnitude", 0.0)
}
}
Expand Down
8 changes: 5 additions & 3 deletions src/main/scala/org/scalameter/FrameworkKey.scala
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package org.scalameter



/** Object containing keys specific to the ScalaMeter benchmarking framework.
*/
object FrameworkKey {
object FrameworkKey extends KeyContainer("", null) {

object dsl {
object dsl extends KeyContainer("dsl", FrameworkKey.this) {
lazy val executor = Key[Executor]("executor")
}

}
}
9 changes: 5 additions & 4 deletions src/main/scala/org/scalameter/Gen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ object Gen {
def vectors = for {
size <- sizes
} yield (0 until size).toVector

def arraybuffers = for {
size <- sizes
} yield mutable.ArrayBuffer(0 until size: _*)
Expand All @@ -161,6 +161,7 @@ object Gen {
for (x <- 0 until size) hm(x) = x
hm
}


def linkedhashtablemaps = for {
size <- sizes
Expand All @@ -169,7 +170,7 @@ object Gen {
for (x <- 0 until size) hm(x) = x
hm
}

def hashtriemaps = for {
size <- sizes
} yield {
Expand All @@ -195,15 +196,15 @@ object Gen {
for (x <- 0 until size) hs.add(x)
hs
}

def linkedhashtablesets = for {
size <- sizes
} yield {
val hs = mutable.LinkedHashSet[Int]()
for (x <- 0 until size) hs.add(x)
hs
}

def avlsets = for {
size <- sizes
} yield {
Expand Down
3 changes: 1 addition & 2 deletions src/main/scala/org/scalameter/Main.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ object Main {
// identify test objects
dyn.currentContext.withValue(Context.topLevel ++ configuration.context) {
import configuration._

// schedule benchmarks
val testResults = for (benchname <- benches) yield {
val bench = Class.forName(benchname).newInstance.asInstanceOf[PerformanceTest]
val bench = Class.forName(benchname).newInstance.asInstanceOf[DSL]
bench.executeTests()
}

Expand Down
7 changes: 4 additions & 3 deletions src/main/scala/org/scalameter/PerformanceTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package org.scalameter



import collection._
import utils.Tree
import java.util.Date
import scala.collection._
import utils.Tree



abstract class PerformanceTest extends PerformanceTest.Initialization with Serializable {
Expand Down Expand Up @@ -56,7 +57,7 @@ object PerformanceTest {
val regex = sf.r
regex.findFirstIn(fullname) != None
}

/** Quick benchmark run in the same JVM.
* Reports result into the console.
*/
Expand Down
6 changes: 3 additions & 3 deletions src/main/scala/org/scalameter/ScalaMeterFramework.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class ScalaMeterFramework extends Framework {
} try fingerprint match {
case fp: SubclassFingerprint =>
if (!fp.isModule) {
val ptest = testClassLoader.loadClass(testClassName).newInstance.asInstanceOf[PerformanceTest]
val ptest = testClassLoader.loadClass(testClassName).newInstance.asInstanceOf[DSL]
ptest.executeTests()
} else {
val module = Class.forName(testClassName + "$", true, testClassLoader)
Expand All @@ -75,12 +75,12 @@ class ScalaMeterFramework extends Framework {

private case object PerformanceTestClassFingerprint extends SubclassFingerprint {
def isModule = false
def superClassName = classOf[PerformanceTest].getName
def superClassName = classOf[DSL].getName
}

private case object PerformanceTestModuleFingerprint extends SubclassFingerprint {
def isModule = true
def superClassName = classOf[PerformanceTest].getName
def superClassName = classOf[DSL].getName
}

}
Expand Down
75 changes: 75 additions & 0 deletions src/main/scala/org/scalameter/SerializableMethod.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
package org.scalameter;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

//Makes the object Method Serializable, taken from http:https://stackoverflow.com/questions/4919205/java-serializing-methods
public class SerializableMethod implements Serializable {
private static final long serialVersionUID = 6631604036553063682L;
private Method method;

public SerializableMethod(Method method)
{
this.method = method;
}

public Method getMethod()
{
return method;
}
public void invoke(Object o){
try {
method.invoke(o);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Object invokeA(Object o1, Object o2){
Object ret = null;
try {
ret = method.invoke(o1, o2);
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return ret;
}
private void writeObject(ObjectOutputStream out) throws IOException
{
out.writeObject(method.getDeclaringClass());
out.writeUTF(method.getName());
out.writeObject(method.getParameterTypes());
}

private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException
{
Class<?> declaringClass = (Class<?>)in.readObject();
String methodName = in.readUTF();
Class<?>[] parameterTypes = (Class<?>[])in.readObject();
try
{
method = declaringClass.getMethod(methodName, parameterTypes);
}
catch (Exception e)
{
throw new IOException(String.format("Error occurred resolving deserialized method '%s.%s'", declaringClass.getSimpleName(), methodName), e);
}
}
}
4 changes: 2 additions & 2 deletions src/main/scala/org/scalameter/dsl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import utils.Tree



trait DSL {
abstract class DSL {

import DSL._

Expand Down Expand Up @@ -56,7 +56,7 @@ trait DSL {
events.emit(Event(
cls.getName, s"The `include` can only be used with benchmarks in classes -- make ${cls.getName} a class.",
Events.Error, new Exception("Cannot instantiate singleton object.")))
} else cls.newInstance.asInstanceOf[PerformanceTest]
} else cls.newInstance.asInstanceOf[DSL]
}

/** Runs all the tests in this test class or singleton object.
Expand Down
Loading

0 comments on commit 06dd0ee

Please sign in to comment.