Skip to content

Commit

Permalink
Refactor costcenter internals
Browse files Browse the repository at this point in the history
  • Loading branch information
mschuwalow committed Mar 11, 2024
1 parent 05c8ee3 commit 8d06cbe
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ object PluginSamplingProfilerSpec extends BaseSpec {
)
val program = (ProfilerExamples.zioProgram *> CausalProfiler.progressPoint("done")).forever
Live.live(profiler.profile(program)).map { result =>
println(result)

def isSlowEffect(location: CostCenter) =
location.hasParentMatching("zio\\.profiling\\.ProfilerExamples\\.slow\\(.*\\)".r)
def isFastEffect(location: CostCenter) =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ object TaggingPhase extends PluginPhase {
private object TaggableTypeTree {
private def zioTypeRef(using Context): TypeRef = requiredClassRef("zio.ZIO")

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

def unapply(tp: Tree[Type])(using Context): Option[TaggingTarget] =
tp.tpe.dealias match {
Expand Down
61 changes: 19 additions & 42 deletions zio-profiling/src/main/scala/zio/profiling/CostCenter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,42 +16,20 @@ import scala.util.matching.Regex
* } yield ()
* }}}
*/
sealed trait CostCenter { self =>
import CostCenter._

final def render: String =
self match {
case Root => ""
case Child(Root, name) => name
case Child(parent, name) => s"${parent.render};$name"
}

final def location: Option[String] = self match {
case Root => None
case Child(_, current) => Some(current)
}

final def isRoot: Boolean = self match {
case Root => true
case _ => false
}
final case class CostCenter(locations: Chunk[String]) {
def render: String = locations.mkString(";")

def isRoot: Boolean = locations.isEmpty

/**
* Create a child cost center that is nested under this one.
*/
final def /(location: String): CostCenter = self match {
case Root => Child(Root, location)
case Child(_, current) =>
if (current == location)
self
else
Child(self, location)
}

final def isChildOf(other: CostCenter): Boolean = self == other || (self match {
case Root => false
case Child(parent, _) => parent.isChildOf(other)
})
def /(location: String): CostCenter = CostCenter(locations :+ location)

def location: Option[String] = locations.lastOption

def isChildOf(other: CostCenter): Boolean =
locations.startsWith(other.locations)

/**
* Check whether this cost center has a parent with a given name.
Expand All @@ -62,23 +40,22 @@ sealed trait CostCenter { self =>
* (Root / "foo" / "bar").hasParent("baz") // false
* }}}
*/
final def hasParent(name: String): Boolean = self match {
case Root => false
case Child(parent, current) => current == name || parent.hasParent(name)
}
def hasParent(name: String): Boolean =
locations.contains(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.findFirstIn(current).nonEmpty || parent.hasParentMatching(regex)
}
def hasParentMatching(regex: Regex): Boolean =
locations.exists(regex.findFirstIn(_).nonEmpty)
}

object CostCenter {
case object Root extends CostCenter
final case class Child(parent: CostCenter, current: String) extends CostCenter

/**
* The root cost center.
*/
val Root: CostCenter = CostCenter(Chunk.empty)

/**
* Get the current cost center this fiber is executing in.
Expand Down

0 comments on commit 8d06cbe

Please sign in to comment.