Created
July 6, 2013 14:45
-
-
Save peschkaj/5940121 to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using Cassandra; | |
using Cassandra.Data; | |
using Cassandra.Data.Linq; | |
using metrics.Core; | |
using metrics.Reporting; | |
using System.Diagnostics; | |
using System.Threading.Tasks; | |
namespace CassandraLoadTest | |
{ | |
public class DatastaxDriverTest | |
{ | |
private readonly Session Session; | |
private readonly int nThreads; | |
private HistogramMetric _readHistogram = metrics.Metrics.Histogram(typeof(DatastaxDriverTest), "Reads"); | |
private HistogramMetric _writeHistogram = metrics.Metrics.Histogram(typeof(DatastaxDriverTest), "Writes"); | |
public DatastaxDriverTest(string[] hosts, string datacenter, long constDelayMS, int queryTimeout, | |
int coreConnectionPerHost, int maxConnectionPerHost, int nThreads) | |
{ | |
Builder cassandraBuilder = Cluster.Builder().AddContactPoints(hosts) | |
.WithLoadBalancingPolicy(new DCAwareRoundRobinPolicy(datacenter)) | |
.WithReconnectionPolicy(new ConstantReconnectionPolicy(constDelayMS)) | |
.WithRetryPolicy(DefaultRetryPolicy.Instance) | |
.WithQueryTimeout(queryTimeout) | |
.WithCompression(CompressionType.NoCompression); | |
cassandraBuilder.PoolingOptions.SetCoreConnectionsPerHost(HostDistance.Local, coreConnectionPerHost); | |
cassandraBuilder.PoolingOptions.SetMaxConnectionsPerHost(HostDistance.Local, maxConnectionPerHost); | |
Cluster cluster = cassandraBuilder.Build(); | |
this.Session = cluster.Connect(); | |
this.nThreads = nThreads; | |
ConsoleReporter consoleReport = new ConsoleReporter(); | |
consoleReport.Start(10, metrics.TimeUnit.Seconds); | |
} | |
public void PlaylistTest() | |
{ | |
string keyspaceName = "playlist"; | |
using (Session) { | |
try | |
{ | |
Session.ChangeKeyspace(keyspaceName); | |
} | |
catch (InvalidQueryException) | |
{ | |
Session.CreateKeyspaceIfNotExists(keyspaceName); | |
Session.ChangeKeyspace(keyspaceName); | |
} | |
var table = Session.GetTable<PlaylistItem>(); | |
table.CreateIfNotExists(); | |
var guid = Guid.NewGuid(); | |
{ | |
var batch = Session.CreateBatch(); | |
var song1 = new PlaylistItem() { | |
PlaylistID = guid, | |
Title = "Some Enchanted Evening", | |
Album = "The Columbia Years (1943-1952) The Complete Recordings", | |
Artist = "Frank Sinatra", | |
SongID = Guid.NewGuid() | |
}; | |
var song2 = new PlaylistItem() { | |
PlaylistID = guid, | |
Title = "Back in the USSR", | |
Album = "The White Album", | |
Artist = "The Beatles", | |
SongID = Guid.NewGuid() | |
}; | |
var song3 = new PlaylistItem() { | |
PlaylistID = guid, | |
Title = "Jumpin' Jack Flash", | |
Album = "Beggars Banquet", | |
Artist = "The Rolling Stones", | |
SongID = Guid.NewGuid() | |
}; | |
batch.Append(table.Insert(song1)); | |
batch.Append(table.Insert(song2)); | |
batch.Append(table.Insert(song3)); | |
batch.Execute(); | |
} | |
// Now we read data back for the entire playlist: | |
var context = new Context(Session); | |
context.AddTable<PlaylistItem>(); | |
var playlist = from i in context.GetTable<PlaylistItem>() | |
where i.PlaylistID == guid | |
select i; | |
foreach(var song in playlist.Execute()) { | |
Console.WriteLine(song.Title + " - " + song.Album + " - " + song.Artist); | |
} | |
table.Delete(); | |
Session.DeleteKeyspaceIfExists(keyspaceName); | |
} | |
} | |
} | |
} |
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
namespace CassandraLoadTest | |
{ | |
public class MainLoadTest | |
{ | |
public static void Main(string[] args) | |
{ | |
string[] hosts = new string[]{"localhost"}; | |
string datacenter= "datacenter1"; | |
long constDelayMS= 60000; | |
int queryTimeout = 5000; | |
int coreConnectionPerHost= 2; | |
int maxConnectionPerHost = 8; | |
int nThreads = 50; | |
var driver = new DatastaxDriverTest(hosts, datacenter, constDelayMS, queryTimeout, coreConnectionPerHost, | |
maxConnectionPerHost, nThreads); | |
driver.PlaylistTest(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment