java - 为什么在 Google App Engine 上首次访问 JSP 文件需要更长的时间?

标签 java google-app-engine

这是我的测试:我访问一个servlet来让JVM加载(吸收加载请求)。然后我访问一个包含 <%= 5+7 %> 的 JSP就在其中,没有别的。第一次访问此 JSP 使用 350 毫秒的 CPU 时间,延迟为 200 毫秒。后续访问只有大约 20ms 的延迟和 cpu 时间。

为什么启动servlet容器后第一次访问jsp需要更长的时间?

编辑:请注意,Google App Engine 应该预编译 jsps。当我上传我的应用程序时,我可以在控制台输出中看到 Initializing precompilation... .

编辑:这是对任何 JSP 的第一次访问,然后是后续访问,即使它对不同的 JSP 也不会花费很长时间。就好像 JSP 系统有一些加载要做或有什么事情。

另外,这不是一个加载请求,我先访问一个servlet,然后再访问JSP来吸收加载请求。此外,没有任何加载请求使用的 CPU 时间少于 500 毫秒。

最佳答案

JSP 在第一次访问/请求时被编译为扩展 HttpServlet 的类。一些 servlet 容器将在启动期间直接编译它(或可配置),这样您就不会注意到“滞后”。其他人不支持它,然后您需要自己预编译 JSP 文件并用它部署您的 WAR。

我从未使用过它,所以我无法从头顶回答,但快速浏览一下它的 Java FAQ让我知道you can enable precompilation on startup将以下条目添加到 appengine-web.xml 文件中:

<precompilation-enabled>true</precompilation-enabled>

更新:根据 appengine 文档的性能部分,它似乎会按需启动(和关闭)JVM,这可能会导致“加载请求”。这是 documentation 的摘录:

What is a loading request?

Some requests run slower because App Engine needs to create a new Java virtual machine to service the request. We call that kind of request, a Loading Request. During a loading request, your application undergoes initialization (such as class loading, JIT compiling, etc) which causes the request to take longer.

For slow requests which are already close to App Engine's request deadline, the extra initialization can push it past the deadline, causing a DeadlineExceededException. What causes loading requests?

App Engine spins up JVMs on demand, so there are several reasons why you may receive a loading request:

  1. You just uploaded a new version of your application.
  2. Your application may not be getting any traffic.
  3. Your traffic has become high enough to need another JVM to scale.

You can expect that during the course of developing your application, you will often experience the first two scenarios. In comparison, for a production app receiving even a very small but steady amount of traffic, loading requests are relatively infrequent.

换句话说,这无法通过任何编程方式解决。要么就接受它,要么考虑使用带有完整 servlet 容器的专用服务器。

关于java - 为什么在 Google App Engine 上首次访问 JSP 文件需要更长的时间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2464755/

相关文章:

java - 如何在 JFrame 中显示/隐藏 JPanel?

java - Spring MVC 绑定(bind)到类型字段

java - 如何在 Android 和 Google App Engine 中使用共享类

python - WTForms:我将每个字段单独绑定(bind)到对象上,有没有办法一次性将对象绑定(bind)到表单?

python - Appengine - 是否可以仅使用没有模型名称的键字符串来获取实体?

java - Google AppEngine java.net.Socket 受限错误

java - 在 IntelliJ 的 Kotlin > Java 转换后将源代码恢复为 Java

java.sql.SQLException : No suitable driver found for jdbc:sqlserver 异常

java.lang.ClassNotFoundException : org. apache.commons.lang.exception.NetableRuntimeException 异常

google-app-engine - Google App Engine 和 Google Compute Engine 有什么区别?