如何在不超过分钟配额的情况下在 Google App Engine 上使用 Java?

发布于 2024-08-27 14:48:10 字数 2276 浏览 6 评论 0原文

doGet() Servlet 内的一个非常简单的 Java 代码在 GAE 上占用了超过一秒的 CPU 时间。我已经阅读了一些与配额相关的文档,显然我没有做错任何事情。

//Request the user Agent info
String userAgent = req.getHeader("User-Agent");

我想知道什么是使用 CPU 最多的,我使用了 google 帮助推荐。

    //The two lines below will get the CPU before requesting User-Agent Information
    QuotaService qs = QuotaServiceFactory.getQuotaService();
    long start = qs.getCpuTimeInMegaCycles();

    //Request the user Agent info
    String userAgent = req.getHeader("User-Agent");

    //The three lines below will get the CPU after requesting User-Agent Information 
    // and informed it to the application log.
    long end = qs.getCpuTimeInMegaCycles();
    double cpuSeconds = qs.convertMegacyclesToCpuSeconds(end - start);
    log.warning("CPU Seconds on geting User Agent: " + cpuSeconds);

上面的代码告诉我的唯一一件事是,检查标头将使用超过一秒(1000 毫秒)的 cpu 时间,这对于 Google 来说是日志面板上的警告。这似乎是一个非常简单的请求,但仍然使用了超过一秒的 cpu。

我缺少什么?


下面贴上日志图片供大家娱乐。 日志Google App Engine 缓慢报告

为了大家的利益,我发布完整的代码。

@SuppressWarnings("serial")
public class R2CComingSoonSiteServlet extends HttpServlet {

private static final Logger log = Logger.getLogger(R2CComingSoonSiteServlet.class.getName());

public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
    //The two lines below will get the CPU before requesting User-Agent Information
    QuotaService qs = QuotaServiceFactory.getQuotaService();
    long start = qs.getCpuTimeInMegaCycles();

    //Request the user Agent info
    String userAgent = req.getHeader("User-Agent");

    //The three lines below will get the CPU after requesting User-Agent Information 
    // and informed it to the application log.
    long end = qs.getCpuTimeInMegaCycles();
    double cpuSeconds = qs.convertMegacyclesToCpuSeconds(end - start);
    log.warning("CPU Seconds on geting User Agent: " + cpuSeconds);

    userAgent = userAgent.toLowerCase();
    if(userAgent.contains("iphone"))
        resp.sendRedirect("/mobIndex.html");
    else
        resp.sendRedirect("/index.html");} }

A very simple java code inside a doGet() servlet is getting more than a second of cpu time on GAE. I have read some quota related documentation and apparently I am not doing anything wrong.

//Request the user Agent info
String userAgent = req.getHeader("User-Agent");

I wanted to know what was using the CPU the most, I use a google help recommendation.

    //The two lines below will get the CPU before requesting User-Agent Information
    QuotaService qs = QuotaServiceFactory.getQuotaService();
    long start = qs.getCpuTimeInMegaCycles();

    //Request the user Agent info
    String userAgent = req.getHeader("User-Agent");

    //The three lines below will get the CPU after requesting User-Agent Information 
    // and informed it to the application log.
    long end = qs.getCpuTimeInMegaCycles();
    double cpuSeconds = qs.convertMegacyclesToCpuSeconds(end - start);
    log.warning("CPU Seconds on geting User Agent: " + cpuSeconds);

The only thing that the code above tells me is that inspecting the header will use more than a second (1000ms) of cpu time, which for Google is a warning on the log panel. That seems to be a very simple request and still is using more than a second of cpu.

What I am missing?


Below the image of the logs for everyone's entertainment.
logs for Google App Engine Slow Reports

I am posting the full code, for the benefit of all.

@SuppressWarnings("serial")
public class R2CComingSoonSiteServlet extends HttpServlet {

private static final Logger log = Logger.getLogger(R2CComingSoonSiteServlet.class.getName());

public void doGet(HttpServletRequest req, HttpServletResponse resp)
        throws IOException {
    //The two lines below will get the CPU before requesting User-Agent Information
    QuotaService qs = QuotaServiceFactory.getQuotaService();
    long start = qs.getCpuTimeInMegaCycles();

    //Request the user Agent info
    String userAgent = req.getHeader("User-Agent");

    //The three lines below will get the CPU after requesting User-Agent Information 
    // and informed it to the application log.
    long end = qs.getCpuTimeInMegaCycles();
    double cpuSeconds = qs.convertMegacyclesToCpuSeconds(end - start);
    log.warning("CPU Seconds on geting User Agent: " + cpuSeconds);

    userAgent = userAgent.toLowerCase();
    if(userAgent.contains("iphone"))
        resp.sendRedirect("/mobIndex.html");
    else
        resp.sendRedirect("/index.html");} }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

冷清清 2024-09-03 14:48:10

App Engine 上不再有任何每分钟配额。任何涉及他们的消息都已过时。如果您想更好地分析 CPU 使用情况,您可能需要尝试新发布的 Java 的 appstats

There are no longer any per-minute quotas on App Engine. Any messages referring to them are out of date. If you want to do better profiling of your CPU usage, you may want to try out the newly released appstats for Java.

玩物 2024-09-03 14:48:10

你的日志显示它只是有时很慢。

你的servlet对象的构建真的很慢吗?

Your logs show that it is only slow sometimes.

Is the construction of your servlet object really slow?

梦在深巷 2024-09-03 14:48:10

上面的代码告诉我的唯一一件事是,检查标头将使用超过一秒(1000 毫秒)的 CPU 时间,这对于 Google 来说是日志面板上的警告。这似乎是一个非常简单的请求,但仍然使用超过一秒的 cpu。

如果不调用配额 API,也会发生这种情况吗?

The only thing that the code above tells me is that inspecting the header will use more than a second (1000ms) of cpu time, which for Google is a warning on the log panel. That seems to be a very simple request and still is using more than a second of cpu.

Does this also happen without the calls to the quota API?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文