⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 jtpccterminal.java

📁 业界最为经典的SQL性能测试工具
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
/*
 * jTPCCTerminal - Terminal emulator code for jTPCC (transactions)
 *
 * Copyright (C) 2003, Raul Barbosa
 * Copyright (C) 2004-2006, Denis Lussier
 *
 */

import java.io.PrintWriter;
import java.io.StringWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
import java.util.Enumeration;
import java.util.Random;
import java.util.Vector;

import pojo.Customer;
import pojo.District;
import pojo.History;
import pojo.Item;
import pojo.NewOrder;
import pojo.Oorder;
import pojo.OrderLine;
import pojo.Stock;
import pojo.Warehouse;
import client.JOutputArea;
import client.jTPCCConfig;
import client.jTPCCUtil;


public class jTPCCTerminal implements jTPCCConfig, Runnable
{
    private String terminalName;
    private Connection conn = null;
    private Statement stmt = null;
    private Statement stmt1 = null;
    private ResultSet rs = null;
    private int terminalWarehouseID, terminalDistrictID;
    private int paymentWeight, orderStatusWeight, deliveryWeight, stockLevelWeight;
    private JOutputArea terminalOutputArea, errorOutputArea;
    private boolean debugMessages;
    private jTPCC parent;
    private Random  gen;

    private int transactionCount = 1, numTransactions, numWarehouses, newOrderCounter;
    private StringBuffer query = null;
    private int result = 0;
    private boolean stopRunningSignal = false;

    //NewOrder Txn
      private PreparedStatement stmtGetCustWhse = null;
      private PreparedStatement stmtGetDist = null;
      private PreparedStatement stmtInsertNewOrder = null;
      private PreparedStatement stmtUpdateDist = null;
      private PreparedStatement stmtInsertOOrder = null;
      private PreparedStatement stmtGetItem = null;
      private PreparedStatement stmtGetStock = null;
      private PreparedStatement stmtUpdateStock = null;
      private PreparedStatement stmtInsertOrderLine = null;

    //Payment Txn
      private PreparedStatement payUpdateWhse = null;
      private PreparedStatement payGetWhse = null;
      private PreparedStatement payUpdateDist = null;
      private PreparedStatement payGetDist = null;
      private PreparedStatement payCountCust = null;
      private PreparedStatement payCursorCustByName = null;
      private PreparedStatement payGetCust = null;
      private PreparedStatement payGetCustCdata = null;
      private PreparedStatement payUpdateCustBalCdata = null;
      private PreparedStatement payUpdateCustBal = null;
      private PreparedStatement payInsertHist = null;

    //Order Status Txn
      private PreparedStatement ordStatCountCust = null;
      private PreparedStatement ordStatGetCust = null;
      private PreparedStatement ordStatGetNewestOrd = null;
      private PreparedStatement ordStatGetCustBal = null;
      private PreparedStatement ordStatGetOrder = null;
      private PreparedStatement ordStatGetOrderLines = null;

    //Delivery Txn
      private PreparedStatement delivGetOrderId = null;
      private PreparedStatement delivDeleteNewOrder = null;
      private PreparedStatement delivGetCustId = null;
      private PreparedStatement delivUpdateCarrierId = null;
      private PreparedStatement delivUpdateDeliveryDate = null;
      private PreparedStatement delivSumOrderAmount = null;
      private PreparedStatement delivUpdateCustBalDelivCnt = null;

    //Stock Level Txn
      private PreparedStatement stockGetDistOrderId = null;
      private PreparedStatement stockGetCountStock = null;

    public jTPCCTerminal(String terminalName, int terminalWarehouseID, int terminalDistrictID, Connection conn, int numTransactions, JOutputArea terminalOutputArea, JOutputArea errorOutputArea, boolean debugMessages, int paymentWeight, int orderStatusWeight, int deliveryWeight, int stockLevelWeight, int numWarehouses, jTPCC parent) throws SQLException
    {
        this.terminalName = terminalName;
        this.conn = conn;
        this.stmt = conn.createStatement();
        this.stmt.setMaxRows(200);
        this.stmt.setFetchSize(100);

        this.stmt1 = conn.createStatement();
        this.stmt1.setMaxRows(1);

        this.terminalWarehouseID = terminalWarehouseID;
        this.terminalDistrictID = terminalDistrictID;
        this.terminalOutputArea = terminalOutputArea;
        this.errorOutputArea = errorOutputArea;
        this.debugMessages = debugMessages;
        this.parent = parent;
        this.numTransactions = numTransactions;
        this.paymentWeight = paymentWeight;
        this.orderStatusWeight = orderStatusWeight;
        this.deliveryWeight = deliveryWeight;
        this.stockLevelWeight = stockLevelWeight;
        this.numWarehouses = numWarehouses;
        this.newOrderCounter = 0;
        terminalMessage("Terminal \'" + terminalName + "\' has WarehouseID=" + terminalWarehouseID + " and DistrictID=" + terminalDistrictID + ".");
    }

    public void run()
    {
        gen = new Random(System.currentTimeMillis() * conn.hashCode());

        executeTransactions(numTransactions);
        try
        {
            printMessage("Closing statement and connection...");
            stmt.close();
            conn.close();
        }
        catch(Exception e)
        {
            printMessage("An error occurred!");
            logException(e);
        }

        printMessage("Terminal \'" + terminalName + "\' finished after " + (transactionCount-1) + " transaction(s).");

        parent.signalTerminalEnded(this, newOrderCounter);
    }

    public void stopRunningWhenPossible()
    {
        stopRunningSignal = true;
        printMessage("Terminal received stop signal!");
        printMessage("Finishing current transaction before exit...");
    }

    private void executeTransactions(int numTransactions)
    {
        boolean stopRunning = false;

        if(numTransactions != -1)
            printMessage("Executing " + numTransactions + " transactions...");
        else
            printMessage("Executing for a limited time...");

        for(int i = 0; (i < numTransactions || numTransactions == -1) && !stopRunning; i++)
        {
            long transactionType = jTPCCUtil.randomNumber(1, 100, gen);
            int skippedDeliveries = 0, newOrder = 0;
            String transactionTypeName;

            long transactionStart = System.currentTimeMillis();

            if(transactionType <= paymentWeight)
            {
                executeTransaction(PAYMENT);
                transactionTypeName = "Payment";
            }
            else if(transactionType <= paymentWeight + stockLevelWeight)
            {
                executeTransaction(STOCK_LEVEL);
                transactionTypeName = "Stock-Level";
            }
            else if(transactionType <= paymentWeight + stockLevelWeight + orderStatusWeight)
            {
                executeTransaction(ORDER_STATUS);
                transactionTypeName = "Order-Status";
            }
            else if(transactionType <= paymentWeight + stockLevelWeight + orderStatusWeight + deliveryWeight)
            {
                skippedDeliveries = executeTransaction(DELIVERY);
                transactionTypeName = "Delivery";
            }
            else
            {
                executeTransaction(NEW_ORDER);
                transactionTypeName = "New-Order";
                newOrderCounter++;
                newOrder = 1;
            }

            long transactionEnd = System.currentTimeMillis();

            if(!transactionTypeName.equals("Delivery"))
            {
                parent.signalTerminalEndedTransaction(this.terminalName, transactionTypeName, transactionEnd - transactionStart, null, newOrder);
            }
            else
            {
                parent.signalTerminalEndedTransaction(this.terminalName, transactionTypeName, transactionEnd - transactionStart, (skippedDeliveries == 0 ? "None" : "" + skippedDeliveries + " delivery(ies) skipped."), newOrder);
            }


            if(stopRunningSignal) stopRunning = true;
        }
    }


    private int executeTransaction(int transaction)
    {
        int result = 0;

        switch(transaction)
        {
            case NEW_ORDER:
                int districtID = jTPCCUtil.randomNumber(1, configDistPerWhse, gen);
                int customerID = jTPCCUtil.getCustomerID(gen);

                int numItems = (int)jTPCCUtil.randomNumber(5, 15, gen);
                int[] itemIDs = new int[numItems];
                int[] supplierWarehouseIDs = new int[numItems];
                int[] orderQuantities = new int[numItems];
                int allLocal = 1;
                for(int i = 0; i < numItems; i++)
                {
                    itemIDs[i] = jTPCCUtil.getItemID(gen);
                    if(jTPCCUtil.randomNumber(1, 100, gen) > 1)
                    {
                        supplierWarehouseIDs[i] = terminalWarehouseID;
                    }
                    else
                    {
                        do
                        {
                            supplierWarehouseIDs[i] = jTPCCUtil.randomNumber(1, numWarehouses, gen);
                        }
                        while(supplierWarehouseIDs[i] == terminalWarehouseID && numWarehouses > 1);
                        allLocal = 0;
                    }
                    orderQuantities[i] = jTPCCUtil.randomNumber(1, 10, gen);
                }

                // we need to cause 1% of the new orders to be rolled back.
                if(jTPCCUtil.randomNumber(1, 100, gen) == 1)
                    itemIDs[numItems-1] = -12345;

                terminalMessage("\nStarting transaction #" + transactionCount + " (New-Order)...");
                newOrderTransaction(terminalWarehouseID, districtID, customerID, numItems, allLocal, itemIDs, supplierWarehouseIDs, orderQuantities);
                break;


            case PAYMENT:
                districtID = jTPCCUtil.randomNumber(1, 10, gen);

                int x = jTPCCUtil.randomNumber(1, 100, gen);
                int customerDistrictID;
                int customerWarehouseID;
                if(x <= 85)
                {
                    customerDistrictID = districtID;
                    customerWarehouseID = terminalWarehouseID;
                }
                else
                {
                    customerDistrictID = jTPCCUtil.randomNumber(1, 10, gen);
                    do
                    {
                        customerWarehouseID = jTPCCUtil.randomNumber(1, numWarehouses, gen);
                    }
                    while(customerWarehouseID == terminalWarehouseID && numWarehouses > 1);
                }

                long y = jTPCCUtil.randomNumber(1, 100, gen);
                boolean customerByName;
                String customerLastName = null;
                customerID = -1;
//                if(y <= 60)  {
                    // 60% lookups by last name
//                    customerByName = true;
//                    customerLastName = jTPCCUtil.getLastName(gen);
//                    printMessage("Last name lookup = " + customerLastName);
//                } else {
//                    // 40% lookups by customer ID
                    customerByName = false;
                    customerID = jTPCCUtil.getCustomerID(gen);
//                }

                float paymentAmount = (float)(jTPCCUtil.randomNumber(100, 500000, gen)/100.0);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -