public
Created

Cassandra Performance Test

  • Download Gist
cassandra_test-01.cs
C#
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using FluentCassandra;
using FluentCassandra.Types;
 
namespace CassandraTest1
{
class Program
{
private static int count = 10000;
private static int dataLength = 1024;
private static int threadCount = 8;
 
static void Main(string[] args)
{
Task[] tasks = new Task[threadCount];
 
Stopwatch watch = new Stopwatch();
watch.Start();
for (int i = 0; i < threadCount; i++)
{
tasks[i] = Task.Factory.StartNew(DoWork);
}
 
Task.WaitAll(tasks);
watch.Stop();
 
double rate = (count * threadCount) / watch.Elapsed.TotalSeconds;
double throughput = rate * dataLength;
Console.WriteLine("Total Completed: " + watch.Elapsed + "\tRate: " + rate + "\tThroughput: " + throughput);
Console.ReadKey();
}
 
private static void DoWork()
{
using (var db = new CassandraContext("Blog", "localhost"))
{
Stopwatch watch = new Stopwatch();
watch.Start();
 
Random random = new Random();
byte[] data = new byte[dataLength];
random.NextBytes(data);
int errors = 0;
 
for (int i = 0; i < count; i++)
{
// Insert
Guid postId = Guid.NewGuid();
string titleName = i.ToString();
 
var family = db.GetColumnFamily<UTF8Type, UTF8Type>("Posts");
 
var post = family.CreateRecord(postId);
dynamic details = post.CreateSuperColumn();
 
details.Body = data;
 
db.Attach(post);
db.SaveChanges();
}
 
double rate = count / watch.Elapsed.TotalSeconds;
double throughput = rate * data.Length;
Console.WriteLine("Completed: " + watch.Elapsed + "\tRate: " + rate + "\tThroughput: " + throughput + "\tErrors:" + errors);
}
}
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.