@@ -32,6 +32,12 @@ macro test_nolog_on_windows(ex...)
3232 end
3333end
3434
35+ # Copied from `@time`.
36+ function count_allocs (f, args... )
37+ stats = @timed f (args... )
38+ return Base. gc_alloc_count (stats. gcstats)
39+ end
40+
3541@testset " LibPQ" begin
3642
3743@testset " ConninfoDisplay" begin
@@ -1612,6 +1618,48 @@ end
16121618 close (conn)
16131619 @test ! isopen (conn)
16141620 end
1621+
1622+ @testset " getindex(::Column) performance" begin
1623+ @testset " $in_val , bin=$bin_fmt " for (
1624+ in_val, out_val, bin_fmt, num_allocs
1625+ ) in [
1626+ (" 5::float8" , 5.0 , true , 0 ),
1627+ (" 5::float4" , 5.0f0 , true , 0 ),
1628+ (" 3::int8" , Int64 (3 ), true , 0 ),
1629+ (" 3::int4" , Int32 (3 ), true , 0 ),
1630+ (" 3::int2" , Int16 (3 ), true , 0 ),
1631+ (" 'hello'::varchar" , " hello" , true , 1 ),
1632+ (" 5::float8" , 5.0 , false , 2 ),
1633+ (" 5::float4" , 5.0f0 , false , 2 ),
1634+ (" 3::int8" , Int64 (3 ), false , 2 ),
1635+ (" 3::int4" , Int32 (3 ), false , 2 ),
1636+ (" 3::int2" , Int16 (3 ), false , 2 ),
1637+ (" 'hello'::varchar" , " hello" , false , 3 ),
1638+ ]
1639+
1640+ # Establish connection and construct temporary table.
1641+ conn = LibPQ. Connection (" dbname=postgres user=$DATABASE_USER " )
1642+
1643+ # Get the column.
1644+ result = execute (
1645+ conn, " SELECT $in_val AS my_column;" ; binary_format= bin_fmt,
1646+ )
1647+ col = columntable (result). my_column
1648+
1649+ # Ensure that element is of expected type and value.
1650+ @test typeof (col[1 ]) == typeof (out_val)
1651+ @test col[1 ] == out_val
1652+
1653+ # Ensure that getting an element from the column produces num_allocs allocs.
1654+ foo (col) = [col[1 ] for _ in 1 : 100 ]
1655+ count_allocs (foo, col)
1656+ max_expected_allocs = num_allocs * 100 + 5
1657+ @test count_allocs (foo, col) < max_expected_allocs
1658+
1659+ close (result)
1660+ close (conn)
1661+ end
1662+ end
16151663 end
16161664
16171665 @testset " Statements" begin
0 commit comments