public
Created

  • Download Gist
benchmark.rb
Ruby
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
require 'bundler'
Bundler.require
 
require 'benchmark'
 
database = CassandraCQL::Database.new('127.0.0.1:9160')
begin
database.execute("CREATE KEYSPACE bm_test WITH strategy_class='SimpleStrategy' AND strategy_options:replication_factor=1")
database.execute("USE bm_test")
database.execute("CREATE COLUMNFAMILY bm_test (KEY uuid PRIMARY KEY) WITH comparator = uuid AND default_validation = text")
 
uuid = CassandraCQL::UUID.new
 
bind = 10_000.times.flat_map { [CassandraCQL::UUID.new, ''] }
bind << uuid
statement = Array.new(10_000) { "? = ?" }.join(', ')
database.execute("UPDATE bm_test SET #{statement} WHERE KEY = ?", *bind)
 
Benchmark.bmbm(40) do |bm|
[1, 10, 100, 1000, 10_000].each do |columns|
bm.report("Array#index: #{columns} columns") do
$use_column_indices = false
database.execute("SELECT FIRST #{columns} * FROM bm_test WHERE KEY = ?", uuid).fetch_row.to_hash
end
 
bm.report("Hash lookup: #{columns} columns") do
$use_column_indices = true
database.execute("SELECT FIRST #{columns} * FROM bm_test WHERE KEY = ?", uuid).fetch_row.to_hash
end
end
end
 
ensure
database.execute("DROP KEYSPACE bm_test")
end
row.rb
Ruby
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 72 73 74 75 76 77 78 79 80 81 82 83
=begin
Copyright 2011 Inside Systems, Inc.
 
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
 
http://www.apache.org/licenses/LICENSE-2.0
 
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
=end
 
module CassandraCQL
class Row
attr_reader :row
def initialize(row, schema)
@row, @schema = row, schema
@value_cache = Hash.new { |h, key|
# If it's a number and not one of our columns, assume it's an index
if key.kind_of?(Fixnum) and !column_names.include?(key)
column_name = column_names[key]
column_index = key
else
column_name = key
column_index = column_index(key)
end
if column_index.nil?
# Cache negative hits
h[column_name] = nil
else
h[column_name] = ColumnFamily.cast(@row.columns[column_index].value, @schema.values[@row.columns[column_index].name])
end
}
end
def [](obj)
@value_cache[obj]
end
 
def column_names
@names ||= @row.columns.map do |column|
ColumnFamily.cast(column.name, @schema.names[column.name])
end
end
 
def column_indices
@column_indices ||= Hash[column_names.each_with_index.to_a]
end
 
def column_index(column_name)
if defined?($use_column_indices) && $use_column_indices
column_indices[column_name]
else
column_names.index(column_name)
end
end
def column_values
column_names.map do |name|
@value_cache[name]
end
end
def columns
@row.columns.size
end
def to_a
column_values
end
# TODO: This should be an ordered hash
def to_hash
Hash[([column_names, column_values]).transpose]
end
end
end

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.