Created
April 19, 2012 15:50
-
-
Save nori3tsu/2421908 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
require 'rubygems' | |
require 'cassandra' | |
require 'benchmark' | |
class Cassandra | |
def batch_insert(column_family, key, hash, batch_size, options = {}) | |
@batch ||= [] | |
insert(column_family, key, hash, options) | |
if batch_size == @batch.size | |
flush(options) | |
end | |
end | |
def flush(options = {}) | |
return if @batch.nil? || @batch.size == 0 | |
compacted_map,seen_clevels = compact_mutations! | |
clevel = if options[:consistency] != nil | |
options[:consistency] | |
elsif seen_clevels.length > 1 | |
raise "Multiple consistency levels used in the batch, and no override...cannot pick one" | |
else | |
seen_clevels.first | |
end | |
_mutate(compacted_map,clevel) | |
ensure | |
@batch = nil | |
end | |
end | |
HOST = '127.0.0.1' | |
PORT = 9160 | |
KEYSPACE = 'MyKeyspace' | |
COLUMN_FAMILY = 'ExampleCF' | |
DATA = { 'name' => 'nori', 'age' => '16' } | |
def create_client | |
Cassandra.new(KEYSPACE, "#{HOST}:#{PORT}", :timeout => 60) | |
end | |
def insert(count, data) | |
client = create_client | |
(1..count).each do |i| | |
client.insert(COLUMN_FAMILY, SimpleUUID::UUID.new.to_s, data) | |
end | |
end | |
def batch(count, data) | |
client = create_client | |
client.batch do | |
(1..count).each do |i| | |
client.insert(COLUMN_FAMILY, SimpleUUID::UUID.new.to_s, data) | |
end | |
end | |
end | |
def batch_insert(count, data) | |
client = create_client | |
(1..count).each do |i| | |
client.batch_insert(COLUMN_FAMILY, SimpleUUID::UUID.new.to_s, data, 300) | |
end | |
client.flush | |
end | |
space = 15 | |
count = 50000 | |
Benchmark.bm(space) do |x| | |
3.times do |i| | |
x.report("insert:#{i}") { | |
insert(count, DATA) | |
} | |
end | |
end | |
Benchmark.bm(space) do |x| | |
3.times do |i| | |
x.report("batch:#{i}") { | |
batch(count, DATA) | |
} | |
end | |
end | |
Benchmark.bm(space) do |x| | |
3.times do |i| | |
x.report("batch_insert:#{i}") { | |
batch_insert(count, DATA) | |
} | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment