Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions sjsonnet/src/sjsonnet/Profiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,12 @@ object ProfileOutputFormat {
*/
final class Profiler(val format: ProfileOutputFormat, wd: Path) {

private def relativeFileName(pos: Position): Option[String] =
Option(pos).flatMap(p => Option(p.currentFile)).map(_.relativeToString(wd))

private class ExprBox(val expr: Expr) {
val id: BoxId = {
val fileName = Option(expr.pos)
.map(_.currentFile.relativeToString(wd))
.getOrElse("<unknown>")
val fileName = relativeFileName(expr.pos).getOrElse("<unknown>")

val name = {
val exprName = expr.getClass.getName.split('.').last.split('$').last
Expand Down Expand Up @@ -111,11 +112,8 @@ final class Profiler(val format: ProfileOutputFormat, wd: Path) {

private def flameGraphFrameName(e: Expr): String = {
val name = e.exprErrorString
if (isBuiltin(e) || e.pos == null) name
else {
val file = e.pos.currentFile.relativeToString(wd)
s"$name ($file)"
}
if (isBuiltin(e)) name
else relativeFileName(e.pos).fold(name)(file => s"$name ($file)")
}

private def getOrCreate(e: Expr): ExprBox = {
Expand Down
50 changes: 50 additions & 0 deletions sjsonnet/test/src/sjsonnet/ProfilerTests.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package sjsonnet

import utest._

object ProfilerTests extends TestSuite {
private val code =
"""local f(x) = std.length(x) == std.length(std.objectFields({ a: 1, b: 2 }));
|f([1, 2])""".stripMargin

private def interpreter(): Interpreter =
new Interpreter(
Map.empty,
Map.empty,
DummyPath("root"),
Importer.empty,
parseCache = new DefaultParseCache
)

def tests: Tests = Tests {
test("synthetic static values have unknown source files") {
Seq(ProfileOutputFormat.Text, ProfileOutputFormat.FlameGraph).foreach { format =>
val interp = interpreter()
val profiler = new Profiler(format, DummyPath("root"))
interp.evaluator.profiler = profiler

interp.interpret(code, DummyPath("root", "main.jsonnet")) ==> Right(ujson.True)

val boxes = profiler.collectResult().boxes
assert(boxes.exists(_.id == BoxId("Num", "<unknown>", -1)))
}
}

test("flame graph frames can have synthetic positions") {
val pos = new Position(null, -1)
val frame = Expr.Apply1(
pos,
Expr.Id(pos, "f"),
Val.Null(pos),
tailstrict = false
)
val profiler = new Profiler(ProfileOutputFormat.FlameGraph, DummyPath("root"))

val saved = profiler.enter(frame)
profiler.exit(saved)

val boxes = profiler.collectResult().boxes
assert(boxes.exists(_.id.fileName == "<unknown>"))
}
}
}
Loading