forked from SphereEx-QE/database-jmh
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request SphereEx-QE#2 from SphereEx/add-traffic-rule-case
Add : add Traffic Algorithm benchmark
- Loading branch information
Showing
2 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...ngsphere5/src/main/java/icu/wwj/jmh/shardingsphere5/TrafficAlgorithmCompareBenchmark.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package icu.wwj.jmh.shardingsphere5; | ||
|
||
import org.apache.shardingsphere.traffic.algorithm.traffic.hint.SQLHintTrafficAlgorithm; | ||
import org.apache.shardingsphere.traffic.algorithm.traffic.segment.SQLMatchTrafficAlgorithm; | ||
import org.apache.shardingsphere.traffic.algorithm.traffic.segment.SQLRegexTrafficAlgorithm; | ||
import org.apache.shardingsphere.traffic.api.traffic.hint.HintTrafficValue; | ||
import org.apache.shardingsphere.traffic.api.traffic.segment.SegmentTrafficValue; | ||
import org.openjdk.jmh.annotations.Benchmark; | ||
import org.openjdk.jmh.annotations.BenchmarkMode; | ||
import org.openjdk.jmh.annotations.Fork; | ||
import org.openjdk.jmh.annotations.Level; | ||
import org.openjdk.jmh.annotations.Measurement; | ||
import org.openjdk.jmh.annotations.Mode; | ||
import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
import org.openjdk.jmh.annotations.Scope; | ||
import org.openjdk.jmh.annotations.Setup; | ||
import org.openjdk.jmh.annotations.State; | ||
import org.openjdk.jmh.annotations.Threads; | ||
import org.openjdk.jmh.annotations.Warmup; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
@BenchmarkMode(Mode.Throughput) | ||
@OutputTimeUnit(TimeUnit.MILLISECONDS) | ||
@Fork(1) | ||
@Threads(5) | ||
@Warmup(iterations = 5, time = 5) | ||
@Measurement(iterations = 5, time = 10) | ||
@State(Scope.Benchmark) | ||
public class TrafficAlgorithmCompareBenchmark { | ||
|
||
private SQLHintTrafficAlgorithm sqlHintAlgorithm; | ||
|
||
private SQLMatchTrafficAlgorithm sqlMatchAlgorithm; | ||
|
||
private SQLRegexTrafficAlgorithm sqlRegexAlgorithm; | ||
|
||
@Setup(Level.Trial) | ||
public void setUp() { | ||
initSQLHintAlgorithm(); | ||
initSQLMatchAlgorithm(); | ||
initSQLRegexAlgorithm(); | ||
} | ||
|
||
private void initSQLRegexAlgorithm() { | ||
sqlRegexAlgorithm = new SQLRegexTrafficAlgorithm(); | ||
sqlRegexAlgorithm.getProps().setProperty("regex", "(?i)^(UPDATE|SELECT).*WHERE user_id.*"); | ||
sqlRegexAlgorithm.init(); | ||
} | ||
|
||
private void initSQLMatchAlgorithm() { | ||
sqlMatchAlgorithm = new SQLMatchTrafficAlgorithm(); | ||
sqlMatchAlgorithm.getProps().setProperty("sql", "SELECT * FROM t_order WHERE content IN (?, ?); UPDATE t_order SET creation_date = NOW() WHERE user_id = 1;"); | ||
sqlMatchAlgorithm.init(); | ||
} | ||
|
||
private void initSQLHintAlgorithm() { | ||
sqlHintAlgorithm = new SQLHintTrafficAlgorithm(); | ||
sqlHintAlgorithm.getProps().setProperty("use_traffic", "true"); | ||
sqlHintAlgorithm.init(); | ||
} | ||
|
||
@Benchmark | ||
public void testSQLHintAlgorithmMatch() { | ||
sqlHintAlgorithm.match(new HintTrafficValue<>("/* shardingsphere hint:use_traffic=true */")); | ||
} | ||
|
||
@Benchmark | ||
public void testSQLMatchAlgorithmMatch() { | ||
sqlMatchAlgorithm.match(new SegmentTrafficValue(null, "SELECT * FROM t_order WHERE content IN (?, ?);")); | ||
} | ||
|
||
@Benchmark | ||
public void testSQLRegexAlgorithmMatch() { | ||
sqlRegexAlgorithm.match(new SegmentTrafficValue(null, "SELECT * FROM t_order WHERE user_id IN (?, ?);")); | ||
} | ||
} |