Skip to content

Instantly share code, notes, and snippets.

@MikeGoldsmith
Created October 5, 2017 16:26
Show Gist options
  • Save MikeGoldsmith/afbc493c138754dee9eb3b245713bed5 to your computer and use it in GitHub Desktop.
Save MikeGoldsmith/afbc493c138754dee9eb3b245713bed5 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using Couchbase;
using Couchbase.Configuration.Client;
using Couchbase.Core;
using Couchbase.IO;
using Newtonsoft.Json;
namespace ConsoleApp1
{
class Program
{
private const int DataSetSize = 10000;
private static readonly Random Randomizer = new Random();
private static readonly string Json = JsonConvert.SerializeObject(
new
{
firstname = "mike",
lastname = "goldsmith",
dob = new DateTime(1984, 2, 27)
}
);
static void Main()
{
var tokenSource = new CancellationTokenSource();
Console.CancelKeyPress += delegate
{
WriteToOutput("Stopping workload ..");
tokenSource.Cancel();
};
var config = new ClientConfiguration
{
Servers = new List<Uri> {new Uri("couchbase://10.112.170.101")},
UseSsl = true
};
using (var cluster = new Cluster(config))
{
cluster.Authenticate("Administrator", "password");
var bucket = cluster.OpenBucket("default");
var other = cluster.OpenBucket("other");
WriteToOutput("Populating data set ..");
foreach (var index in Enumerable.Range(1, DataSetSize))
{
var key = CreateKey(index);
bucket.Upsert(key, Json);
other.UpsertAsync(key, Json);
}
WriteToOutput("Starting workload ..");
while (!tokenSource.IsCancellationRequested)
{
Task.Factory.StartNew(() => Execute(bucket), tokenSource.Token)
.ContinueWith(task => Execute(other), tokenSource.Token)
.Wait(tokenSource.Token);
}
}
WriteToOutput("Done - press key to close");
Console.ReadKey(true);
}
private static void WriteToOutput(string message)
{
Console.WriteLine($"{DateTime.UtcNow} - {message}");
}
private static string CreateKey(int index)
{
return $"key-{index}";
}
private static Task Execute(IBucket bucket)
{
var key = CreateKey(Randomizer.Next(DataSetSize));
return bucket.GetAsync<dynamic>(key)
.ContinueWith(task =>
{
var result = task.Result;
if (result.Status == ResponseStatus.None)
{
WriteToOutput("Received response code \"None\"");
WriteToOutput(JsonConvert.SerializeObject(result));
}
});
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment