Skip to content

Commit

Permalink
Fix plugin on scala 3.3.3
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuwalow committed Mar 11, 2024
1 parent abdea68 commit 79a2867
Show file tree
Hide file tree
Showing 6 changed files with 100 additions and 16 deletions.
22 changes: 18 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ inThisBuild(
)
)

addCommandAlias("compileSources", "core/Test/compile; taggingPlugin/compile; examples/compile")
addCommandAlias("testAll", "core/test")
addCommandAlias("compileSources", "core/Test/compile; taggingPlugin/compile; taggingPluginTests/compile; examples/compile; benchmarks/compiile;")
addCommandAlias("testAll", "core/test; taggingPluginTests/test")

addCommandAlias("check", "fixCheck; fmtCheck")
addCommandAlias("fix", "scalafixAll")
Expand All @@ -28,7 +28,7 @@ addCommandAlias("prepare", "fix; fmt")
lazy val root = project
.in(file("."))
.settings(publish / skip := true)
.aggregate(core, jmh, taggingPlugin, examples, benchmarks, docs)
.aggregate(core, jmh, taggingPlugin, taggingPluginTests, examples, benchmarks, docs)

lazy val core = project
.in(file("zio-profiling"))
Expand Down Expand Up @@ -62,6 +62,20 @@ lazy val taggingPlugin = project
pluginDefinitionSettings
)

lazy val taggingPluginTests = project
.in(file("zio-profiling-tagging-plugin-tests"))
.dependsOn(core, taggingPlugin % "plugin")
.settings(
stdSettings("zio-profiling-tagging-plugin-tests"),
publish / skip := true,
Compile / scalacOptions += s"-Xplugin:${(taggingPlugin / Compile / packageTask).value.getAbsolutePath}",
libraryDependencies ++= Seq(
"dev.zio" %% "zio-test" % zioVersion % Test,
"dev.zio" %% "zio-test-sbt" % zioVersion % Test
),
testFrameworks += new TestFramework("zio.test.sbt.ZTestFramework")
)

lazy val examples = project
.in(file("examples"))
.dependsOn(core, taggingPlugin % "plugin")
Expand All @@ -76,7 +90,7 @@ lazy val benchmarks = project
.dependsOn(core)
.enablePlugins(JmhPlugin)
.settings(
stdSettings("examples"),
stdSettings("benchmarks"),
publish / skip := true
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package zio.profiling.examples
import zio._
import zio.profiling.sampling._

object PresentationExample extends ZIOAppDefault {
object SamplingProfilerSimpleExample extends ZIOAppDefault {
def run: URIO[Any, ExitCode] = {

val fast = ZIO.succeed(Thread.sleep(400))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package zio.profiling

import zio._
import zio.stream.ZStream

object ProfilerExamples {
val fast: ZIO[Any, Nothing, Unit] = ZIO.succeed(Thread.sleep(400))

val slow: ZIO[Any, Nothing, Unit] = ZIO.succeed(Thread.sleep(200)) <&> ZIO.succeed(Thread.sleep(600))

val zioProgram: ZIO[Any, Nothing, Unit] = fast <&> slow

val fastStream: ZStream[Any, Nothing, Unit] = ZStream.fromZIO(fast)

val slowStream: ZStream[Any, Nothing, Unit] = ZStream.fromZIO(slow)

val zioStreamProgram: ZIO[Any, Nothing, Unit] = (fastStream <&> slowStream).runDrain
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package zio.profiling.sampling

import zio.profiling.{CostCenter, ProfilerExamples}
import zio.test.Assertion.{hasSize, isGreaterThanEqualTo}
import zio.test._
import zio.Scope

object PluginSamplingProfilerSpec extends ZIOSpecDefault {

def spec: Spec[Environment with TestEnvironment with Scope, Any] = suite("PluginSamplingProfiler")(
test("Should correctly profile simple example program") {
Live.live(SamplingProfiler().profile(ProfilerExamples.zioProgram)).map { result =>
val sortedEntries = result.entries.sortBy(_.samples).reverse

def isSlowEffect(location: CostCenter) =
location.hasParentMatching("zio\\.profiling\\.ProfilerExamples\\.slow\\(.*\\)".r)
def isFastEffect(location: CostCenter) =
location.hasParentMatching("zio\\.profiling\\.ProfilerExamples\\.fast\\(.*\\)".r)

assert(sortedEntries)(hasSize(isGreaterThanEqualTo(2))) &&
assertTrue(isSlowEffect(sortedEntries(0).costCenter)) &&
assertTrue(isFastEffect(sortedEntries(1).costCenter))
}
},
test("Should correctly profile simple example streams program") {
Live.live(SamplingProfiler().profile(ProfilerExamples.zioStreamProgram)).map { result =>
val sortedEntries = result.entries.sortBy(_.samples).reverse

def isSlowEffect(location: CostCenter) =
location.hasParentMatching("zio\\.profiling\\.ProfilerExamples\\.slowStream\\(.*\\)".r)
def isFastEffect(location: CostCenter) =
location.hasParentMatching("zio\\.profiling\\.ProfilerExamples\\.fastStream\\(.*\\)".r)

assert(sortedEntries)(hasSize(isGreaterThanEqualTo(2))) &&
assertTrue(isSlowEffect(sortedEntries(0).costCenter)) &&
assertTrue(isFastEffect(sortedEntries(1).costCenter))
}
}
)

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import dotty.tools.dotc.report
import dotty.tools.dotc.core.Types.TypeRef
import dotty.tools.dotc.ast.tpd.{TreeOps, Literal}
import dotty.tools.dotc.ast.untpd.Mod.Given.apply
import dotty.tools.dotc.core.Flags

object TaggingPhase extends PluginPhase {

Expand All @@ -23,12 +22,14 @@ object TaggingPhase extends PluginPhase {
override val runsAfter = Set(Pickler.name)
override val runsBefore = Set(Staging.name)

override def transformValDef(tree: tpd.ValDef)(using Context): tpd.Tree = tree match {
case ValDef(_, TaggableTypeTree(taggingTarget), rhs) if !tree.rhs.isEmpty =>
val transformedRhs = tagEffectTree(descriptiveName(tree), tree.rhs, taggingTarget)
cpy.ValDef(tree)(rhs = transformedRhs)
case _ =>
tree
override def transformValDef(tree: tpd.ValDef)(using Context): tpd.Tree = {
tree match {
case ValDef(_, TaggableTypeTree(taggingTarget), rhs) if !tree.rhs.isEmpty =>
val transformedRhs = tagEffectTree(descriptiveName(tree), tree.rhs, taggingTarget)
cpy.ValDef(tree)(rhs = transformedRhs)
case _ =>
tree
}
}

override def transformDefDef(tree: tpd.DefDef)(using Context): tpd.Tree = tree match {
Expand All @@ -48,8 +49,8 @@ object TaggingPhase extends PluginPhase {
}

private def tagEffectTree(name: String, tree: tpd.Tree, taggingTarget: TaggingTarget)(using Context): tpd.Tree = {
val costcenterSym = requiredModule("_root_.zio.profiling.CostCenter")
val traceSym = requiredModule("_root_.zio.Trace")
val costcenterSym = requiredModule("zio.profiling.CostCenter")
val traceSym = requiredModule("zio.Trace")
val emptyTraceSym = traceSym.requiredMethodRef("empty")

taggingTarget match {
Expand Down Expand Up @@ -79,9 +80,9 @@ object TaggingPhase extends PluginPhase {
private case class ZStreamTaggingTarget(rType: Type, eType: Type, aType: Type) extends TaggingTarget

private object TaggableTypeTree {
private def zioTypeRef(using Context): TypeRef = requiredClassRef("_root_.zio.ZIO")
private def zioTypeRef(using Context): TypeRef = requiredClassRef("zio.ZIO")

private def zStreamTypeRef(using Context): TypeRef = requiredClassRef("_root_.stream.ZStream")
private def zStreamTypeRef(using Context): TypeRef = requiredClassRef("stream.ZStream")

def unapply(tp: Tree[Type])(using Context): Option[TaggingTarget] =
tp.tpe.dealias match {
Expand Down
10 changes: 10 additions & 0 deletions zio-profiling/src/main/scala/zio/profiling/CostCenter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package zio.profiling
import zio._
import zio.stream.ZStream

import scala.util.matching.Regex

/**
* A CostCenter allows grouping multiple source code locations into one unit for reporting and targeting purposes.
* Instead of relying on a function call hierarchy to identify a location, zio-profiling relies on manual tagging.
Expand Down Expand Up @@ -52,6 +54,14 @@ sealed trait CostCenter { self =>
case Root => false
case Child(parent, current) => current == name || parent.hasParent(name)
}

/**
* Check whether this cost center has a parent with a name matching the given regex.
*/
final def hasParentMatching(regex: Regex): Boolean = self match {
case Root => false
case Child(parent, current) => regex.matches(current) || parent.hasParentMatching(regex)
}
}

object CostCenter {
Expand Down

0 comments on commit 79a2867

Please sign in to comment.