Skip to content

[performance] Benchmarks for typed read, queryAll, POJO insert and compressed decode - #3517

Open
utvaxling wants to merge 7 commits into
ClickHouse:mainfrom
utvaxling:perf/benchmarks
Open

utvaxling wants to merge 7 commits into
ClickHouse:mainfrom
utvaxling:perf/benchmarks

Conversation

@utvaxling

@utvaxling utvaxling commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR adds JMH benchmarks to the performance module for four paths of client V2 that the suite did not measure: a read through the typed getters, Client.queryAll, the insert of a list of POJOs, and the decode of a compressed response together with its values. Issue #3516 has the reason.

The PR adds five files. It does not change an existing class, a build file or a library.

Closes #3516

The benchmarks

Benchmark What it measures
TypedRead.queryV2 A query, and each value taken one time with the getter of its type (getLong, getDouble, getString and so on), never with readValue.
TypedRead.decodeV2 The same read from memory, without the network and the server.
QueryAll.recordsV2 Client.queryAll(sql): the whole result as a list of GenericRecord.
QueryAll.pojosV2 Client.queryAll(sql, class, schema): the whole result as a list of POJOs.
PojoInsert.insertV2 Client.insert(table, list) with a list of POJOs.
CompressedRead.decodeV2 The decode of an LZ4 block-compressed response from memory: CompressedBlockInputStream and the value decoding of the reader.

DataSetPojo (in com.clickhouse.benchmark.data) is a utility for POJO benchmarks. It makes the POJO class of a table at run time with ASM: a field, a getter and a setter for each column, with a primitive property for a number that is not nullable. A benchmark registers DataSetPojo.of(schema).type() in the client. No class has to be written for a dataset, and the POJO benchmarks run with each dataset of the suite.

How the benchmarks get and check their data

  • A benchmark that needs data gets it through a client instance of its own state. No benchmark opens a connection of its own.
  • CompressedRead reads the response through a client and compresses it in memory with CompressedBlockOutputStream, the stream that the client uses for its requests, in blocks of 1 MiB. The server sends a compressed response in blocks of 1 MiB too.
  • TypedRead.decodeV2 reads from the array stream of ClickHouseInputStream. That stream has no lock. The read methods of java.io.ByteArrayInputStream are synchronized, and the reader calls them for each value.
  • QueryAll.pojosV2 proves one time for each iteration that the client set every property of the POJO. The client only logs a column that has no setter.
  • PojoInsert.insertV2 counts the rows of the table after each insert. After the first insert it also compares the content of the table with the content of the source table (sum(cityHash64(*))). Then it empties the table. It does this with one client that it keeps for the iteration.
  • Each reader, stream and response is closed with try-with-resources. The clients of the states are closed in the teardown, and in the setup when the setup fails.

How to run them

Build the module as its README says. BenchmarkRunner has no -b flag for the new classes (see "Not in this PR"), so start JMH with the name:

java -cp performance/target/jmh-benchmark-0.1-SNAPSHOT-jar-with-dependencies.jar org.openjdk.jmh.Main \
  'com\.clickhouse\.benchmark\.clients\.(TypedRead|QueryAll|PojoInsert|CompressedRead)\.' \
  -p datasetSourceName=file://<dataset.csv> -p limit=100000 -bm sample -tu ms -prof gc

Each benchmark also runs alone, for example with TypedRead\.decodeV2$.

Results on main

Libraries of main at 1b33bf6d, a dataset of 100,000 rows made from sample_dataset.sql (10 Float64, 10 String and 10 Int64 columns), JMH 1.37, JDK 17.0.21, heap 2 GiB, 2 forks, 3 warm-up iterations of 5 s and 5 iterations of 10 s, ClickHouse 26.8 in a container on the same machine. One run. Each run had no error line in the output of the benchmark.

Benchmark Mean time for each operation Allocation for each operation
TypedRead.queryV2 101.4 ms 153.42 MB
TypedRead.decodeV2 65.2 ms 153.38 MB
QueryAll.recordsV2 378.2 ms 397.32 MB
QueryAll.pojosV2 87.6 ms 88.27 MB
PojoInsert.insertV2 132.5 ms 168.06 MB
CompressedRead.decodeV2 79.4 ms 147.67 MB

For comparison, a short run of existing benchmarks with the same jar gives 107.7 MB for QueryClient.queryV2 and 78.5 MB for InsertClient.insertV2RowBinary.

With the time-series dataset of my measurements (DateTime, DateTime64, Date, UInt64 and other columns) the benchmarks TypedRead, QueryAll and PojoInsert run with no error line too.

Two notes for the use of the numbers:

  • The allocation of each benchmark is the same in each fork, to 0.1 MB. In one earlier fork QueryAll.pojosV2 had 1.5 MB less.
  • The time of TypedRead.decodeV2 is constant in a fork but not between forks: with the libraries of 356a218e, 9 forks of one jar gave 67.8 to 74.0 ms. Compare its time with 5 forks or more.

What the benchmarks show

Profiles with async-profiler, taken on 356a218e (an older commit of main). The allocation of these benchmarks is the same there and on 1b33bf6d, but for TypedRead, where #3473 removed 84 MB.

  • Client.queryAll(sql) fills a LinkedHashMap for each row, and MapBackedRecord copies it into a HashMap. The two maps are 74% of the allocation. The records stay alive until the call ends, and the pauses of the garbage collector are about 45% of the time. The call takes 4.3 times the time of queryAll into POJOs.
  • TypedRead: the getters of numbers make a box for each value (45.6 MB), and readValue has put a box into the record before (46.0 MB).
  • PojoInsert: 55% of the allocation comes from reflection: an empty varargs array for each Method.invoke, and a box for each number. 31% of the CPU samples are in ChunkedOutputStream.write, because each value is written into the chunked stream of the HTTP client by itself.
  • QueryAll.pojosV2: 15% of the allocation is getDeclaredConstructor() and newInstance() for each row.

Not in this PR

  • Flags in BenchmarkRunner and lines in the README for the new classes. They change existing files. I can add them here or in a follow-up, as you prefer.
  • A dependency on ASM in performance/pom.xml. DataSetPojo uses org.objectweb.asm. The module gets it through client-v2 (org.ow2.asm:asm), so the build works without a change. If you want the dependency declared in the module, it is one entry.
  • A fix for the rows that the datasets keep in memory. I found this while I wrote PojoInsert: BenchmarkBase.loadClickHouseRecords adds each record of response.records() of client V1 to a list, and client V1 uses one record object again for each row. So getRowsOrdered(), getRows() and getClickHouseRecords() hold the same row as many times as the table has rows. The RowBinary inserts of InsertClient, ConcurrentInsertClient and MixedWorkload, Serializers and JDBCInsert write that one row. Their checks count the rows, so they pass. Measured with PojoInsert.insertV2 on 356a218e: 106 ms for the one row 100,000 times, 144 ms for 100,000 different rows, with the same allocation. PojoInsert does not use these rows: it reads the rows of the filled table one time as POJOs. A fix changes the numbers of the existing insert benchmarks, so I did not put it into this PR.

Tests

The change is benchmark code, so there is no unit test. Each benchmark was run with the stock dataset, and TypedRead, QueryAll and PojoInsert also with the time-series dataset, with the libraries of 356a218e and of 1b33bf6d. The branch has no conflict with main.

No entry for the CHANGELOG: the change has no effect on the libraries.

Checklist

🤖 Generated with Claude Code


Note

Low Risk
Additive benchmark-only code in the performance module; no production client or API behavior changes.

Overview
Adds five new files to the performance module—no changes to library code, build configs, or existing benchmarks. Coverage targets client V2 paths that were missing from the suite (closes #3516).

DataSetPojo generates table-shaped POJO classes at runtime with ASM (getters/setters per column, cached by schema) so POJO benchmarks work across datasets without hand-written models; checkFilled validates that reads actually populated every property.

TypedRead measures full-result reads using per-type getters (getLong, getString, etc.) instead of readValue, with live query vs in-memory decode variants.

QueryAll benchmarks whole-result materialization as GenericRecord lists vs registered POJO lists.

PojoInsert benchmarks Client.insert(table, list); each invocation is verified (row count, content hash on first run) and the table is truncated for the next iteration.

CompressedRead benchmarks LZ4 block decompression plus row decoding from pre-compressed in-memory payloads (1 MiB blocks), isolating decode cost from network/server.

Reviewed by Cursor Bugbot for commit eebf5d4. Bugbot is set up for automated code reviews on this repo. Configure here.

utvaxling and others added 7 commits November 30, 2026 22:32
…sponse

The performance module has a benchmark that decodes the values of a
response that is not compressed (Deserializers.DeserializerOutputStreamV2)
and one that reads an LZ4 block-compressed response in large pieces
without decoding (Compression.queryV2NativeLz4). No benchmark covers
the path between them, where the binary reader asks the decompressing
stream for 1 to 8 bytes at a time.

CompressedRead.decodeV2 reads all rows of a compressed response from
memory with client V2: CompressedBlockInputStream and the decoding of
each value, without the network and the server. The bytes are the body
that the server sends for compress=1; the benchmark gets them one time
with a plain HTTP request.

The benchmark is a new class. It runs alone by name, has no parameter
of its own and does not catch exceptions. No existing class is changed.

First numbers on JDK 17 (100,000 rows of 30 columns, 2 runs of 2 forks):
79.5 ms and 147.7 MB for each operation in both runs. In the profile,
CompressedBlockInputStream.read has 38% of the CPU samples (HeapByteBuffer.get
10%), the refill of a block 16% (the checksum 9%).

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
New class, no existing class is changed. It reads all rows with client V2
and takes each value one time, with the getter of its type (getLong,
getDouble, getString and so on), never with readValue.

- TypedRead.queryV2 reads the response of a query.
- TypedRead.decodeV2 reads the same response from memory, through a stream
  that has no lock.

QueryClient.queryV2 takes each value as an object and
QueryClient.queryV2WithTypes takes each value two times; this is the case
between them. The getter of each column is chosen one time for a response.
The class has getters for the types of the stock dataset and of the
time-series dataset.

Measured with JMH on JDK 17.0.21, 100,000 rows for each operation, runs A
and B of 2 forks each, same jar:
- stock dataset (10 Float64, 10 String, 10 Int64 columns):
  queryV2 115.1 and 116.9 ms, 237.34 MB for each operation;
  decodeV2 85.8 and 78.9 ms, 237.30 MB.
- time-series dataset: queryV2 53.0 and 50.4 ms, 95.42 MB;
  decodeV2 38.2 and 39.4 ms, 95.39 MB.
The allocation is the same in every fork. The time of decodeV2 on the stock
dataset is stable inside a fork and differs between forks: 9 forks gave
77.8 to 86.6 ms. Compare its time with 5 forks or more.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
…POJOs

Two new classes, no existing class is changed.

QueryAll reads a whole result into a list with Client.queryAll of client
V2, one object for each row:
- QueryAll.recordsV2 gets a list of GenericRecord;
- QueryAll.pojosV2 gets a list of POJOs, which the client fills through
  the setters of a registered class.

SampleRow is the POJO: a row of a dataset made from sample_dataset.sql
(10 Float64, 10 String and 10 Int64 columns). It checks that the table has
its columns, and the benchmark proves one time for each iteration that the
client sets every property: the client only logs a column that has no
setter.

Measured with JMH on JDK 17.0.21, heap 2 GiB, runs A and B of 2 forks
each, same jar:
- 100,000 rows for each operation: recordsV2 401.6 and 394.3 ms, 397.3 MB
  for each operation; pojosV2 89.7 and 90.2 ms, 88.3 MB (one fork of four:
  86.7 MB). For comparison, QueryClient.queryV2 reads the same rows without
  keeping them in 84 ms and 107.6 MB.
- 10 rows for each operation, one run: recordsV2 2.63 ms and 88.2 KB;
  pojosV2 2.41 ms and 52.1 KB; QueryClient.queryV2 2.41 ms and 52.5 KB.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
New class PojoInsert, no existing benchmark class is changed.
PojoInsert.insertV2 inserts a list of POJOs with Client.insert(table, list)
of client V2: the client takes each value from the getter of a registered
class and writes it in format RowBinary.

After each insert the state of the benchmark counts the rows of the table
and empties it with its own client, so that no new client is made for each
check.

SampleRow (the POJO of the QueryAll benchmark) gets a method that makes a
row from the values of a dataset row.

Measured with JMH on JDK 17.0.21, heap 2 GiB, 2 forks for each run, with
InsertClient.insertV2RowBinary in the same call:
- 100,000 rows for each insert, runs A and B: insertV2 105.4 and 104.3 ms,
  168.05 MB for each insert in every fork; InsertClient.insertV2RowBinary
  104.0 and 104.6 ms, 78.44 MB.
- 10 rows for each insert, one run: insertV2 56.6 ms and 129.6 KB;
  InsertClient.insertV2RowBinary 59.8 ms and 2.06 MB, most of which is the
  new clients of its check after each insert.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
DataSetPojo is a new utility of the benchmarks. It makes the POJO class of
a table at run time with ASM: a public class with a field, a getter and a
setter for each column, with a primitive type for a number that is not
nullable. Tables with the same columns get the same class. It also proves
that a read has set every property (the client only logs a column that has
no setter). Any benchmark that reads or inserts POJOs can use it.

QueryAll.pojosV2 and PojoInsert.insertV2 use it in place of SampleRow, the
class that was written by hand for one dataset. SampleRow is removed. Both
benchmarks now also run with the time-series dataset.

PojoInsert.insertV2 now inserts the rows of the filled table, read one
time as POJOs, and proves after the first insert that the table has the
content of the filled table. Before, it took the rows that the dataset
keeps in memory. Those are the same row as many times as the table has
rows: BenchmarkBase.loadClickHouseRecords keeps the one record object that
client V1 uses again for each row. That method is not changed here.

ASM comes with the client (org.ow2.asm:asm); the build file of the module
is not changed.

Measured with JMH on JDK 17.0.21, heap 2 GiB, 100,000 rows for each
operation, 2 forks for each run, this commit and the commit before it in
one session:
- QueryAll.pojosV2: 89.3 ms before, 89.8 and 88.4 ms now, 88.27 MB for
  each operation before and now. The generated class costs nothing.
- PojoInsert.insertV2: 106.0 ms before, 143.8 and 142.6 ms now, 168.06 MB
  before and 168.07 MB now. The client does the same work; the server
  takes longer for 100,000 different rows than for one row 100,000 times.
- time-series dataset, one run: QueryAll.pojosV2 42.2 ms and 71.46 MB;
  QueryAll.recordsV2 86.4 ms and 182.86 MB; PojoInsert.insertV2 119.3 ms
  and 63.48 MB.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
CompressedRead read the compressed body of a response with
HttpURLConnection. It now reads the response through a client of its own
state and compresses it in memory with CompressedBlockOutputStream, the
stream that the client uses for its requests, in blocks of 1 MiB as the
server sends them. No benchmark of this branch opens a connection of its
own.

TypedRead.decodeV2 read from a stream class of its own. It now reads from
the array stream of ClickHouseInputStream, which has no lock either. The
class of the benchmark is removed.

Measured with JMH on JDK 17.0.21, heap 2 GiB, 100,000 rows of the stock
dataset for each operation, this commit and the commit before it in one
session:
- CompressedRead.decodeV2: 77.7 ms before (2 forks), 79.5 and 79.6 ms now
  (runs A and B), 147.67 MB for each operation before and now. The blocks
  that the client's compressor makes take 2.3% longer to decode than the
  blocks of the server.
- TypedRead.decodeV2: 83.0 ms before (2 forks), 67.8 to 74.0 ms now
  (9 forks), 237.30 MB before and now. Its time differs between forks as
  before; compare it with 5 forks or more.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
- CompressedRead.decodeV2, TypedRead.queryV2 and TypedRead.decodeV2 made a
  reader for each operation and did not close it. The reader and its
  stream are now in a try-with-resources block; for a query the reader is
  closed before its response, as ResultSetImpl of jdbc-v2 does.
- The state of QueryAll and of PojoInsert makes a client for each
  iteration and closes it in the teardown. When the setup fails after the
  client was made, the teardown does not run: the setup now closes the
  client before it gives the exception on.

Measured with JMH on JDK 17.0.21, 100,000 rows for each operation, 2
forks, against the commit before: CompressedRead.decodeV2 79.5 -> 79.2 ms
and 147.67 MB both; TypedRead.decodeV2 71.3 -> 71.4 ms and 237.30 MB both.
The close calls do not change the numbers.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
@utvaxling
utvaxling requested review from a team and mzitnik as code owners December 3, 2026 21:26
@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown

AI review gate — Human approval in place of AI review

Approved on the head commit by chernser (write access, not the author), standing in for the AI review:

  • Cursor Bugbot is still reviewing this commit.

What to do:

  • Nothing to do yet if the review is running. The gate turns green when it finishes clean.
  • No review showing up? Post bugbot run as a new top-level PR comment. If that does nothing, Bugbot is probably not enabled for this repository: ask a repository administrator.

Evaluated at eebf5d44. This required workflow does not re-run on its own once the AI review later concludes — check the Cursor Bugbot check on this commit directly for its current state.

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes using high effort and found 1 potential issue.

Fix All in Cursor

Reviewed by Cursor Bugbot for commit eebf5d4. Configure here.

}
if (!hasValue) {
throw new IllegalStateException("The property of column " + columns[i] + " has no value in "
+ rows.size() + " rows: the client did not call its setter");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fill check rejects valid empty values

Medium Severity

checkFilled treats null, false, and numeric zero as proof a setter never ran. Those are valid column values, so a nullable, flag, or counter column whose sampled rows are all empty aborts setup even though every setter ran. QueryAll and PojoInsert only sample 100 rows, so zeros at the start of a column fail the same way.

Additional Locations (2)
Fix in Cursor Fix in Web

Reviewed by Cursor Bugbot for commit eebf5d4. Configure here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@utvaxling please correct

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[performance][benchmark] Typed read, queryAll, POJO insert and compressed decode

2 participants