Guest User

Untitled

a guest
Jan 23rd, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.61 KB | None | 0 0
  1. require 'cassandra-cql'
  2. require 'simple_uuid'
  3. require 'cassandra/0.8'
  4. require 'benchmark'
  5. require 'forgery'
  6.  
  7. def setup
  8. @cassandra_cql = CassandraCQL::Database.new('127.0.0.1:9160')
  9.  
  10. begin
  11. @cassandra_cql.execute("DROP KEYSPACE Testing")
  12. rescue CassandraCQL::Error::InvalidRequestException => ex
  13. end
  14.  
  15. @cassandra_cql.execute("CREATE KEYSPACE Testing WITH strategy_class='org.apache.cassandra.locator.SimpleStrategy' AND strategy_options:replication_factor=1")
  16. @cassandra_cql.execute("use Testing")
  17.  
  18. begin
  19. @cassandra_cql.execute 'DROP COLUMNFAMILY wide_row_thrift'
  20. rescue CassandraCQL::Error::InvalidRequestException => ex
  21. end
  22.  
  23. begin
  24. @cassandra_cql.execute 'DROP COLUMNFAMILY wide_row_cql'
  25. rescue CassandraCQL::Error::InvalidRequestException => ex
  26. end
  27.  
  28. @cassandra_cql.execute "CREATE COLUMNFAMILY wide_row_cql (id uuid PRIMARY KEY)"
  29. @cassandra_cql.execute "CREATE COLUMNFAMILY wide_row_thrift (id uuid PRIMARY KEY)"
  30.  
  31. begin
  32. @cassandra_cql.execute 'DROP COLUMNFAMILY narrow_row_thrift'
  33. rescue CassandraCQL::Error::InvalidRequestException => ex
  34. end
  35.  
  36. begin
  37. @cassandra_cql.execute 'DROP COLUMNFAMILY narrow_row_cql'
  38. rescue CassandraCQL::Error::InvalidRequestException => ex
  39. end
  40.  
  41. @cassandra_cql.execute "CREATE COLUMNFAMILY narrow_row_cql (id uuid PRIMARY KEY)"
  42. @cassandra_cql.execute "CREATE COLUMNFAMILY narrow_row_thrift (id uuid PRIMARY KEY)"
  43. setup_connections
  44. end
  45.  
  46. def setup_connections
  47. @cassandra_thrift = Cassandra.new('Testing', "127.0.0.1:9160")
  48. @cassandra_cql = CassandraCQL::Database.new('127.0.0.1:9160', :keyspace => 'Testing')
  49. end
  50.  
  51. def insert_narrow_row_cql(count = 1000)
  52. count.times do
  53. @cassandra_cql.execute("INSERT INTO narrow_row_cql (id, email, password, first_name, last_name) VALUES (?, ?, ?, ?, ?)",
  54. CassandraCQL::UUID.new,
  55. Forgery(:internet).email_address,
  56. Forgery(:basic).password,
  57. Forgery(:name).first_name,
  58. Forgery(:name).last_name,
  59. )
  60. end
  61. end
  62.  
  63. def insert_narrow_row_thrift(count = 1000)
  64. count.times do
  65. @cassandra_thrift.insert(:narrow_row_thrift, SimpleUUID::UUID.new.to_s,
  66. { 'email' => Forgery(:internet).email_address,
  67. 'password' => Forgery(:basic).password,
  68. 'first_name' => Forgery(:name).first_name,
  69. 'last_name' => Forgery(:name).last_name
  70. }
  71. )
  72. end
  73. end
  74.  
  75. def insert_wide_row_cql(row_count, column_count)
  76. row_count.times do
  77. columns = [CassandraCQL::UUID.new]
  78.  
  79. cql = "INSERT INTO wide_row_cql (id"
  80. column_count.times do |index|
  81. cql += ",'column_#{index.to_s.rjust(4,'0')}'"
  82. columns << Forgery(:basic).text
  83. end
  84.  
  85. cql += ") VALUES (?#{',?' * column_count})"
  86. @cassandra_cql.execute(cql, *columns)
  87. end
  88. end
  89.  
  90. def insert_wide_row_thrift(row_count, column_count)
  91. row_count.times do
  92. columns = {}
  93. column_count.times do |index|
  94. columns['column_' + index.to_s.rjust(4,'0')] = Forgery(:basic).text
  95. end
  96.  
  97. @cassandra_thrift.insert(:wide_row_thrift, SimpleUUID::UUID.new.to_s, columns)
  98. end
  99. end
  100.  
  101. def read_wide_row_cql
  102. rows_read = 0
  103. @cassandra_cql.execute("SELECT * FROM wide_row_cql").fetch{|row| rows_read += 1}
  104. end
  105.  
  106. def read_wide_row_thrift
  107. rows_read = 0
  108. @cassandra_thrift.each(:wide_row_thrift, :count => 1000) {|row| rows_read += 1}
  109. end
  110.  
  111. def read_narrow_row_cql
  112. rows_read = 0
  113. @cassandra_cql.execute("SELECT * FROM narrow_row_cql").fetch{|row| rows_read += 1}
  114. end
  115.  
  116. def read_narrow_row_thrift
  117. rows_read = 0
  118. @cassandra_thrift.each(:narrow_row_thrift) {|row| rows_read += 1}
  119. end
  120.  
  121. def benchmark_inserts
  122. Benchmark.bm(35)do|x|
  123. x.report("CQL Insert 100 rows 1000 cols:") {insert_wide_row_cql(100, 1000) }
  124. x.report("Thrift Insert 100 rows 1000 cols:") {insert_wide_row_thrift(100, 1000) }
  125. x.report("CQL Insert 1000 rows 5 cols: ") {insert_narrow_row_cql(1000) }
  126. x.report("Thrift Insert 1000 rows 5 cols: ") {insert_narrow_row_thrift(1000) }
  127. end
  128. end
  129.  
  130. def benchmark_reads
  131. Benchmark.bm(35)do|x|
  132. x.report("CQL Read 100 rows 1000 cols:") {read_wide_row_cql }
  133. x.report("Thrift 100 rows 1000 cols:") {read_wide_row_thrift }
  134. x.report("CQL Read 1000 rows 5 cols: ") {read_narrow_row_cql }
  135. x.report("Thrift 1000 rows 5 cols: ") {read_narrow_row_thrift }
  136. end
  137. end
  138.  
  139. if __FILE__ == $0
  140. setup
  141. setup_connections
  142. benchmark_inserts
  143. benchmark_reads
  144.  
  145. #require 'perftools'
  146. #PerfTools::CpuProfiler.start("/tmp/read_wide_row_cql") do
  147. # read_wide_row_cql
  148. #end
  149. #
  150. #require 'rbtrace'
  151. #while true
  152. # read_wide_row_cql
  153. #end
  154. end
Add Comment
Please, Sign In to add comment