99 */
1010package net .sf .jsqlparser .benchmark ;
1111
12- import net .sf .jsqlparser .parser .CCJSqlParser ;
13- import net .sf .jsqlparser .statement .Statements ;
1412import org .openjdk .jmh .annotations .*;
1513import org .openjdk .jmh .infra .Blackhole ;
1614
2119import java .nio .charset .StandardCharsets ;
2220import java .nio .file .*;
2321import java .util .concurrent .*;
24- import java .util .function .Consumer ;
2522
2623@ BenchmarkMode (Mode .AverageTime )
2724@ OutputTimeUnit (TimeUnit .MILLISECONDS )
@@ -44,13 +41,33 @@ public void setup() throws Exception {
4441 } else {
4542 Path jarPath = downloadJsqlparserJar (version );
4643 URLClassLoader loader = new URLClassLoader (new URL [] {jarPath .toUri ().toURL ()}, null );
47- runner = new DynamicParserRunner (loader );
44+ try {
45+ runner = new DynamicParserRunner (loader );
46+ } catch (Exception ex ) {
47+ loader .close ();
48+ throw ex ;
49+ }
4850 }
4951
50- // Adjust path as necessary based on where source root is during test execution
51- Path path = Paths .get ("src/test/resources/net/sf/jsqlparser/performance.sql" );
52- sqlContent = Files .readString (path , StandardCharsets .UTF_8 );
53- executorService = Executors .newSingleThreadExecutor ();
52+ // Reject incompatible SQL/configurations before collecting measurements.
53+ try {
54+ Path path = Paths .get ("src/test/resources/net/sf/jsqlparser/performance.sql" );
55+ sqlContent = Files .readString (path , StandardCharsets .UTF_8 );
56+ executorService = Executors .newSingleThreadExecutor ();
57+ Object statements = runner .parseStatements (sqlContent , executorService ,
58+ SqlParserRunner .Configuration .SIMPLE );
59+ if (statements == null ) {
60+ throw new IllegalStateException ("Parser " + version
61+ + " returned no statements for the benchmark corpus with SIMPLE configuration" );
62+ }
63+ } catch (Exception ex ) {
64+ try {
65+ tearDown ();
66+ } catch (Exception cleanup ) {
67+ ex .addSuppressed (cleanup );
68+ }
69+ throw ex ;
70+ }
5471 }
5572
5673 private Path downloadJsqlparserJar (String version ) throws IOException {
@@ -74,10 +91,10 @@ private Path downloadJsqlparserJar(String version) throws IOException {
7491
7592 @ Benchmark
7693 public void parseSQLStatements (Blackhole blackhole ) throws Exception {
77- final Statements statements = runner .parseStatements (
94+ final Object statements = runner .parseStatements (
7895 sqlContent ,
7996 executorService ,
80- ( Consumer < CCJSqlParser >) parser -> parser . withAllowComplexParsing ( false ) );
97+ SqlParserRunner . Configuration . SIMPLE );
8198 blackhole .consume (statements );
8299 }
83100
@@ -87,15 +104,23 @@ public void parseQuotedText(Blackhole blackhole) throws Exception {
87104 + "INSERT INTO recycle_record (a,f) VALUES ('\\ 'anything', 'abc');\n "
88105 + "INSERT INTO recycle_record (a,f) VALUES ('\\ '','83653692186728700711687663398101');\n " ;
89106
90- final Statements statements = runner .parseStatements (
107+ final Object statements = runner .parseStatements (
91108 sqlStr ,
92109 executorService ,
93- ( Consumer < CCJSqlParser >) parser -> parser . withBackslashEscapeCharacter ( true ) );
110+ SqlParserRunner . Configuration . BACKSLASH_ESCAPES );
94111 blackhole .consume (statements );
95112 }
96113
97114 @ TearDown (Level .Trial )
98- public void tearDown () {
99- executorService .shutdown ();
115+ public void tearDown () throws Exception {
116+ try {
117+ if (executorService != null ) {
118+ executorService .shutdownNow ();
119+ }
120+ } finally {
121+ if (runner != null ) {
122+ runner .close ();
123+ }
124+ }
100125 }
101126}
0 commit comments