简单 play/scala 基准测试,渲染视图与原始文本输出比较

Simple play/scala benchmark, rendering a view versus a raw text output comparison

我刚刚对一个新的 play/scala 应用程序进行了基准测试。

在我的操作中执行简单的文本输出时,我每秒收到 60K 个请求。

如果我渲染视图(见下文),它会下降到每秒 13K。 由于视图只是 Scala 中的函数,我原以为调用函数的额外开销不会如此显着地降低每秒请求数。

我只 运行 10-30 秒的基准测试, jvm 优化可能需要更长的时间还是这只是预期的行为?

def index() = Action { implicit request: Request[AnyContent] =>
    Ok("hello")
}

如果我实际渲染视图,每秒请求数会下降到大约 13K

def index() = Action { implicit request: Request[AnyContent] =>
    Ok(views.html.index())
}

/app/views/index.scala.html

@()

@main("Welcome to Play") {
  <h1>Welcome to Play!</h1>
}

/app/views/main.scala.html

@*
 * This template is called from the `index` template. This template
 * handles the rendering of the page header and body tags. It takes
 * two arguments, a `String` for the title of the page and an `Html`
 * object to insert into the body of the page.
 *@
@(title: String)(content: Html)

<!DOCTYPE html>
<html lang="en">
    <head>
        @* Here's where we render the page title `String`. *@
        <title>@title</title>
        <link rel="stylesheet" media="screen" href="@routes.Assets.versioned("stylesheets/main.css")">
        <link rel="shortcut icon" type="image/png" href="@routes.Assets.versioned("images/favicon.png")">

    </head>
    <body>
        @* And here's where we render the `Html` object containing
         * the page content. *@
        @content

      <script src="@routes.Assets.versioned("javascripts/main.js")" type="text/javascript"></script>
    </body>
</html>

正如我在评论中所说,视图不是一个微不足道的函数。它执行字符串连接并调用 routes.Assets.versioned 三次。分析会话显示,视图基本上只等待这个函数:

深入研究,我们了解到,versioned 函数总是从类路径重新读取文件:

或许你可以开个issue问问Play框架的作者,assets的serving是否可以优化得更好?

编辑:我分析了两个设置。首先我修改了HomeControllerSpec test:

"render the index page from a new instance of controller" in {
  val controller = new HomeController(stubControllerComponents())
  val indexAction = controller.index()
  val fakeRequest = FakeRequest(GET, "/")

  var i = 100000
  while (i > 0) {
    indexAction.apply(fakeRequest)
    i -= 1
  }

但这并不排除某些组件在生产模式下的行为可能有所不同。所以我 运行 sbt stage 并开始生成应用程序,将分析器附加到 运行 JVM 并向分析的 JVM 执行 10000 个请求。不过结果是一样的。