# encoding: utf-8

#--
# Copyright DataStax, 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.
#++

require_relative 'benchmark'
require 'cassandra'

class PreparedSelectCqlRb < Benchmark

    def connect_to_cluster
        puts "#{Time.now - start} Connecting to cluster..."
        client = Cassandra::Client::AsynchronousClient.new(hosts: ['127.0.0.1'])
        client.connect.value
        client.use('simplex').value
        @statement = client.prepare('SELECT * FROM songs').value
        at_exit { client.close.value }
    end

    def target
        puts "#{Time.now - start} Starting producing #{@iterations} selects..."
        futures = @iterations.times.map do
            @statement.execute
        end

        puts "#{Time.now - start} Starting consuming selects..."
        futures.each do |future|
            begin
                future.value.size
                increment_success
            rescue => e
                puts "#{e.class.name}: #{e.message}"
                increment_errors
            end
        end
    end
end

PreparedSelectCqlRb.new.run ARGV[0]
