How to use Java on Google App Engine without exceeding minute quotas?
-
22-09-2019 - |
문제
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.
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");} }
해결책
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.
다른 팁
Your logs show that it is only slow sometimes.
Is the construction of your servlet object really slow?
오 소년.결정은 링크가 을 포함하는 문자열에 적용된 STRPOS의 결과를 기반으로합니다.$str
에는 첫 번째 유형의 링크가 포함되어 있으므로 해당 if 조건은 항상 true입니다.정규 표현식을 사용하여 해당 문제를 해결하거나 단순화 된 예제가 아니라 실제 코드가 아니라면 다음을 시도하십시오.
.$str='<a href="http://www.sitea.com/vip:vp_14098">link A</a><a href="http://www.sitea.com/contact">link B</a><a href="http://www.siteb.com/player">link C</a>';
$str = str_replace('http://www.sitea.com/vip:', 'http://localhost/aaa?search=vip:',$str);
$str = str_replace('http://www.sitea.com/', 'http://localhost/bbb/',$str);
$str = str_replace('http://www.siteb.com/', 'http://localhost/ccc/',$str);
echo $str;