|
| 1 | +/* |
| 2 | + * Copyright 2025 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.spanner.connection; |
| 18 | + |
| 19 | +import com.google.cloud.spanner.Dialect; |
| 20 | +import org.openjdk.jmh.annotations.Benchmark; |
| 21 | +import org.openjdk.jmh.annotations.Fork; |
| 22 | +import org.openjdk.jmh.annotations.Measurement; |
| 23 | +import org.openjdk.jmh.annotations.Warmup; |
| 24 | + |
| 25 | +@Fork(value = 1, warmups = 0) |
| 26 | +@Warmup(iterations = 1, time = 5) |
| 27 | +@Measurement(iterations = 5, time = 5) |
| 28 | +public class StatementParserBenchmark { |
| 29 | + private static final Dialect dialect = Dialect.POSTGRESQL; |
| 30 | + private static final AbstractStatementParser parser = |
| 31 | + AbstractStatementParser.getInstance(dialect); |
| 32 | + |
| 33 | + private static String longQueryText = generateLongQuery(100 * 1024); // 100kb |
| 34 | + |
| 35 | + /** Generates a long SQL-looking string. */ |
| 36 | + private static String generateLongQuery(int length) { |
| 37 | + StringBuilder sb = new StringBuilder(length + 50); |
| 38 | + sb.append("SELECT * FROM foo WHERE 1"); |
| 39 | + while (sb.length() < length) { |
| 40 | + sb.append(" OR abcdefghijklmnopqrstuvwxyz"); |
| 41 | + } |
| 42 | + return sb.toString(); |
| 43 | + } |
| 44 | + |
| 45 | + @Benchmark |
| 46 | + public boolean isQueryTest() { |
| 47 | + return parser.isQuery("CREATE TABLE FOO (ID INT64, NAME STRING(100)) PRIMARY KEY (ID)"); |
| 48 | + } |
| 49 | + |
| 50 | + @Benchmark |
| 51 | + public boolean longQueryTest() { |
| 52 | + return parser.isQuery(longQueryText); |
| 53 | + } |
| 54 | +} |
0 commit comments