2017-01-18 6 views
0

Ich habe ein sbt Autoplugin und wenn der Benutzer eine Aufgabe ausführt, möchte ich eine neue JVM mit einem -javaagent abzweigen. Die Aufgabe sollte Speicher unter Verwendung jamm messen.sbt autoplugin: füge javaagent für Aufgabe hinzu

object SbtMemory extends AutoPlugin { 
    object autoImport { 
     val agentTest = inputKey[Unit]("Run task with javaagent") 
    } 
    def makeAgentOptions(classpath: Classpath) : String = { 
      val jammJar = classpath.map(_.data).filter(_.toString.contains("jamm")).head 
      s"-javaagent:$jammJar" 
     } 
    override lazy val projectSettings = 
      Seq(
       agentTest := agentTask.value, 
       fork in agentTest := true, 
       javaOptions in agentTest += (dependencyClasspath in Test).map(makeAgentOptions).value 
      ) 

    lazy val agentTask = Def.task { 
     val o = new Array[Byte](1024*1024) 
     val mm = new MemoryMeter() 
     println("Size of new Array[Byte](1024*1024): " + mm.measureDeep(o)) 
    } 
} 

Wenn ich sbt perf von der Kommandozeile ausführen, bekomme ich die folgende Ausnahme:

java.lang.IllegalStateException: Instrumentation is not set; Jamm must be set as -javaagent 

Ich habe auch versucht die javaOptions Druck und die -javaagent Option nicht gesetzt wurde.

Wie kann ich die -javaagent javaOption innerhalb des Plugins hinzufügen, um die Aufgabe mit jamm auszuführen?

Danke!

Antwort

0

Offenbar ist fork nur für die Aufgabe run und test verfügbar. Ich fügte meinen eigenen Forking-Code hinzu und verlegte den Kennzahlcode in eine separate Klasse MemoryMeasure:

val mainClass: String = "MemoryMeasure" 
val forkOptions = ForkOptions(
    bootJars = (fullClasspath in Test).value.files, 
    runJVMOptions = Seq(
     (dependencyClasspath in Test).map(makeAgentOptions).value 
    ) 
) 
val process = Fork.java.fork(forkOptions, mainClass +: arguments) 
def cancel() = { 
    process.destroy() 
    1 
} 
val exitCode = try process.exitValue() catch { case e: InterruptedException => cancel() } 
Verwandte Themen