Skip to content

Commit

Permalink
feat: Added random query distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
Julien Ruaux committed Jul 26, 2022
1 parent c503f54 commit 61f752f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public MySQLDataLoader(Config config, DataSource dataSource) {
}

@PostConstruct
public void execute() throws SQLException {
public void start() throws SQLException {
CUSTOMERS.execute(new CustomerRowProvider(config.getCustomers()), dataSource, config.getBatch());
PRODUCTS.execute(new ProductRowProvider(config.getProducts()), dataSource, config.getBatch());
ORDERS.execute(new OrderRowProvider(config.getOrders(), config.getCustomers()), dataSource, config.getBatch());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
Expand Down Expand Up @@ -49,7 +50,7 @@ public MySQLQueryExecutor(RedisURI redisURI, DataSourceProperties dataSourceProp
}

@PostConstruct
public void executeQueries() {
public void start() {
if (config.isFlush()) {
RedisClient.create(redisURI).connect().sync().flushall();
}
Expand Down Expand Up @@ -80,6 +81,7 @@ public void executeQueries() {

private static class QueryTask implements Runnable {

private final Random random = new Random();
private final DataSource dataSource;
private final ProgressBar progressBar;
private final int totalRows;
Expand All @@ -93,17 +95,15 @@ public QueryTask(DataSource dataSource, int totalRows, ProgressBar progressBar)

@Override
public void run() {
int index = 0;
while (!stopped) {
index++;
try (Connection connection = dataSource.getConnection();
PreparedStatement statement = connection.prepareStatement(QUERY)) {
statement.setInt(1, index % totalRows);
int orderNumber = random.nextInt(totalRows) + 1;
statement.setInt(1, orderNumber);
try (ResultSet resultSet = statement.executeQuery()) {
while (resultSet.next()) {
for (int columnIndex = 1; columnIndex <= resultSet.getMetaData()
.getColumnCount(); columnIndex++) {
resultSet.getObject(columnIndex);
for (int index = 0; index < resultSet.getMetaData().getColumnCount(); index++) {
resultSet.getObject(index + 1);
}
}
}
Expand Down

0 comments on commit 61f752f

Please sign in to comment.