-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4b2f257
commit 1b7919f
Showing
23 changed files
with
179 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
IO.puts("This benchmark is based on https://github.com/ClickHouse/clickhouse-go#benchmark\n") | ||
|
||
port = String.to_integer(System.get_env("CH_PORT") || "8123") | ||
hostname = System.get_env("CH_HOSTNAME") || "localhost" | ||
scheme = System.get_env("CH_SCHEME") || "http" | ||
database = System.get_env("CH_DATABASE") || "ch_bench" | ||
|
||
{:ok, conn} = Ch.start_link(scheme: scheme, hostname: hostname, port: port) | ||
Ch.query!(conn, "CREATE DATABASE IF NOT EXISTS {$0:Identifier}", [database]) | ||
|
||
Ch.query!(conn, """ | ||
CREATE TABLE IF NOT EXISTS #{database}.benchmark ( | ||
col1 UInt64, | ||
col2 String, | ||
col3 Array(UInt8), | ||
col4 DateTime | ||
) Engine Null | ||
""") | ||
|
||
types = [Ch.Types.u64(), Ch.Types.string(), Ch.Types.array(Ch.Types.u8()), Ch.Types.datetime()] | ||
statement = "INSERT INTO #{database}.benchmark FORMAT RowBinary" | ||
|
||
rows = fn count -> | ||
Enum.map(1..count, fn i -> | ||
[i, "Golang SQL database driver", [1, 2, 3, 4, 5, 6, 7, 8, 9], NaiveDateTime.utc_now()] | ||
end) | ||
end | ||
|
||
alias Ch.RowBinary | ||
|
||
Benchee.run( | ||
%{ | ||
# "control" => fn rows -> Enum.each(rows, fn _row -> :ok end) end, | ||
"encode" => fn rows -> RowBinary.encode_rows(rows, types) end, | ||
"insert" => fn rows -> Ch.query!(conn, statement, rows, types: types) end, | ||
# "control stream" => fn rows -> rows |> Stream.chunk_every(60_000) |> Stream.run() end, | ||
"encode stream" => fn rows -> | ||
rows | ||
|> Stream.chunk_every(60_000) | ||
|> Stream.map(fn chunk -> RowBinary.encode_rows(chunk, types) end) | ||
|> Stream.run() | ||
end, | ||
"insert stream" => fn rows -> | ||
stream = | ||
rows | ||
|> Stream.chunk_every(60_000) | ||
|> Stream.map(fn chunk -> RowBinary.encode_rows(chunk, types) end) | ||
|
||
Ch.query!(conn, statement, stream, encode: false) | ||
end | ||
}, | ||
inputs: %{ | ||
"1_000_000 rows" => rows.(1_000_000) | ||
} | ||
) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
# In-memory INSERT buffer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
# Connecting to multiple nodes | ||
|
||
Similar to https://clickhouse.com/docs/en/integrations/go#connecting-to-multiple-nodes |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# On-disk INSERT buffer | ||
|
||
Here how you could do it | ||
|
||
```elixir | ||
defmodule WriteBuffer do | ||
use GenServer | ||
|
||
# 5 MB | ||
max_buffer_size = 5_000_000 | ||
|
||
def insert(rows) do | ||
row_binary = Ch.RowBinary.encode_many(rows, unquote(encoding_types)) | ||
GenServer.call(__MODULE__, {:buffer, row_binary}) | ||
end | ||
|
||
def init(opts) do | ||
{:ok, fd} = :file.open() | ||
%{fd: fd, buffer_size: 0} | ||
end | ||
|
||
def handle_call({:buffer, row_binary}, _from, state) do | ||
new_buffer_size = state.buffer_size + IO.iodata_length(row_binary) | ||
:file.write(state.fd, row_binary) | ||
|
||
if new_buffer_size < unquote(max_buffer_size) do | ||
%{state | buffer_size: new_buffer_size} | ||
else | ||
flush(state) | ||
end | ||
end | ||
end | ||
``` | ||
|
||
See [tests](../test/ch/on_disk_buffer_test.exs) for more. | ||
|
||
TODO: notes on using it in docker and "surviving" restarts |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,5 @@ | ||
defmodule Ch.Error do | ||
@moduledoc "Error struct wrapping ClickHouse error responses." | ||
defexception [:code, :message] | ||
@type t :: %__MODULE__{code: pos_integer | nil, message: String.t()} | ||
@type t :: %__MODULE__{code: pos_integer | nil, message: binary} | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
defmodule Ch.HTTP do | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
defmodule Ch.Native do | ||
@moduledoc false | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
defmodule Ch.SSL do | ||
@moduledoc false | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.