|
| 1 | +package com.yugabyte.simulation.service; |
| 2 | + |
| 3 | +import com.yugabyte.simulation.dao.InvocationResult; |
| 4 | +import com.yugabyte.simulation.dao.ParamValue; |
| 5 | +import com.yugabyte.simulation.dao.WorkloadDesc; |
| 6 | +import com.yugabyte.simulation.dao.WorkloadParamDesc; |
| 7 | +import com.yugabyte.simulation.services.ServiceManager; |
| 8 | +import com.yugabyte.simulation.util.QuikShipWorkloadUtil; |
| 9 | +import com.yugabyte.simulation.workload.*; |
| 10 | +import org.springframework.beans.factory.annotation.Autowired; |
| 11 | +import org.springframework.jdbc.core.JdbcTemplate; |
| 12 | +import org.springframework.jdbc.core.RowCallbackHandler; |
| 13 | +import org.springframework.stereotype.Repository; |
| 14 | + |
| 15 | +import java.sql.ResultSet; |
| 16 | +import java.sql.SQLException; |
| 17 | +import java.sql.Types; |
| 18 | +import java.util.*; |
| 19 | +import java.util.concurrent.ThreadLocalRandom; |
| 20 | + |
| 21 | +@Repository |
| 22 | +public class QuikShipWorkload extends WorkloadSimulationBase implements WorkloadSimulation { |
| 23 | + |
| 24 | + |
| 25 | + @Autowired |
| 26 | + private JdbcTemplate jdbcTemplate; |
| 27 | + |
| 28 | + @Autowired |
| 29 | + private ServiceManager serviceManager; |
| 30 | + |
| 31 | + @Override |
| 32 | + public String getName() { |
| 33 | + return "QuikShip"; |
| 34 | + } |
| 35 | + |
| 36 | + private static final String CREATE_PRODUCT_TYPE = "CREATE TYPE product_type_enum AS ENUM ('book', 'technology');"; |
| 37 | + |
| 38 | + private static final String CREATE_PRODUCTS = "CREATE TABLE if not exists products(\n" + |
| 39 | + " id SERIAL PRIMARY KEY,\n" + |
| 40 | + " title VARCHAR(255),\n" + |
| 41 | + " author VARCHAR(255),\n" + |
| 42 | + " imageLink VARCHAR(255),\n" + |
| 43 | + " price decimal(12, 2),\n" + |
| 44 | + " product_type product_type_enum\n" + |
| 45 | + ");"; |
| 46 | + |
| 47 | + private static final String CREATE_ORDERS = "CREATE TABLE if not exists orders(\n" + |
| 48 | + " id SERIAL PRIMARY KEY,\n" + |
| 49 | + " total decimal(12, 2),\n" + |
| 50 | + " products VARCHAR(255)\n" + |
| 51 | + ");"; |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | + private static final String DROP_PRODUCT_TYPE = "drop type if exists product_type_enum;"; |
| 56 | + private static final String DROP_PRODUCTS = "drop table if exists products cascade;"; |
| 57 | + private static final String TRUNCATE_PRODUCTS = "truncate table products;"; |
| 58 | + private static final String DROP_ORDERS = "drop table if exists orders cascade;"; |
| 59 | + private static final String TRUNCATE_ORDERS = "truncate table orders;"; |
| 60 | + |
| 61 | + private static final String INSERT_RECORD_ORDERS = "insert into orders(total,products) values(?,?);"; |
| 62 | + |
| 63 | + private final String POINT_SELECT_QUERY_ORDERS = "select id, total, products from orders where id = ?;"; |
| 64 | + |
| 65 | + private static final int ROWS_TO_PRELOAD = 10000; |
| 66 | + |
| 67 | + private enum WorkloadType { |
| 68 | + CREATE_TABLES, |
| 69 | + SEED_DATA, |
| 70 | + RUN_SIMULATION_FIXED_WORKLOAD, |
| 71 | + RUN_SIMULATION, |
| 72 | + RUN_LIKE_QUERY_ON_GENERIC2, |
| 73 | + RUN_LIKE_QUERY_ON_GENERIC3, |
| 74 | + START_NODE, |
| 75 | + STOP_NODE |
| 76 | + } |
| 77 | + |
| 78 | + private final FixedStepsWorkloadType createTablesWorkloadType; |
| 79 | + private final FixedTargetWorkloadType seedingWorkloadType; |
| 80 | + private final ThroughputWorkloadType runInstanceType; |
| 81 | + private final FixedTargetWorkloadType simulationFixedWorkloadType; |
| 82 | + |
| 83 | + public QuikShipWorkload() { |
| 84 | + this.createTablesWorkloadType = new FixedStepsWorkloadType( |
| 85 | + new Step("Drop orders", (a,b) -> jdbcTemplate.execute(DROP_ORDERS)), |
| 86 | + new Step("Drop products", (a,b) -> jdbcTemplate.execute(DROP_PRODUCTS)), |
| 87 | + new Step("Drop product_type_enum", (a, b) -> jdbcTemplate.execute(DROP_PRODUCT_TYPE)), |
| 88 | + new Step("Create product_type_enum", (a,b) -> jdbcTemplate.execute(CREATE_PRODUCT_TYPE)), |
| 89 | + new Step("Create products", (a,b) -> jdbcTemplate.execute(CREATE_PRODUCTS)), |
| 90 | + new Step("Create orders", (a,b) -> jdbcTemplate.execute(CREATE_ORDERS)), |
| 91 | + new Step("Populate Products", (a,b) -> jdbcTemplate.execute(QuikShipWorkloadUtil.INSERT_PRODUCTS_DATA)) |
| 92 | + ); |
| 93 | + |
| 94 | + this.seedingWorkloadType = new FixedTargetWorkloadType(); |
| 95 | + this.runInstanceType = new ThroughputWorkloadType(); |
| 96 | + this.simulationFixedWorkloadType = new FixedTargetWorkloadType(); |
| 97 | + } |
| 98 | + |
| 99 | + private WorkloadDesc createTablesWorkload = new WorkloadDesc( |
| 100 | + QuikShipWorkload.WorkloadType.CREATE_TABLES.toString(), |
| 101 | + "Create Tables", |
| 102 | + "Create the database tables. If the table already exists it will be dropped" |
| 103 | + ); |
| 104 | + |
| 105 | + private WorkloadDesc seedingWorkload = new WorkloadDesc( |
| 106 | + QuikShipWorkload.WorkloadType.SEED_DATA.toString(), |
| 107 | + "Seed Data", |
| 108 | + "Load data into the orders table", |
| 109 | + new WorkloadParamDesc("Items to generate:", 1, Integer.MAX_VALUE, 10000), |
| 110 | + new WorkloadParamDesc("Threads", 1, 500, 32) |
| 111 | + ); |
| 112 | + |
| 113 | + private WorkloadDesc runningWorkload = new WorkloadDesc( |
| 114 | + QuikShipWorkload.WorkloadType.RUN_SIMULATION.toString(), |
| 115 | + "Simulation - TPS", |
| 116 | + "Run a simulation of a reads on orders placed", |
| 117 | + new WorkloadParamDesc("Throughput (tps)", 1, 1000000, 500), |
| 118 | + new WorkloadParamDesc("Max Threads", 1, 500, 64), |
| 119 | + new WorkloadParamDesc("Include placing of new orders (inserts)", false) |
| 120 | + ); |
| 121 | + |
| 122 | + private WorkloadDesc simulationFixedWorkload = new WorkloadDesc( |
| 123 | + QuikShipWorkload.WorkloadType.RUN_SIMULATION_FIXED_WORKLOAD.toString(), |
| 124 | + "Simulation", |
| 125 | + "Run a simulation of a reads on orders placed", |
| 126 | + new WorkloadParamDesc("Invocations", 1, 10000000, 1000000), |
| 127 | + new WorkloadParamDesc("Max Threads", 1, 500, 64), |
| 128 | + new WorkloadParamDesc("Include placing of new orders (inserts)", false) |
| 129 | + ); |
| 130 | + |
| 131 | + |
| 132 | + @Override |
| 133 | + public List<WorkloadDesc> getWorkloads() { |
| 134 | + return Arrays.asList( |
| 135 | + createTablesWorkload |
| 136 | + , seedingWorkload |
| 137 | + , simulationFixedWorkload |
| 138 | + , runningWorkload |
| 139 | + ); |
| 140 | + } |
| 141 | + |
| 142 | + |
| 143 | + @Override |
| 144 | + public InvocationResult invokeWorkload(String workloadId, ParamValue[] values) { |
| 145 | + QuikShipWorkload.WorkloadType type = QuikShipWorkload.WorkloadType.valueOf(workloadId); |
| 146 | + try { |
| 147 | + switch (type) { |
| 148 | + case CREATE_TABLES: |
| 149 | + this.createTables(); |
| 150 | + return new InvocationResult("Ok"); |
| 151 | + case SEED_DATA: |
| 152 | + this.seedData(values[0].getIntValue(), values[1].getIntValue()); |
| 153 | + return new InvocationResult("Ok"); |
| 154 | + case RUN_SIMULATION: |
| 155 | + this.runSimulation(values); |
| 156 | + return new InvocationResult("Ok"); |
| 157 | + case RUN_SIMULATION_FIXED_WORKLOAD: |
| 158 | + this.runSimulationFixedWorkload(values); |
| 159 | + return new InvocationResult("Ok"); |
| 160 | + case STOP_NODE: |
| 161 | + return new InvocationResult("Ok"); |
| 162 | + case START_NODE: |
| 163 | + return new InvocationResult("Ok"); |
| 164 | + |
| 165 | + } |
| 166 | + throw new IllegalArgumentException("Unknown workload "+ workloadId); |
| 167 | + } |
| 168 | + catch (Exception e) { |
| 169 | + return new InvocationResult(e); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + private void createTables() { |
| 174 | + createTablesWorkloadType.createInstance(serviceManager).execute(); |
| 175 | + } |
| 176 | + |
| 177 | + private void seedData(int numberToGenerate, int threads) { |
| 178 | + seedingWorkloadType |
| 179 | + .createInstance(serviceManager) |
| 180 | + .execute(threads, numberToGenerate, (customData, threadData) -> { |
| 181 | + runInserts(); |
| 182 | +// UUID uuid = LoadGeneratorUtils.getUUID(); |
| 183 | +// jdbcTemplate.update(INSERT_RECORD_GENERIC1, |
| 184 | +// uuid, |
| 185 | +// LoadGeneratorUtils.getInt(0, 100), |
| 186 | +// LoadGeneratorUtils.getInt(20, 300), |
| 187 | +// LoadGeneratorUtils.getInt(100, 1000), |
| 188 | +// LoadGeneratorUtils.getInt(0, 1000), |
| 189 | +// LoadGeneratorUtils.getDouble(), |
| 190 | +// LoadGeneratorUtils.getDouble(), |
| 191 | +// LoadGeneratorUtils.getDouble() |
| 192 | +// ); |
| 193 | +// jdbcTemplate.update(INSERT_RECORD_GENERIC2, |
| 194 | +// uuid, |
| 195 | +// LoadGeneratorUtils.getAlphaString(LoadGeneratorUtils.getInt(1,30)) |
| 196 | +// ); |
| 197 | +// jdbcTemplate.update(INSERT_RECORD_GENERIC3, |
| 198 | +// uuid, |
| 199 | +// LoadGeneratorUtils.getAlphaString(LoadGeneratorUtils.getInt(1,255)), |
| 200 | +// LoadGeneratorUtils.getAlphaString(LoadGeneratorUtils.getInt(1,30)) |
| 201 | +// ); |
| 202 | + return threadData; |
| 203 | + }); |
| 204 | + } |
| 205 | + |
| 206 | + |
| 207 | + |
| 208 | + |
| 209 | + private void runSimulationFixedWorkload(ParamValue[] values) { |
| 210 | + int numOfInvocations = values[0].getIntValue(); |
| 211 | + int maxThreads = values[1].getIntValue(); |
| 212 | + boolean runInserts = values[2].getBoolValue(); |
| 213 | + seedingWorkloadType |
| 214 | + .createInstance(serviceManager) |
| 215 | + .execute(maxThreads, numOfInvocations, (customData, threadData) -> { |
| 216 | + int id = LoadGeneratorUtils.getInt(1,ROWS_TO_PRELOAD);; |
| 217 | + runPointReadOrders(id); |
| 218 | + if(runInserts){ |
| 219 | + runInserts(); |
| 220 | + } |
| 221 | + return threadData; |
| 222 | + }); |
| 223 | + } |
| 224 | + |
| 225 | + private void runSimulation(ParamValue[] values) { |
| 226 | + int tps = values[0].getIntValue(); |
| 227 | + int maxThreads = values[1].getIntValue(); |
| 228 | + boolean runInserts = values[2].getBoolValue(); |
| 229 | + |
| 230 | + Random random = ThreadLocalRandom.current(); |
| 231 | + jdbcTemplate.setFetchSize(1000); |
| 232 | + |
| 233 | + runInstanceType |
| 234 | + .createInstance(serviceManager, this.runningWorkload, values) |
| 235 | + .setMaxThreads(maxThreads) |
| 236 | + .execute(tps, (customData, threadData) -> { |
| 237 | + int id = LoadGeneratorUtils.getInt(1,ROWS_TO_PRELOAD);; |
| 238 | + runPointReadOrders(id); |
| 239 | + |
| 240 | + if(runInserts){ |
| 241 | + runInserts(); |
| 242 | + } |
| 243 | + }); |
| 244 | + } |
| 245 | + |
| 246 | + private void runPointReadOrders(int id){ |
| 247 | + String query = POINT_SELECT_QUERY_ORDERS; |
| 248 | + jdbcTemplate.query(query, new Object[] {id}, new int[] {Types.INTEGER}, |
| 249 | + new RowCallbackHandler() { |
| 250 | + @Override |
| 251 | + public void processRow(ResultSet rs) throws SQLException { |
| 252 | +// System.out.printf("id=%s, col1='%s', col2=%s \n", |
| 253 | +// rs.getString("id"), |
| 254 | +// rs.getString("total"), |
| 255 | +// rs.getString("products") |
| 256 | +// ); |
| 257 | + } |
| 258 | + }); |
| 259 | + } |
| 260 | + |
| 261 | + |
| 262 | + private void runInserts(){ |
| 263 | + UUID uuid = LoadGeneratorUtils.getUUID(); |
| 264 | + jdbcTemplate.update(INSERT_RECORD_ORDERS, |
| 265 | + LoadGeneratorUtils.getDouble(1.00,1000.00), |
| 266 | + LoadGeneratorUtils.getText(10,40) |
| 267 | + ); |
| 268 | + } |
| 269 | + |
| 270 | + |
| 271 | + |
| 272 | +} |
0 commit comments