Skip to content

Commit

Permalink
Add mysql tls sm benchmark example
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffery.wsj authored and superajun-wsj committed Sep 20, 2023
1 parent b952974 commit b3f28e5
Show file tree
Hide file tree
Showing 26 changed files with 814 additions and 0 deletions.
21 changes: 21 additions & 0 deletions examples/tls/benchmark/mysql/mysql_client_entrypoint.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/bash

apt update -y && apt install openjdk-11-jdk maven -y

# build mysql client benchmark.
mvn clean package

WORK_DIR=`pwd`

# replace mysql's properity file
mkdir -p target/tmp && cp target/benchmark-1.0.0-jar-with-dependencies.jar target/tmp
pushd target/tmp
jar -xvf benchmark-1.0.0-jar-with-dependencies.jar && rm -rf benchmark-1.0.0-jar-with-dependencies.jar
cp ${WORK_DIR}/src/main/resources/TlsSettings.properties ./com/mysql/cj/
jar -cvf benchmark-1.0.0-jar-with-dependencies.jar ./
popd

# run with provider Dragonwell
java -cp target/tmp/benchmark-1.0.0-jar-with-dependencies.jar -DProvider=Dragonwell com.alibaba.dragonwell.security.mysql.SMTlsMySQLBenchmark
# run with provider Kona
java -cp target/tmp/benchmark-1.0.0-jar-with-dependencies.jar -DProvider=Kona com.alibaba.dragonwell.security.mysql.SMTlsMySQLBenchmark
5 changes: 5 additions & 0 deletions examples/tls/benchmark/mysql/mysql_run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash

./mysql_server_startup.sh

docker run -it --network=host --rm -v `pwd`:`pwd` -w `pwd` ubuntu:22.04 ./mysql_client_entrypoint.sh
12 changes: 12 additions & 0 deletions examples/tls/benchmark/mysql/mysql_server_startup.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#!/bin/bash

# startup mysql service with tls which supports rfc8998.
# stop and remove an mysql service if it existed.
container_name="mysql_service"
if [[ $(docker ps -a --format "{{.Names}}" | grep "^${container_name}$") ]]; then
docker stop mysql_service && docker rm mysql_service
fi

# create and start a new mysql service.
docker run -itd --net=host --privileged=true --name mysql_service cape2/mysql-ssl-smx /usr/sbin/init
docker exec -it mysql_service /MySQL-SMx/entrypoint/mysqld_entrypoint.sh
101 changes: 101 additions & 0 deletions examples/tls/benchmark/mysql/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.alibaba.dragonwell.security.mysql</groupId>
<artifactId>benchmark</artifactId>
<packaging>jar</packaging>
<version>1.0.0</version>
<name>benchmark</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<jmh.version>1.35</jmh.version>
</properties>
<dependencies>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-core</artifactId>
<version>${jmh.version}</version>
</dependency>
<dependency>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh.version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>1.70</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.33</version>
</dependency>
<dependency>
<groupId>com.tencent.kona</groupId>
<artifactId>kona-provider</artifactId>
<version>1.0.7.1</version>
</dependency>
<dependency>
<groupId>com.tencent.kona</groupId>
<artifactId>kona-ssl</artifactId>
<version>1.0.7.1</version>
</dependency>
<dependency>
<groupId>com.tencent.kona</groupId>
<artifactId>kona-crypto</artifactId>
<version>1.0.7.1</version>
</dependency>
<dependency>
<groupId>com.tencent.kona</groupId>
<artifactId>kona-pkix</artifactId>
<version>1.0.7.1</version>
</dependency>
<dependency>
<groupId>com.alibaba.dragonwell</groupId>
<artifactId>security-native</artifactId>
<version>1.2.0</version>
<classifier>${os.detected.classifier}</classifier>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.4.1.Final</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-assembly-plugin</artifactId>
<version>3.3.0</version>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
<executions>
<execution>
<id>make-assembly</id>
<phase>package</phase>
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package com.alibaba.dragonwell.security.mysql;

import java.security.Security;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.FileOutputStream;
import java.io.File;
import java.util.concurrent.TimeUnit;

import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.infra.Blackhole;
import org.openjdk.jmh.results.format.ResultFormatType;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;

import com.mysql.cj.jdbc.MysqlDataSource;
import com.tencent.kona.KonaProvider;
import com.alibaba.dragonwell.security.DragonwellSecurityProvider;

@BenchmarkMode(Mode.AverageTime)
@Warmup(iterations = 6, time = 1)
@Measurement(iterations = 6, time = 1)
@Threads(1)
@Fork(1)
@State(value = Scope.Thread)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class SMTlsMySQLBenchmark {
private final static String DRAGONWELL = "Dragonwell";
private final static String KONA = "Kona";
private final static String PROVIDER = System.getProperty("Provider");

public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(SMTlsMySQLBenchmark.class.getSimpleName())
.result("mysql_sm4_security_provider_benchmark.json")
.resultFormat(ResultFormatType.JSON).build();
new Runner(opt).run();
}

@Benchmark
public void smTlsMySQLBenchMark(SMTlsMySQLBenchmark.BenchmarkContext context, Blackhole blackhole) throws Exception {
String IMAGE_DATA = "image_data";
ResultSet resultSet = context.getPreparedStatement().executeQuery();
blackhole.consume(resultSet);
resultSet.close();
}

@State(Scope.Thread)
public static class BenchmarkContext {
private final String USER = "root";
private final String HOST_NAME = "127.0.0.1";
private final String JKS_PASSWORD = "123456";
private final String SQL_PASSWORD = "Wsj.123456";
private final String DATA_BASE = "IMAGES";
private final String DATA_TABLE = "images_table";
private final String SQL = "SELECT image_data FROM images_table WHERE id = ?;";
private final int PORT = 3306;

private MysqlDataSource mysqlDS = null;
private Connection conn = null;
private PreparedStatement ps = null;

private void initContext() throws Exception {
if (DRAGONWELL.equals(PROVIDER)) {
Security.insertProviderAt(new DragonwellSecurityProvider(), 1);
} else if (KONA.equals(PROVIDER)) {
Security.insertProviderAt(new KonaProvider(), 1);
}

String trustCA = extractLibrary(SMTlsMySQLBenchmark.class.getClassLoader(), "truststore.jks");
mysqlDS = new MysqlDataSource();
mysqlDS.setUseSSL(true);
mysqlDS.setTrustCertificateKeyStoreType("JKS");
mysqlDS.setTrustCertificateKeyStoreUrl(trustCA);
mysqlDS.setTrustCertificateKeyStorePassword(JKS_PASSWORD);

mysqlDS.setServerName(HOST_NAME);
mysqlDS.setPort(PORT);
mysqlDS.setUser(USER);
mysqlDS.setPassword(SQL_PASSWORD);
mysqlDS.setDatabaseName(DATA_BASE);

conn = mysqlDS.getConnection();

String sql = "SELECT image_data FROM images_table WHERE id = ?;";
ps = conn.prepareStatement(sql);
ps.setInt(1, 1);
}

public PreparedStatement getPreparedStatement() {
return ps;
}

@Setup
public void createContext() throws Exception {
initContext();
}

@TearDown
public void teardown() throws Exception {
ps.close();
conn.close();
}

private String extractLibrary(ClassLoader classLoader, String name) throws Exception {
int pos = name.lastIndexOf('.');
File file = File.createTempFile(name.substring(0, pos), name.substring(pos));
String fullPath = file.getAbsolutePath();
try (InputStream in = classLoader.getResourceAsStream(name);
OutputStream out = new FileOutputStream(file)) {
byte[] buf = new byte[4096];
int length;
while ((length = in.read(buf)) > 0) {
out.write(buf, 0, length);
}
} finally {
file.deleteOnExit();
}
return fullPath;
}
}
}
Loading

0 comments on commit b3f28e5

Please sign in to comment.