Skip to main content

Benchmarks

ant next to GORM 1.31, Bun 1.3, ent 0.14 and plain database/sql, doing the same work on one PostgreSQL 17. Plain SQL stands in for sqlc, whose generated code is a query string and a Scan. TestSameQuery in the same module checks that all four libraries return the same rows for the query on Why ant.

Setup​

tools/compare runs the same operations in each library against PostgreSQL 17, on one machine: a Ryzen 5 3600, with the database in Docker on WSL2.

  • Each number is the median of 10 runs. A run repeats the operation for a second, hundreds to thousands of times, and averages it.
  • Every number includes the round trip to the database, which is most of the time on small queries.
  • Commits don't wait for the disk (synchronous_commit=off), so disk speed doesn't drown out the libraries on writes.
  • Plain SQL is the baseline: a hand-written query and Scan, which is what sqlc generates.
  • ant runs with change tracking (its default) and with NoTracking.

Green is the best library in each row.

Time​

µs per operationlower is betterant antNoTrackingGORM Bun ent Plain SQLbaseline
Read 1000 rows2,0751,6923,3612,3702,2401,692
Find one by key442397405384398398
Filter, sort, 10 rows557539568535556535
100 users, 500 posts2,2381,7742,7312,0302,0311,581
Insert one410–4201,112404392
Update one column399–407424402390
Load, edit, save805–8188121,869798

Memory​

KiB per operationlower is betterant antNoTrackingGORM Bun ent Plain SQLbaseline
Read 1000 rows582275392295589257
Find one by key3.83.55.76.34.91.5
Filter, sort, 10 rows11.57.99.79.211.93.8
100 users, 500 posts486241362277335257
Insert one2.5–7.06.83.71.3
Update one column2.5–5.65.03.10.3
Load, edit, save5.8–11.211.210.71.8

Allocations​

Allocations per operationlower is betterant antNoTrackingGORM Bun ent Plain SQLbaseline
Read 1000 rows6,8506,79414,8108,79015,8437,772
Find one by key6752894112636
Filter, sort, 10 rows14611521912430599
100 users, 500 posts5,0113,1449,5586,2188,1524,063
Insert one47–79548623
Update one column27–6114648
Load, edit, save116–1465226944

What the numbers say​

  • Small queries cost about the same in every library.
  • ant keeps a copy of each loaded row so SaveChanges can find changes. That makes 1000 rows 1.2 times slower and twice the memory. With NoTracking, ant reads them as fast as plain SQL.
  • ant uses the least memory of the four libraries in every row. Bun allocates the least on single-row reads and updates.
  • Bun puts insert values into the SQL text, so the driver prepares every insert as a new statement. default_query_exec_mode=simple_protocol in the DSN brings it to 452 µs.
  • ent's update reads the row back in a transaction, so load, edit and save takes four statements instead of two.

Building queries​

tests/benchmarks measures what ant adds with no database involved:

OperationTimeAllocations
Resolving a field pointer15 ns0
Building and rendering a query with two conditions, an order, and a limit3.6 µs22

Budgets in tests/benchmarks/allocs_test.go and tests/benchmarks/budget_test.go fail a test if these grow, or if reading, tracking, saving or inserting rows starts to allocate more for each row.

ant keeps the SQL of the statements it runs most, by their shape: Find, a read with a few plain conditions, and the insert of one entity. A second run with the same shape binds its values to the kept SQL and skips building and rendering. Anything else, such as a join, an Or or In, is built and rendered each time, which takes a few microseconds against a few hundred for the round trip.

Run them yourself​

make db-up DATABASES=postgres # from the ant repo root
cd tools/compare
go test -run '^$' -bench . -benchmem -count 10 | tee new.txt
go run golang.org/x/perf/cmd/benchstat@latest new.txt