Skip to content

Commit

Permalink
Merge pull request SphereEx-QE#2 from SphereEx/add-traffic-rule-case
Browse files Browse the repository at this point in the history
Add : add Traffic Algorithm benchmark
  • Loading branch information
wsm12138 authored Mar 8, 2022
2 parents ff20712 + 7220eba commit 783750c
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
5 changes: 5 additions & 0 deletions jmh-shardingsphere5/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ THE POSSIBILITY OF SUCH DAMAGE.
<artifactId>jmh-base</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-traffic-core</artifactId>
<version>${shardingsphere.version}</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core</artifactId>
Expand Down
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 (?, ?);"));
}
}

0 comments on commit 783750c

Please sign in to comment.