<commit_before>require 'test_helper'
require 'logger'
require 'benchmark'

class ThreadTest < TinyTds::TestCase

  describe 'Threaded SELECT queries' do

    before do
      @logger = Logger.new $stdout
      @logger.level = Logger::WARN
      @poolsize = 4
      @numthreads = 10
      @query = "waitfor delay '00:00:01'"
      @pool = ConnectionPool.new(:size => @poolsize, :timeout => 5) { new_connection }
    end

    after do
      @pool.shutdown { |c| c.close }
    end

    it 'should finish faster in parallel' do
      skip if sqlserver_azure?
      x = Benchmark.realtime do
        threads = []
        @numthreads.times do |i|
          start = Time.new
          threads << Thread.new do
            ts = Time.new
            @pool.with { |c| c.execute(@query).do }
            te = Time.new
            @logger.info "Thread #{i} finished in #{te - ts} thread seconds, #{te - start} real seconds"
          end
        end
        threads.each { |t| t.join }
      end
      assert x < @numthreads, "#{x} is not faster than #{@numthreads} seconds"
      mintime = (1.0*@numthreads/@poolsize).ceil
      @logger.info "#{@numthreads} queries on #{@poolsize} threads: #{x} sec. Minimum time: #{mintime} sec."
      assert x > mintime, "#{x} is not slower than #{mintime} seconds"
    end

    it 'should not crash on error in parallel' do
      threads = []
      @numthreads.times do |i|
        threads << Thread.new do
          @pool.with do |client|
            begin
              result = client.execute "select dbname()"
              result.each { |r| puts r }
            rescue Exception => e
              # We are throwing an error on purpose here since 0.6.1 would
              # segfault on errors thrown in threads
            end
          end
        end
      end<commit_msg>Skup timing/parallel test for Azure.<commit_after>require 'test_helper'
require 'logger'
require 'benchmark'

class ThreadTest < TinyTds::TestCase

  describe 'Threaded SELECT queries' do

    before do
      @logger = Logger.new $stdout
      @logger.level = Logger::WARN
      @poolsize = 4
      @numthreads = 10
      @query = "waitfor delay '00:00:01'"
      @pool = ConnectionPool.new(:size => @poolsize, :timeout => 5) { new_connection }
    end

    after do
      @pool.shutdown { |c| c.close }
    end

    it 'should finish faster in parallel' do
      skip if sqlserver_azure?
      x = Benchmark.realtime do
        threads = []
        @numthreads.times do |i|
          start = Time.new
          threads << Thread.new do
            ts = Time.new
            @pool.with { |c| c.execute(@query).do }
            te = Time.new
            @logger.info "Thread #{i} finished in #{te - ts} thread seconds, #{te - start} real seconds"
          end
        end
        threads.each { |t| t.join }
      end
      assert x < @numthreads, "#{x} is not faster than #{@numthreads} seconds"
      mintime = (1.0*@numthreads/@poolsize).ceil
      @logger.info "#{@numthreads} queries on #{@poolsize} threads: #{x} sec. Minimum time: #{mintime} sec."
      assert x > mintime, "#{x} is not slower than #{mintime} seconds"
    end

    it 'should not crash on error in parallel' do
      skip if sqlserver_azure?
      threads = []
      @numthreads.times do |i|
        threads << Thread.new do
          @pool.with do |client|
            begin
              result = client.execute "select dbname()"
              result.each { |r| puts r }
            rescue Exception => e
              # We are throwing an error on purpose here since 0.6.1 would
              # segfault on errors thrown in threads
            end
          end
        end
      end
{'fraction_non_alphanumeric': 0.1082924168030551, 'fraction_numerical': 0.008183306055646482, 'mean_word_length': 1.9103174603174604, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5406637', 'n_tokens_mistral': 1095, 'n_tokens_neox': 1058, 'n_words': 360}