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。
我错过了什么?
为了所有人的利益,我发布了完整的代码。
@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");} }