Skip to content

Instantly share code, notes, and snippets.

@dkuppitz
Last active December 18, 2015 05:29
Show Gist options
  • Save dkuppitz/5732817 to your computer and use it in GitHub Desktop.
Save dkuppitz/5732817 to your computer and use it in GitHub Desktop.
RexPro vs REST / MsgPack vs JSON
Avg. time for RexPro (dynamic): 39 ms
Avg. time for RexPro (typed): 42 ms
Avg. time for REST (dynamic): 7 ms
Avg. time for REST (typed): 5 ms
For parallel execution:
Avg. time for RexPro (dynamic): 562 ms
Avg. time for RexPro (typed): MsgPack always crashing
Avg. time for REST (dynamic): 61 ms
Avg. time for REST (typed): 25 ms
namespace ConsoleApplication4
{
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using Newtonsoft.Json;
using Rexster;
internal class Program
{
private static void Main()
{
const int loops = 100;
var rexproMillisDynamic = MeasureRexPro(loops);
var rexproMillisTyped = MeasureRexProTyped(loops);
var restMillisDynamic = MeasureRestDynamic(loops);
var restMillisTyped = MeasureRestTyped(loops);
Console.WriteLine("Avg. time for RexPro (dynamic): {0} ms", rexproMillisDynamic / loops);
Console.WriteLine("Avg. time for RexPro (typed): {0} ms", rexproMillisTyped / loops);
Console.WriteLine("Avg. time for REST (dynamic): {0} ms", restMillisDynamic / loops);
Console.WriteLine("Avg. time for REST (typed): {0} ms", restMillisTyped / loops);
Console.ReadLine();
}
private static long MeasureRestTyped(int loops)
{
var totalMillis = 0L;
#if PARALLEL
Enumerable.Range(0, loops).AsParallel().ForAll(_ =>
#else
while (loops-- > 0)
#endif
{
const string script = "g.v(4)";
var sw = Stopwatch.StartNew();
var json = JsonConvert.SerializeObject(new { script });
var req = WebRequest.Create("http://10.0.0.10:8182/graphs/graph/tp/gremlin");
req.Method = "POST";
req.ContentType = "application/json";
req.ContentLength = json.Length;
using (var writer = new StreamWriter(req.GetRequestStream()))
{
writer.Write(json);
}
var res = req.GetResponse();
using (var reader = new StreamReader(res.GetResponseStream()))
{
var obj = JsonConvert.DeserializeObject<RestResponse<RestTestVertex>>(reader.ReadToEnd());
string message = obj.results.First().message;
sw.Stop();
Debug.Assert(message == "hello world");
}
totalMillis += sw.ElapsedMilliseconds;
#if PARALLEL
});
#else
}
#endif
return totalMillis;
}
private static long MeasureRestDynamic(int loops)
{
var totalMillis = 0L;
#if PARALLEL
Enumerable.Range(0, loops).AsParallel().ForAll(_ =>
#else
while (loops-- > 0)
#endif
{
const string script = "g.v(4)";
var sw = Stopwatch.StartNew();
var json = JsonConvert.SerializeObject(new { script });
var req = WebRequest.Create("http://10.0.0.10:8182/graphs/graph/tp/gremlin");
req.Method = "POST";
req.ContentType = "application/json";
req.ContentLength = json.Length;
using (var writer = new StreamWriter(req.GetRequestStream()))
{
writer.Write(json);
}
var res = req.GetResponse();
using (var reader = new StreamReader(res.GetResponseStream()))
{
dynamic obj = JsonConvert.DeserializeObject(reader.ReadToEnd());
string message = obj.results[0].message;
sw.Stop();
Debug.Assert(message == "hello world");
}
totalMillis += sw.ElapsedMilliseconds;
#if PARALLEL
});
#else
}
#endif
return totalMillis;
}
private static long MeasureRexProTyped(int loops)
{
var totalMillis = 0L;
var client = new RexProClient("10.0.0.10");
#if PARALLEL
Enumerable.Range(0, loops).AsParallel().ForAll(_ =>
#else
while (loops-- > 0)
#endif
{
var sw = Stopwatch.StartNew();
var obj = client.Query<Vertex<TestVertex>>("g.v(4)");
string message = obj.Data.message;
sw.Stop();
Debug.Assert(message == "hello world");
totalMillis += sw.ElapsedMilliseconds;
#if PARALLEL
});
#else
}
#endif
return totalMillis;
}
private static long MeasureRexPro(int loops)
{
var totalMillis = 0L;
var client = new RexProClient("10.0.0.10");
#if PARALLEL
Enumerable.Range(0, loops).AsParallel().ForAll(_ =>
#else
while (loops-- > 0)
#endif
{
var sw = Stopwatch.StartNew();
var obj = client.Query("g.v(4)");
string message = obj.Data.message;
sw.Stop();
Debug.Assert(message == "hello world");
totalMillis += sw.ElapsedMilliseconds;
#if PARALLEL
});
#else
}
#endif
return totalMillis;
}
}
public class TestVertex
{
public string message;
}
public abstract class RestVertex
{
public long _id;
public string _type;
}
public class RestTestVertex : RestVertex
{
public string message;
}
public class RestResponse<T>
{
public IEnumerable<T> results;
public bool success;
public string version;
public double queryTime;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment