Files
five/_FiveSql2/test/bench_sql.prg
CharlesKWON dd270d5d9d perf: RTL Go-native migration — 27 optimizations, DML up to 70-90x
Systematic pass through PRG hot paths, promoting them to Go RTL while
preserving Harbour/FiveSql2 semantics. Full log in
docs/RTL-Go-Native-Migration.md.

Bench (bench_sql) vs 2026-04-08 baseline
 - B1  SELECT *             2,192 → 114   µs   (19x)
 - B6  INNER JOIN           9,291 → 233   µs   (40x)
 - B7  CTE simple           8,037 → 129   µs   (62x)
 - B9  ROW_NUMBER           3,705 → 265   µs   (14x)
 - B10 RANK PARTITION       4,748 → 309   µs   (15x)
 - B12 INSERT (WA cache)    4,319 →  63   µs   (69x)
 - B13 UPDATE (WA cache)    6,144 →  68   µs   (90x)
 - B15 CTE+WIN+JOIN        18,395 → 1,873 µs   (10x)

Infrastructure
 - HbHash O(1) Index preserving insertion order (Harbour KEEPORDER)
 - HbDeepClone Go RTL (scalar-sharing, immutable hash keys)
 - MEMRDD auto-imported via gengo; all Five programs get mem:name driver
 - SQL plan + pcode caches (s_hPlanCache, s_hDmlPcodeCache)
 - Opt-in SqlWACacheEnable — dbUseArea/Close/Commit batched for DML

SQL engine
 - FiveSql2 lexer ported to Go (byte FSM) with combined automatic
   template parameterization (literals → ?, concat queries share plan)
 - Go RTL: SqlDistinct, SqlGroupRows, SqlWindowPartitions,
   SqlWindowSortPartition, SqlWindowAssignRank, SqlComputeAggSimple,
   SqlBulkInsert, SqlBulkUpdate, SqlExprHasAgg, SqlEvalHaving
 - CTE / subquery / driving-table materialize paths use MEMRDD
 - SqlCoerce/SqlCmp/SqlIsTrue helpers moved from PRG to Go
 - SqlBulkUpdate defers Flush when WA cache active (APFS fsync was
   dominant B13 cost — 1.6ms/call → gone)

Correctness fixes uncovered during migration
 - ASort default path now sorts dates/logicals/timestamps (was no-op)
 - ORDER BY default NULL placement matches PRG SqlRowCompare across
   Go fast path; explicit NULLS FIRST/LAST honored by both paths
 - SqlBulkUpdate respects EXCLUSIVE vs SHARED mode record locks
 - SqlCmp/SqlCmpEq normalize NumInt vs Double (caught by test 6b)

Verification
 - go test ./...              ALL PASS
 - FiveSql2 test_sql1999      43/43
 - tests/compat_harbour       56/56 (+5 new: ASort dates/logicals,
                              AScan int cross-type)
 - Regression test test_null_order.prg for ORDER BY NULL ordering

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-04-17 20:20:14 +09:00

286 lines
7.9 KiB
Plaintext

/*
* bench_sql.prg — FiveSql2 SQL execution benchmark
* Measures actual query execution time across various SQL patterns.
*
* Copyright (c) 2025-2026 Charles KWON (Charles KWON OhJun)
* All rights reserved.
*/
#include "FiveSqlDef.ch"
#define ITERS 1000
PROCEDURE Main()
LOCAL aR, t0, t1, i, nRows
ErrorBlock( {|e| QOut( "TRAP: " + e:description + " " + e:operation ), Break(e) } )
? "================================================================"
? " FiveSql2 SQL Execution Benchmark"
? " Iterations per query: " + hb_ntos( ITERS )
? "================================================================"
?
/* Setup: create test tables */
SetupBenchData()
/* Opt in to workarea cache — repeated DML against the same table
* skips dbUseArea/dbCloseArea syscalls. Disabled at cleanup. */
SqlWACacheEnable()
? "--- SELECT Benchmarks ---"
/* B1: Simple SELECT * (full scan) */
t0 := hb_MilliSeconds()
FOR i := 1 TO ITERS
aR := five_SQL( "SELECT * FROM bench_emp" )
NEXT
t1 := hb_MilliSeconds()
nRows := 0
IF ValType( aR ) == "A" .AND. Len( aR ) >= 2
nRows := Len( aR[ 2 ] )
ENDIF
R( "B1_SELECT_STAR", t1 - t0, nRows )
/* B2: SELECT with WHERE (filter) */
t0 := hb_MilliSeconds()
FOR i := 1 TO ITERS
aR := five_SQL( "SELECT name, salary FROM bench_emp WHERE salary > 50000" )
NEXT
t1 := hb_MilliSeconds()
nRows := 0
IF ValType( aR ) == "A" .AND. Len( aR ) >= 2
nRows := Len( aR[ 2 ] )
ENDIF
R( "B2_WHERE_FILTER", t1 - t0, nRows )
/* B3: SELECT with ORDER BY */
t0 := hb_MilliSeconds()
FOR i := 1 TO ITERS
aR := five_SQL( "SELECT name, salary FROM bench_emp ORDER BY salary DESC" )
NEXT
t1 := hb_MilliSeconds()
R( "B3_ORDER_BY", t1 - t0, 0 )
/* B4: GROUP BY + HAVING */
t0 := hb_MilliSeconds()
FOR i := 1 TO ITERS
aR := five_SQL( "SELECT dept, COUNT(*) AS cnt, AVG(salary) AS avg_sal FROM bench_emp GROUP BY dept HAVING COUNT(*) > 1" )
NEXT
t1 := hb_MilliSeconds()
nRows := 0
IF ValType( aR ) == "A" .AND. Len( aR ) >= 2
nRows := Len( aR[ 2 ] )
ENDIF
R( "B4_GROUP_HAVING", t1 - t0, nRows )
/* B5: DISTINCT */
t0 := hb_MilliSeconds()
FOR i := 1 TO ITERS
aR := five_SQL( "SELECT DISTINCT dept FROM bench_emp" )
NEXT
t1 := hb_MilliSeconds()
nRows := 0
IF ValType( aR ) == "A" .AND. Len( aR ) >= 2
nRows := Len( aR[ 2 ] )
ENDIF
R( "B5_DISTINCT", t1 - t0, nRows )
? ""
? "--- JOIN Benchmarks ---"
/* B6: INNER JOIN */
t0 := hb_MilliSeconds()
FOR i := 1 TO ITERS
aR := five_SQL( "SELECT e.name, o.product, o.amount FROM bench_emp e JOIN bench_ord o ON e.id = o.emp_id" )
NEXT
t1 := hb_MilliSeconds()
nRows := 0
IF ValType( aR ) == "A" .AND. Len( aR ) >= 2
nRows := Len( aR[ 2 ] )
ENDIF
R( "B6_INNER_JOIN", t1 - t0, nRows )
? ""
? "--- CTE Benchmarks ---"
/* B7: Simple CTE */
t0 := hb_MilliSeconds()
FOR i := 1 TO ITERS
aR := five_SQL( "WITH top_emp AS (SELECT name, salary FROM bench_emp WHERE salary > 60000) SELECT * FROM top_emp ORDER BY salary DESC" )
NEXT
t1 := hb_MilliSeconds()
R( "B7_CTE_SIMPLE", t1 - t0, 0 )
/* B8: RECURSIVE CTE (sequence 1..20) */
t0 := hb_MilliSeconds()
FOR i := 1 TO ITERS
aR := five_SQL( "WITH RECURSIVE seq(n) AS (SELECT 1 UNION ALL SELECT n+1 FROM seq WHERE n < 20) SELECT n FROM seq" )
NEXT
t1 := hb_MilliSeconds()
nRows := 0
IF ValType( aR ) == "A" .AND. Len( aR ) >= 2
nRows := Len( aR[ 2 ] )
ENDIF
R( "B8_RECURSIVE_CTE", t1 - t0, nRows )
? ""
? "--- Window Function Benchmarks ---"
/* B9: ROW_NUMBER() */
t0 := hb_MilliSeconds()
FOR i := 1 TO ITERS
aR := five_SQL( "SELECT name, salary, ROW_NUMBER() OVER (ORDER BY salary DESC) AS rn FROM bench_emp" )
NEXT
t1 := hb_MilliSeconds()
R( "B9_ROW_NUMBER", t1 - t0, 0 )
/* B10: RANK() PARTITION BY */
t0 := hb_MilliSeconds()
FOR i := 1 TO ITERS
aR := five_SQL( "SELECT name, dept, salary, RANK() OVER (PARTITION BY dept ORDER BY salary DESC) AS rnk FROM bench_emp" )
NEXT
t1 := hb_MilliSeconds()
R( "B10_RANK_PART", t1 - t0, 0 )
/* B11: SUM() OVER PARTITION */
t0 := hb_MilliSeconds()
FOR i := 1 TO ITERS
aR := five_SQL( "SELECT name, dept, salary, SUM(salary) OVER (PARTITION BY dept) AS dept_total FROM bench_emp" )
NEXT
t1 := hb_MilliSeconds()
R( "B11_SUM_OVER", t1 - t0, 0 )
? ""
? "--- DML Benchmarks ---"
/* B12: INSERT (1000 inserts) */
t0 := hb_MilliSeconds()
FOR i := 1 TO ITERS
five_SQL( "INSERT INTO bench_tmp (id, val) VALUES (" + hb_ntos( i ) + ", 'test')" )
NEXT
t1 := hb_MilliSeconds()
R( "B12_INSERT", t1 - t0, ITERS )
/* B13: UPDATE */
t0 := hb_MilliSeconds()
FOR i := 1 TO ITERS
five_SQL( "UPDATE bench_emp SET salary = salary + 1 WHERE id = 1" )
NEXT
t1 := hb_MilliSeconds()
R( "B13_UPDATE", t1 - t0, 0 )
? ""
? "--- Aggregate Benchmarks ---"
/* B14: COUNT(*) */
t0 := hb_MilliSeconds()
FOR i := 1 TO ITERS
aR := five_SQL( "SELECT COUNT(*) AS cnt FROM bench_emp" )
NEXT
t1 := hb_MilliSeconds()
R( "B14_COUNT", t1 - t0, 0 )
/* B15: Complex: CTE + Window + WHERE */
t0 := hb_MilliSeconds()
FOR i := 1 TO ITERS
aR := five_SQL( "WITH dept_stats AS (SELECT dept, AVG(salary) AS avg_sal FROM bench_emp GROUP BY dept) SELECT e.name, e.salary, d.avg_sal, ROW_NUMBER() OVER (PARTITION BY e.dept ORDER BY e.salary DESC) AS rn FROM bench_emp e JOIN dept_stats d ON e.dept = d.dept WHERE e.salary > d.avg_sal" )
NEXT
t1 := hb_MilliSeconds()
nRows := 0
IF ValType( aR ) == "A" .AND. Len( aR ) >= 2
nRows := Len( aR[ 2 ] )
ENDIF
R( "B15_CTE_WIN_JOIN", t1 - t0, nRows )
? ""
? "================================================================"
? " Benchmark Complete"
? "================================================================"
/* Cleanup — dbCloseAll flushes + closes every workarea. */
SqlWACacheDisable()
dbCloseAll()
FErase( "bench_emp.dbf" )
FErase( "bench_ord.dbf" )
FErase( "bench_tmp.dbf" )
FErase( "__cte_top_emp.dbf" )
FErase( "__cte_dept_stats.dbf" )
FErase( "__cte_seq.dbf" )
RETURN
STATIC FUNCTION SetupBenchData()
LOCAL i, aDepts, nIdx
aDepts := { "Engineering", "Sales", "Marketing", "HR", "Finance" }
/* bench_emp: 100 employees */
IF hb_FileExists( "bench_emp.dbf" )
FErase( "bench_emp.dbf" )
ENDIF
dbCreate( "bench_emp.dbf", { ;
{ "ID", "N", 10, 0 }, ;
{ "NAME", "C", 30, 0 }, ;
{ "DEPT", "C", 20, 0 }, ;
{ "SALARY", "N", 12, 2 } ;
} )
USE bench_emp.dbf NEW EXCLUSIVE
FOR i := 1 TO 100
dbAppend()
FieldPut( 1, i )
FieldPut( 2, "Employee_" + PadL( hb_ntos( i ), 3, "0" ) )
nIdx := ( ( i - 1 ) % 5 ) + 1
FieldPut( 3, aDepts[ nIdx ] )
FieldPut( 4, 30000 + i * 500 )
NEXT
dbCommit()
CLOSE bench_emp
/* bench_ord: 200 orders */
IF hb_FileExists( "bench_ord.dbf" )
FErase( "bench_ord.dbf" )
ENDIF
dbCreate( "bench_ord.dbf", { ;
{ "ID", "N", 10, 0 }, ;
{ "EMP_ID", "N", 10, 0 }, ;
{ "PRODUCT", "C", 30, 0 }, ;
{ "AMOUNT", "N", 12, 2 } ;
} )
USE bench_ord.dbf NEW EXCLUSIVE
FOR i := 1 TO 200
dbAppend()
FieldPut( 1, i )
FieldPut( 2, ( ( i - 1 ) % 100 ) + 1 )
FieldPut( 3, "Product_" + PadL( hb_ntos( i % 20 ), 3, "0" ) )
FieldPut( 4, 100 + i * 10 )
NEXT
dbCommit()
CLOSE bench_ord
/* bench_tmp: empty table for INSERT bench */
IF hb_FileExists( "bench_tmp.dbf" )
FErase( "bench_tmp.dbf" )
ENDIF
dbCreate( "bench_tmp.dbf", { ;
{ "ID", "N", 10, 0 }, ;
{ "VAL", "C", 20, 0 } ;
} )
RETURN NIL
STATIC FUNCTION R( cLabel, nMs, nRows )
LOCAL cRow := ""
IF nRows > 0
cRow := " rows=" + hb_ntos( nRows )
ENDIF
? " " + PadR( cLabel, 20 ) + Str( nMs, 8 ) + " ms (" + ;
Str( nMs * 1000 / ITERS, 8, 1 ) + " us/query)" + cRow
RETURN NIL