use of edu.umass.cs.gnsserver.utils.ValuesMap in project GNS by MobilityFirst.
the class ActiveHandler method main.
/***************** Test methods ****************/
/**
* @param args
* @throws JSONException
* @throws ExecutionException
* @throws InterruptedException
*/
public static void main(String[] args) throws JSONException, InterruptedException, ExecutionException {
int numProcess = Integer.parseInt(args[0]);
int numThread = Integer.parseInt(args[1]);
boolean blocking = Boolean.parseBoolean(args[2]);
if (numProcess <= 0) {
System.out.println("Number of clients must be larger than 0.");
System.exit(0);
}
final ThreadPoolExecutor executor;
executor = new ThreadPoolExecutor(numProcess * numThread, numProcess * numThread, 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
executor.prestartAllCoreThreads();
String guid = "4B48F507395639FD806459281C3C09BCBB16FDFF";
String field = "someField";
String noop_code = "";
try {
noop_code = new String(Files.readAllBytes(Paths.get("./scripts/activeCode/noop.js")));
} catch (IOException e) {
e.printStackTrace();
}
ValuesMap value = new ValuesMap();
value.put(field, "someValue");
// initialize a handler
ActiveHandler handler = new ActiveHandler("", null, numProcess, numThread, blocking);
ArrayList<Future<JSONObject>> tasks = new ArrayList<Future<JSONObject>>();
int n = 1000000;
long t1 = System.currentTimeMillis();
for (int i = 0; i < n; i++) {
tasks.add(executor.submit(new ActiveTask(clientPool[i % numProcess], guid, field, noop_code, value, 0)));
}
for (Future<JSONObject> task : tasks) {
task.get();
}
long elapsed = System.currentTimeMillis() - t1;
System.out.println("It takes " + elapsed + "ms, and the average latency for each operation is " + (elapsed * 1000.0 / n) + "us");
System.out.println("The throughput is " + Util.df(n * 1000.0 / elapsed) + "reqs/sec");
handler.shutdown();
System.out.println(DelayProfiler.getStats());
System.exit(0);
}
use of edu.umass.cs.gnsserver.utils.ValuesMap in project GNS by MobilityFirst.
the class ActiveBlockingClient method main.
/**
* Unit test
*
* @param args
* @throws InterruptedException
* @throws JSONException
* @throws ActiveException
*/
public static void main(String[] args) throws InterruptedException, JSONException, ActiveException {
final String suffix = "";
String cfile = "/tmp/client" + suffix;
String sfile = "/tmp/server" + suffix;
final int numThread = 2;
long executionTime = 1000;
if (System.getProperty("executionTime") != null) {
executionTime = Long.parseLong(System.getProperty("executionTime"));
}
String codeFile = "./scripts/activeCode/noop.js";
if (System.getProperty("codeFile") != null) {
codeFile = System.getProperty("codeFile");
}
ActiveBlockingClient client = new ActiveBlockingClient("", null, cfile, sfile, 0, numThread);
String guid = "guid";
String field = "name";
String code = "";
try {
code = new String(Files.readAllBytes(Paths.get(codeFile)));
} catch (IOException e) {
e.printStackTrace();
}
ValuesMap value = new ValuesMap();
value.put(field, executionTime);
JSONObject result = client.runCode(null, guid, field, code, value, 0, 1000);
System.out.println(result);
assertEquals(result.toString(), value.toString());
int initial = client.getRecv();
int n = 100000;
long t1 = System.currentTimeMillis();
for (int i = 0; i < n; i++) {
client.runCode(null, guid, field, code, value, 0, executionTime);
}
long elapsed = System.currentTimeMillis() - t1;
System.out.println("It takes " + elapsed + "ms");
System.out.println("The average time for each task is " + elapsed / n + "ms.");
assert (client.getRecv() - initial == n) : "the number of responses is not the same as the number of requests";
System.out.println("Sequential throughput test succeeds.");
client.shutdown();
}
use of edu.umass.cs.gnsserver.utils.ValuesMap in project GNS by MobilityFirst.
the class FieldAccess method lookupMultipleValues.
/**
* Reads the value of all the fields in a guid.
* Doesn't return internal system fields.
*
* @param header
* @param commandPacket
* @param guid
* @param reader
* @param signature
* @param message
* @param handler
* @param timestamp
* @return a command response
*/
public static CommandResponse lookupMultipleValues(InternalRequestHeader header, CommandPacket commandPacket, String guid, String reader, String signature, String message, Date timestamp, ClientRequestHandlerInterface handler) {
ResponseCode errorCode = FieldAccess.signatureAndACLCheckForRead(header, commandPacket, guid, GNSProtocol.ENTIRE_RECORD.toString(), //fields
null, reader, signature, message, timestamp, handler.getApp());
if (errorCode.isExceptionOrError()) {
return new CommandResponse(errorCode, GNSProtocol.BAD_RESPONSE.toString() + " " + errorCode.getProtocolCode());
}
String resultString;
ResponseCode responseCode;
try {
ValuesMap valuesMap = NSFieldAccess.lookupJSONFieldLocally(header, guid, GNSProtocol.ENTIRE_RECORD.toString(), handler.getApp());
if (valuesMap != null) {
resultString = valuesMap.removeInternalFields().toString();
responseCode = ResponseCode.NO_ERROR;
} else {
resultString = GNSProtocol.BAD_RESPONSE.toString();
responseCode = ResponseCode.BAD_GUID_ERROR;
}
} catch (FailedDBOperationException e) {
resultString = GNSProtocol.BAD_RESPONSE.toString();
responseCode = ResponseCode.DATABASE_OPERATION_ERROR;
}
return new CommandResponse(responseCode, resultString);
}
use of edu.umass.cs.gnsserver.utils.ValuesMap in project GNS by MobilityFirst.
the class FieldAccess method lookupOneMultipleValues.
/**
* Returns the first value of all the fields in a guid in an old-style JSONArray field value.
*
* @param header
* @param commandPacket
* @param guid
* @param reader
* @param signature
* @param message
* @param timestamp
* @param handler
* @return a command response
*/
public static CommandResponse lookupOneMultipleValues(InternalRequestHeader header, CommandPacket commandPacket, String guid, String reader, String signature, String message, Date timestamp, ClientRequestHandlerInterface handler) {
ResponseCode errorCode = FieldAccess.signatureAndACLCheckForRead(header, commandPacket, guid, GNSProtocol.ENTIRE_RECORD.toString(), //fields
null, reader, signature, message, timestamp, handler.getApp());
if (errorCode.isExceptionOrError()) {
return new CommandResponse(errorCode, GNSProtocol.BAD_RESPONSE.toString() + " " + errorCode.getProtocolCode());
}
String resultString;
ResponseCode responseCode;
try {
ValuesMap valuesMap = NSFieldAccess.lookupJSONFieldLocally(null, guid, GNSProtocol.ENTIRE_RECORD.toString(), handler.getApp());
if (valuesMap != null) {
resultString = valuesMap.removeInternalFields().toJSONObjectFirstOnes().toString();
responseCode = ResponseCode.NO_ERROR;
} else {
resultString = GNSProtocol.BAD_RESPONSE.toString();
responseCode = ResponseCode.BAD_GUID_ERROR;
}
} catch (FailedDBOperationException e) {
resultString = GNSProtocol.BAD_RESPONSE.toString();
responseCode = ResponseCode.DATABASE_OPERATION_ERROR;
} catch (JSONException e) {
resultString = GNSProtocol.BAD_RESPONSE.toString() + " " + GNSProtocol.JSON_PARSE_ERROR.toString() + " " + e.getMessage();
responseCode = ResponseCode.JSON_PARSE_ERROR;
}
return new CommandResponse(responseCode, resultString);
}
use of edu.umass.cs.gnsserver.utils.ValuesMap in project GNS by MobilityFirst.
the class AccountAccess method lookupAccountInfoFromGuid.
/**
* Obtains the account info record for the given guid if that guid was used
* to createField an account.
* <p>
* GUID: "ACCOUNT_INFO" -- {account} for primary guid<br>
* GUID: "GUID" -- guid (primary) for secondary guid<br>
* GUID: "GUID_INFO" -- {guid info}<br>
* HRN: "GUID" -- GUID<br>
* <p>
* GUID = Globally Unique Identifier<br>
* HRN = Human Readable Name<br>
*
* @param guid
* @param handler
* @param allowRemoteLookup
* @return the account info record or null if it could not be found
*/
private static AccountInfo lookupAccountInfoFromGuid(InternalRequestHeader header, String guid, ClientRequestHandlerInterface handler, boolean allowRemoteLookup) {
try {
ValuesMap result = NSFieldAccess.lookupJSONFieldLocalNoAuth(null, guid, ACCOUNT_INFO, handler.getApp(), false);
GNSConfig.getLogger().log(Level.FINE, "ValuesMap for {0} / {1}: {2}", new Object[] { guid, ACCOUNT_INFO, result != null ? result.getSummary() : result });
if (result != null) {
return new AccountInfo(new JSONObject(result.getString(ACCOUNT_INFO)));
}
} catch (FailedDBOperationException | JSONException | ParseException e) {
// Do nothing as this is a normal result when the record doesn't
// exist.
}
GNSConfig.getLogger().log(Level.FINE, "ACCOUNT_INFO NOT FOUND for {0}", guid);
if (allowRemoteLookup) {
GNSConfig.getLogger().log(Level.FINE, "LOOKING REMOTELY for ACCOUNT_INFO for {0}", guid);
String value = null;
try {
value = handler.getInternalClient().execute(GNSCommandInternal.fieldRead(guid, ACCOUNT_INFO, header)).getResultString();
} catch (IOException | JSONException | ClientException e) {
} catch (InternalRequestException e) {
//FIXME: This should do something other than print a stack trace
e.printStackTrace();
}
// exist.
if (value != null) {
try {
return new AccountInfo(new JSONObject(value));
} catch (JSONException | ParseException e) {
// Do nothing as this is a normal result when the record
// doesn't exist.
}
}
}
return null;
}
Aggregations