<commit_before>require "kemal"
require "pg"
require "pool/connection"
require "html_builder"

# Compose Objects (like Hash) to have a to_json method
require "json/to_json"

DB = ConnectionPool.new(capacity: 25, timeout: 0.01) do
  PG.connect("postgres://benchmarkdbuser:benchmarkdbpass@#{ENV["DBHOST"]? || "127.0.0.1"}/hello_world")
end

class CONTENT
  UTF8 = "; charset=UTF-8"
  JSON = "application/json"
  PLAIN = "text/plain"
  HTML = "text/html" + UTF8
end

ID_MAXIMUM = 10_000

private def randomWorld
  id = rand(1..ID_MAXIMUM)
  result = DB.connection.exec({Int32, Int32}, "SELECT id, randomNumber FROM world WHERE id = $1", [id]).rows.first
  {:id => result[0], :randomNumber => result[1]}
end

private def setWorld(world)
  DB.connection.exec("UPDATE world set randomNumber = $1 where id = $2", [world[:randomNumber], world[:id]])
  world
end

private def fortunes
  data = [] of  Hash(Symbol, (String | Int32))

  DB.connection.exec({Int32, String}, "select id, message from Fortune").rows.each do |row|
    data.push({:id => row[0], :message => row[1]})
  end
  data
end

private def sanitizedQueryCount(request)
  queries = request.params["queries"] as String
  return 1 if queries.empty? || queries.to_i?.nil?
  if queries.to_i > 500
    queries = 500
  elsif queries.to_i < 1
    queries = 1
  end
  queries.to_i
end

before_all do |env|
  env.response.headers["Server"] = "Kemal"<commit_msg>Update DB pools to increase throughput<commit_after>require "kemal"
require "pg"
require "pool/connection"
require "html_builder"

# Compose Objects (like Hash) to have a to_json method
require "json/to_json"

DB = ConnectionPool.new(capacity: 25, timeout: 0.01) do
  PG.connect("postgres://benchmarkdbuser:benchmarkdbpass@#{ENV["DBHOST"]? || "127.0.0.1"}/hello_world")
end

class CONTENT
  UTF8 = "; charset=UTF-8"
  JSON = "application/json"
  PLAIN = "text/plain"
  HTML = "text/html" + UTF8
end

ID_MAXIMUM = 10_000

private def randomWorld
  id = rand(1..ID_MAXIMUM)
  conn = DB.checkout
  result = conn.exec({Int32, Int32}, "SELECT id, randomNumber FROM world WHERE id = $1", [id]).rows.first
  DB.checkin(conn)
  {:id => result[0], :randomNumber => result[1]}
end

private def setWorld(world)
  conn = DB.checkout
  result = conn.exec("UPDATE world set randomNumber = $1 where id = $2", [world[:randomNumber], world[:id]])
  DB.checkin(conn)
  world
end

private def fortunes
  data = [] of  Hash(Symbol, (String | Int32))

  DB.connection.exec({Int32, String}, "select id, message from Fortune").rows.each do |row|
    data.push({:id => row[0], :message => row[1]})
  end
  data
end

private def sanitizedQueryCount(request)
  queries = request.params["queries"] as String
  return 1 if queries.empty? || queries.to_i?.nil?
  if queries.to_i > 500
    queries = 500
  elsif queries.to_i < 1
    queries = 1
  end
  queries.to_i
end

before_all do |env|
  env.response.headers["Server"] = "Kemal"
{'fraction_non_alphanumeric': 0.14398625429553263, 'fraction_numerical': 0.030240549828178694, 'mean_word_length': 4.273550724637682, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, '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': '7380922', 'n_tokens_mistral': 1092, 'n_tokens_neox': 1037, 'n_words': 277}