Skip to content

Commit

Permalink
Improved timing tests.
Browse files Browse the repository at this point in the history
Thanks to Daniel for suggestions in dkuppitz/rexpro-client#6.
  • Loading branch information
zachkinstner committed Jun 7, 2013
1 parent 9a28163 commit 9cb770d
Showing 1 changed file with 100 additions and 24 deletions.
124 changes: 100 additions & 24 deletions Solution/Fabric.Test.Integration/FabInfra/XTiming.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,41 @@
using System.Diagnostics;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
using Fabric.Infrastructure;
using Fabric.Infrastructure.Api;
using Fabric.Infrastructure.Db;
using NUnit.Framework;
using Rexster;
using Weaver;
using Rexster.Messages;
using ServiceStack.Text;

namespace Fabric.Test.Integration.FabInfra {

/*================================================================================================*/
[TestFixture]
public class XTiming : IntegTestBase {

private TcpClientPool vRexProTcpPool;
private const string RexProDataPattern =
@"Result: ""titangraph\[\nlocal: data/FabricTest\]"",";

private const string RexConnDataPattern =
@"{""reqId"":""1234"",""timer"":\d+,""cmdList"":\[{""timer"":\d+,""results"":\[""titangraph\[local:data/FabricTest\]""\]}\]}";

private const string RestApiDataPattern =
@"{""results"":\[""titangraph\[local:data\\/FabricTest\]""\],""success"":true,""version"":""2.4.0-SNAPSHOT"",""queryTime"":\d+.?\d*}";

private string vResults;
private double vTimeSum;


////////////////////////////////////////////////////////////////////////////////////////////////
/*--------------------------------------------------------------------------------------------*/
protected override void TestSetUp() {
vRexProTcpPool = TcpClientPool.GetPool(ApiCtx.RexConnUrl, 8184);
IsReadOnlyTest = true;
}

/*--------------------------------------------------------------------------------------------*/
Expand All @@ -34,6 +48,7 @@ public class XTiming : IntegTestBase {
/*--------------------------------------------------------------------------------------------*/
[Test]
public void RexProSerial() {
RunRexPro(-1);
RunRexPro(-1);
vResults = "";
vTimeSum = 0;
Expand All @@ -47,6 +62,7 @@ public class XTiming : IntegTestBase {
/*--------------------------------------------------------------------------------------------*/
[Test]
public void RexConnSerial() {
RunRexConn(-1);
RunRexConn(-1);
vResults = "";
vTimeSum = 0;
Expand All @@ -59,6 +75,7 @@ public class XTiming : IntegTestBase {
/*--------------------------------------------------------------------------------------------*/
[Test]
public void RestApiSerial() {
RunRestApi(-1);
RunRestApi(-1);
vResults = "";
vTimeSum = 0;
Expand All @@ -72,49 +89,108 @@ public class XTiming : IntegTestBase {
////////////////////////////////////////////////////////////////////////////////////////////////
/*--------------------------------------------------------------------------------------------*/
private void RunRexPro(int pIndex) {
TimedTcpClient tcp = vRexProTcpPool.TakeClient();

var sw = new Stopwatch();
sw.Start();

var rexPro = new RexProClient(() => tcp);
dynamic data = rexPro.Query("g");
var rexPro = new RexProClient("rexster", 8184);
double t0 = sw.Elapsed.TotalMilliseconds;

var sr = new ScriptRequest { Script = "g" };
double t1 = sw.Elapsed.TotalMilliseconds;

ScriptResponse resp = rexPro.ExecuteScript(sr);
double t2 = sw.Elapsed.TotalMilliseconds;

string json = resp.Dump();
sw.Stop();
double t3 = sw.Elapsed.TotalMilliseconds;

vResults += String.Format("RunRexPro:"+
" {0,8:#0.0000}ms,"+
" {1,8:#0.0000}ms,"+
" {2,8:#0.0000}ms,"+
" {3,8:#0.0000}ms\n",
t0, t1, t2, t3
);

vResults += "RunRexPro: "+sw.Elapsed.TotalMilliseconds+"ms\n";
vTimeSum += sw.Elapsed.TotalMilliseconds;

vRexProTcpPool.ReturnClient(tcp);
Log.Debug("DATA: "+data);
Log.Debug("JSON: "+json);
Log.Debug("PATT: "+RexProDataPattern);
//Assert.True(Regex.IsMatch(resp, RexProDataPattern), "Incorrect data.");
}

/*--------------------------------------------------------------------------------------------*/
private void RunRexConn(int pIndex) {
var wq = new WeaverQuery();
wq.FinalizeQuery("g");

var sw = new Stopwatch();
sw.Start();

var data = ApiCtx.DbData("G", wq);
var req = new RexConnTcpRequest { ReqId = "1234" };
var cmd = new RexConnTcpRequestCommand { Cmd = "query", Args = new[] { "g", "" }};
req.CmdList = new List<RexConnTcpRequestCommand>(new[] { cmd });
double t0 = sw.Elapsed.TotalMilliseconds;

string script = JsonSerializer.SerializeToString(req);
byte[] dataLen = BitConverter.GetBytes(IPAddress.HostToNetworkOrder(script.Length));
byte[] data = Encoding.ASCII.GetBytes(script);
double t1 = sw.Elapsed.TotalMilliseconds;

var tcp = new TcpClient("rexster", 8185);
NetworkStream stream = tcp.GetStream();
stream.Write(dataLen, 0, dataLen.Length); //begin with the request's string length
stream.Write(data, 0, data.Length);
double t2 = sw.Elapsed.TotalMilliseconds;

data = new byte[tcp.ReceiveBufferSize];
int bytes = stream.Read(data, 0, data.Length);
string json = Encoding.ASCII.GetString(data, 4, bytes-4);
sw.Stop();
double t3 = sw.Elapsed.TotalMilliseconds;

vResults += String.Format("RunRexConn:"+
" {0,8:#0.0000}ms,"+
" {1,8:#0.0000}ms,"+
" {2,8:#0.0000}ms,"+
" {3,8:#0.0000}ms\n",
t0, t1, t2, t3
);

vResults += "RunRexConn: "+sw.Elapsed.TotalMilliseconds+"ms\n";
vTimeSum += sw.Elapsed.TotalMilliseconds;
Log.Debug("DATA: "+data.ResponseJson);

Log.Debug("JSON: "+json);
Assert.True(Regex.IsMatch(json, RexConnDataPattern), "Incorrect data.");
}

/*--------------------------------------------------------------------------------------------*/
private void RunRestApi(int pIndex) {
var req = HttpWebRequest.Create("http://rexster:8182/graphs/graph/tp/gremlin?script=g");

var sw = new Stopwatch();
sw.Start();

WebResponse data = req.GetResponse();
var req = HttpWebRequest.Create("http://rexster:8182/graphs/graph/tp/gremlin?script=g");
double t0 = sw.Elapsed.TotalMilliseconds;

WebResponse resp = req.GetResponse();
double t1 = sw.Elapsed.TotalMilliseconds;

var sr = new StreamReader(resp.GetResponseStream());
double t2 = sw.Elapsed.TotalMilliseconds;

string json = sr.ReadToEnd();
sw.Stop();
double t3 = sw.Elapsed.TotalMilliseconds;

vResults += String.Format("RunRestApi:"+
" {0,8:#0.0000}ms,"+
" {1,8:#0.0000}ms,"+
" {2,8:#0.0000}ms,"+
" {3,8:#0.0000}ms\n",
t0, t1, t2, t3
);

vResults += "RunRestApi: "+sw.Elapsed.TotalMilliseconds+"ms\n";
vTimeSum += sw.Elapsed.TotalMilliseconds;
var sr = new StreamReader(data.GetResponseStream());
Log.Debug("DATA: "+sr.ReadToEnd());

Log.Debug("JSON: "+json);
Assert.True(Regex.IsMatch(json, RestApiDataPattern), "Incorrect data.");
}

}
Expand Down

0 comments on commit 9cb770d

Please sign in to comment.