Revision created by MOE tool push_codebase.
[gae.git] / java / src / main / com / google / apphosting / api / ApiStats.java
blob7a19428316a33632ef2bea0380c964913885f26b
1 // Copyright 2009 Google Inc. All Rights Reserved.
3 package com.google.apphosting.api;
5 /**
6 * Represents automatic statistics collected by the ApiProxy. If present, this
7 * object will be stored under the KEY-variable in an Environment's property.
8 * This object is basically a pojo, a simple container for collected values.
11 public abstract class ApiStats {
13 /**
14 * The name that this object is stored in.
16 private static final String KEY = ApiStats.class.getName();
18 /**
19 * For a given environment, return the corresponding ApiStats object. If the
20 * environment does not have an ApiStats object, null will be returned.
22 public static ApiStats get(ApiProxy.Environment env) {
23 return (ApiStats) env.getAttributes().get(KEY);
26 /**
27 * @return the overall time spent in API cycles, as returned by the system.
28 * Unit is megacycles.
30 public abstract long getApiTimeInMegaCycles();
32 /**
33 * @return the overall time spent in CPU processing. Unit is megacycles.
35 public abstract long getCpuTimeInMegaCycles();
37 /**
38 * Creates a new ApiStats object and binds it to a given Environment.
39 * @param env the Environment object to bind this object to.
40 * @exception IllegalStateException if an object is already bound
42 protected ApiStats(ApiProxy.Environment env){
43 bind(env);
46 /**
47 * Binds this object to a particular environment. This step enables the
48 * stats object to access and make available request-specific statistics that
49 * would usually not be accessible.
50 * @param env the Environment object to bind this object to.
51 * @exception IllegalStateException if an object is already bound
53 private void bind(ApiProxy.Environment env) {
54 ApiStats original = get(env);
55 if (original != null) {
57 if (original != this) {
58 throw new IllegalStateException(
59 "Cannot replace existing ApiStats object");
60 } else {
63 } else {
64 env.getAttributes().put(KEY, this);