Created
July 25, 2014 20:06
-
-
Save kevin-montrose/cc614baa67e066696352 to your computer and use it in GitHub Desktop.
Disabling Pipeline penalty
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
const string TypeUrl = "your-elastic-type-url-here"; | |
const int NumRequests = 100000; | |
void Main() | |
{ | |
var randomIds = Enumerable.Range(0, NumRequests).Select(i => Guid.NewGuid()).ToList(); | |
var pipelined = ProfileRequests(randomIds, Pipelined); | |
var nonPipelined = ProfileRequests(randomIds, NonPipelined); | |
pipelined.Dump("Pipelined"); | |
nonPipelined.Dump("NonPipelined"); | |
} | |
private static WebRequest CreateHttpWebRequest(string url, bool pipelined) | |
{ | |
var ret = (HttpWebRequest)HttpWebRequest.Create(url); | |
ret.Pipelined = pipelined; | |
return ret; | |
} | |
private static string ReadStream(Stream str) | |
{ | |
using (var reader = new StreamReader(str)) | |
{ | |
return reader.ReadToEnd(); | |
} | |
} | |
private static Task<string> GetAsync(string url, bool pipelined) | |
{ | |
var tcs = new TaskCompletionSource<string>(); | |
var request = CreateHttpWebRequest(url, pipelined); | |
var state = Guid.NewGuid(); | |
request.BeginGetResponse( | |
asyncRes => | |
{ | |
// not the response we're looking for | |
if (!(asyncRes.AsyncState is Guid) || ((Guid)asyncRes.AsyncState) != state) return; | |
try | |
{ | |
using (var resp = request.EndGetResponse(asyncRes)) | |
using (var stream = resp.GetResponseStream()) | |
{ | |
var txt = ReadStream(stream); | |
tcs.TrySetResult(txt); | |
} | |
} | |
catch (Exception) | |
{ | |
tcs.TrySetResult(null); | |
} | |
}, | |
state | |
); | |
return tcs.Task; | |
} | |
static async Task<string> Pipelined(Guid id) | |
{ | |
return await GetAsync(TypeUrl + id, true); | |
} | |
static async Task<string> NonPipelined(Guid id) | |
{ | |
return await GetAsync(TypeUrl + id, false); | |
} | |
static IEnumerable<List<T>> Batch<T>(IEnumerable<T> e, int batchSize) | |
{ | |
var nextRet = new List<T>(); | |
foreach(var item in e) | |
{ | |
nextRet.Add(item); | |
if(nextRet.Count == batchSize) | |
{ | |
yield return nextRet; | |
nextRet = new List<T>(); | |
} | |
} | |
if(nextRet.Count != 0) | |
{ | |
yield return nextRet; | |
} | |
} | |
static TimeSpan ProfileRequests(List<Guid> ids, Func<Guid, Task<string>> profile) | |
{ | |
var watch = new Stopwatch(); | |
watch.Start(); | |
foreach(var idBatch in Batch(ids, 100)) | |
{ | |
var tasks = idBatch.Select(id => profile(id)).ToArray(); | |
Task.WaitAll(tasks); | |
} | |
watch.Stop(); | |
return watch.Elapsed; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment