-
-
Save esfand/369580 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package testlib; | |
import java.io.IOException; | |
import java.util.ArrayList; | |
import java.util.concurrent.Future; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServlet; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import com.google.apphosting.api.ApiProxy; | |
import com.google.apphosting.api.DatastorePb; | |
import com.google.apphosting.api.ApiProxy.Delegate; | |
import com.google.apphosting.api.ApiProxy.Environment; | |
import com.google.storage.onestore.v3.OnestoreEntity.EntityProto; | |
import com.google.storage.onestore.v3.OnestoreEntity.Path; | |
import com.google.storage.onestore.v3.OnestoreEntity.Property; | |
import com.google.storage.onestore.v3.OnestoreEntity.PropertyValue; | |
import com.google.storage.onestore.v3.OnestoreEntity.Reference; | |
public class DirectProtocolBufferTestServlet extends HttpServlet { | |
/** | |
* | |
*/ | |
private static final long serialVersionUID = 1L; | |
Logger logger = Logger.getLogger(DirectProtocolBufferTestServlet.class.getName()); | |
@Override | |
protected void service(HttpServletRequest request, HttpServletResponse response) | |
throws ServletException, IOException { | |
if(request.getRequestURI().matches(".*/async$")) { | |
async(); | |
} | |
if(request.getRequestURI().matches(".*/sync$")) { | |
sync(); | |
} | |
} | |
long allocateIds(String kind,int size) { | |
Delegate<Environment> delegate = ApiProxy.getDelegate(); | |
Environment env = ApiProxy.getCurrentEnvironment(); | |
String appId = ApiProxy.getCurrentEnvironment().getAppId(); | |
long idFrom; | |
{//allocateIds | |
DatastorePb.AllocateIdsRequest idRequest = new DatastorePb.AllocateIdsRequest(); | |
Path path = new Path(); | |
path.addElement().setType(kind).setName("ignored"); | |
idRequest.setModelKey(new Reference().setApp(appId).setPath(path)); | |
idRequest.setSize(size); | |
byte[] result = delegate.makeSyncCall(env, "datastore_v3", "AllocateIds", idRequest.toByteArray()); | |
DatastorePb.AllocateIdsResponse idResponse = new DatastorePb.AllocateIdsResponse(); | |
idResponse.mergeFrom(result); | |
idFrom = idResponse.getStart(); | |
} | |
return idFrom; | |
} | |
void sync() { | |
long start = allocateIds("synckind",100); | |
long t1 = System.currentTimeMillis(); | |
Delegate<Environment> delegate = ApiProxy.getDelegate(); | |
Environment env = ApiProxy.getCurrentEnvironment(); | |
String appId = ApiProxy.getCurrentEnvironment().getAppId(); | |
for(long i=start;i<start+100;i++) { | |
DatastorePb.PutRequest putRequest = new DatastorePb.PutRequest(); | |
//putRequest.setTransaction(null); | |
Path path = new Path(); | |
path.addElement().setId(i).setType("synckind"); | |
EntityProto entityProto = putRequest.addEntity(); | |
entityProto.setKey(new Reference().setApp(appId).setPath(path)); | |
entityProto.addProperty(new Property().setName("testfield0").setValue(new PropertyValue().setStringValue("syncvalue"+i))); | |
byte[] result = delegate.makeSyncCall(env, "datastore_v3", "Put", putRequest.toByteArray()); | |
DatastorePb.PutResponse putResponse = new DatastorePb.PutResponse(); | |
putResponse.mergeFrom(result); | |
Reference k = putResponse.getMutableKey(0); | |
//logger.warning("Put response Key:"+k.toFlatString()); | |
} | |
logger.warning(String.format("doing makeSyncCall elapsed time:%d",System.currentTimeMillis()-t1)); | |
} | |
void async() { | |
long start = allocateIds("asynckind",100); | |
long t1 = System.currentTimeMillis(); | |
Delegate<Environment> delegate = ApiProxy.getDelegate(); | |
Environment env = ApiProxy.getCurrentEnvironment(); | |
String appId = ApiProxy.getCurrentEnvironment().getAppId(); | |
ApiProxy.ApiConfig config = new ApiProxy.ApiConfig(); | |
config.setDeadlineInSeconds(10.0); | |
ArrayList<Future<byte[]>> futureList = new ArrayList<Future<byte[]>>(); | |
for(long i=start;i<start+100;i++) { | |
DatastorePb.PutRequest putRequest = new DatastorePb.PutRequest(); | |
Path path = new Path(); | |
path.addElement().setId(i).setType("asynckind"); | |
EntityProto entityProto = putRequest.addEntity(); | |
entityProto.setKey(new Reference().setApp(appId).setPath(path)); | |
entityProto.addProperty(new Property().setName("testfield0").setValue(new PropertyValue().setStringValue("asyncvalue"+i))); | |
Future<byte[]> future = delegate.makeAsyncCall(env, "datastore_v3", "Put", putRequest.toByteArray(),config); | |
futureList.add(future); | |
} | |
for(Future<byte[]> future:futureList) { | |
try { | |
byte[] result = future.get(); | |
DatastorePb.PutResponse putResponse = new DatastorePb.PutResponse(); | |
putResponse.mergeFrom(result); | |
Reference k = putResponse.getMutableKey(0); | |
//logger.warning("Put response Key:"+k.toFlatString()); | |
} catch (Exception e) { | |
logger.log(Level.WARNING,"future response got exception",e); | |
} | |
} | |
logger.warning(String.format("doing makeSyncCall elapsed time:%d",System.currentTimeMillis()-t1)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment