dbGenerator.c
上传用户:romrleung
上传日期:2022-05-23
资源大小:18897k
文件大小:15k
源码类别:

MySQL数据库

开发平台:

Visual C++

  1. /* Copyright (C) 2003 MySQL AB
  2.    This program is free software; you can redistribute it and/or modify
  3.    it under the terms of the GNU General Public License as published by
  4.    the Free Software Foundation; either version 2 of the License, or
  5.    (at your option) any later version.
  6.    This program is distributed in the hope that it will be useful,
  7.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  8.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  9.    GNU General Public License for more details.
  10.    You should have received a copy of the GNU General Public License
  11.    along with this program; if not, write to the Free Software
  12.    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
  13. /***************************************************************
  14. * I N C L U D E D   F I L E S                                  *
  15. ***************************************************************/
  16. #include <ndb_global.h>
  17. #include "dbGenerator.h"
  18. /***************************************************************
  19. * L O C A L   C O N S T A N T S                                *
  20. ***************************************************************/
  21. /***************************************************************
  22. * L O C A L   D A T A   S T R U C T U R E S                    *
  23. ***************************************************************/
  24. /***************************************************************
  25. * L O C A L   F U N C T I O N S                                *
  26. ***************************************************************/
  27. static void getRandomSubscriberNumber(SubscriberNumber number);
  28. static void getRandomServerId(ServerId *serverId);
  29. static void getRandomChangedBy(ChangedBy changedBy);
  30. static void getRandomChangedTime(ChangedTime changedTime);
  31. static void clearTransaction(TransactionDefinition *trans);
  32. static void initGeneratorStatistics(GeneratorStatistics *gen);
  33. static void doOneTransaction(UserHandle *uh, GeneratorStatistics *gen);
  34. static void doTransaction_T1(UserHandle *uh, GeneratorStatistics *gen);
  35. static void doTransaction_T2(UserHandle *uh, GeneratorStatistics *gen);
  36. static void doTransaction_T3(UserHandle *uh, GeneratorStatistics *gen);
  37. static void doTransaction_T4(UserHandle *uh, GeneratorStatistics *gen);
  38. static void doTransaction_T5(UserHandle *uh, GeneratorStatistics *gen);
  39. /***************************************************************
  40. * L O C A L   D A T A                                          *
  41. ***************************************************************/
  42. static SequenceValues transactionDefinition[] = {
  43.    {25, 1},
  44.    {25, 2},
  45.    {20, 3},
  46.    {15, 4},
  47.    {15, 5},
  48.    {0,  0}
  49. };
  50. static SequenceValues rollbackDefinition[] = {
  51.    {98, 0},
  52.    {2 , 1},
  53.    {0,  0}
  54. };
  55. static int maxsize = 0;
  56. /***************************************************************
  57. * P U B L I C   D A T A                                        *
  58. ***************************************************************/
  59. /***************************************************************
  60. ****************************************************************
  61. * L O C A L   F U N C T I O N S   C O D E   S E C T I O N      *
  62. ****************************************************************
  63. ***************************************************************/
  64. static void getRandomSubscriberNumber(SubscriberNumber number)
  65. {
  66.    uint32 tmp;
  67.    char sbuf[SUBSCRIBER_NUMBER_LENGTH + 1];
  68.    tmp = myRandom48(NO_OF_SUBSCRIBERS);
  69.    sprintf(sbuf, "%.*d", SUBSCRIBER_NUMBER_LENGTH, tmp);
  70.    memcpy(number, sbuf, SUBSCRIBER_NUMBER_LENGTH);
  71. }
  72. static void getRandomServerId(ServerId *serverId)
  73. {
  74.    *serverId = myRandom48(NO_OF_SERVERS);
  75. }
  76. static void getRandomChangedBy(ChangedBy changedBy)
  77. {
  78.    memset(changedBy, myRandom48(26)+'A', CHANGED_BY_LENGTH);
  79.    changedBy[CHANGED_BY_LENGTH] = 0;
  80. }
  81. static void getRandomChangedTime(ChangedTime changedTime)
  82. {
  83.    memset(changedTime, myRandom48(26)+'A', CHANGED_TIME_LENGTH);
  84.    changedTime[CHANGED_TIME_LENGTH] = 0;
  85. }
  86. static void clearTransaction(TransactionDefinition *trans)
  87. {
  88.    trans->benchTime        = 0.0;
  89.    trans->count            = 0;
  90.    trans->branchExecuted   = 0;
  91.    trans->rollbackExecuted = 0;
  92. }
  93. static int listFull(SessionList *list)
  94. {
  95.    return(list->numberInList == SESSION_LIST_LENGTH);
  96. }
  97. static int listEmpty(SessionList *list)
  98. {
  99.    return(list->numberInList == 0);
  100. }
  101. static void insertSession(SessionList     *list, 
  102.                           SubscriberNumber number,
  103.                           ServerId         serverId)
  104. {
  105.    SessionElement *e;
  106.    if( listFull(list) ) return;
  107.    e = &list->list[list->writeIndex];
  108.    strcpy(e->subscriberNumber, number);
  109.    e->serverId = serverId;
  110.    list->writeIndex = (list->writeIndex + 1) % SESSION_LIST_LENGTH;
  111.    list->numberInList++;
  112. if( list->numberInList > maxsize )
  113. maxsize = list->numberInList;
  114. }
  115. static SessionElement *getNextSession(SessionList *list)
  116. {
  117.    if( listEmpty(list) ) return(0);
  118.    return(&list->list[list->readIndex]);
  119. }
  120. static void deleteSession(SessionList *list)
  121. {
  122.    if( listEmpty(list) ) return;
  123.    list->readIndex = (list->readIndex + 1) % SESSION_LIST_LENGTH;
  124.    list->numberInList--;
  125. }
  126. static void initGeneratorStatistics(GeneratorStatistics *gen)
  127. {
  128.    int i;
  129.    if( initSequence(&gen->transactionSequence,
  130.                     transactionDefinition) != 0 ) {
  131.       printf("could not set the transaction typesn");
  132.       exit(0);
  133.    }
  134.    if( initSequence(&gen->rollbackSequenceT4,
  135.                     rollbackDefinition) != 0 ) {
  136.       printf("could not set the rollback sequencen");
  137.       exit(0);
  138.    }
  139.    if( initSequence(&gen->rollbackSequenceT5,
  140.                     rollbackDefinition) != 0 ) {
  141.       printf("could not set the rollback sequencen");
  142.       exit(0);
  143.    }
  144.    for(i = 0; i < NUM_TRANSACTION_TYPES; i++ )
  145.       clearTransaction(&gen->transactions[i]);
  146.    gen->totalTransactions = 0;
  147.    gen->activeSessions.numberInList = 0;
  148.    gen->activeSessions.readIndex    = 0;
  149.    gen->activeSessions.writeIndex   = 0;
  150. }
  151. static void doOneTransaction(UserHandle *uh, GeneratorStatistics *gen)
  152. {
  153.    unsigned int transactionType;
  154.    transactionType = getNextRandom(&gen->transactionSequence);
  155.    switch(transactionType) {
  156.       case 1:
  157.          doTransaction_T1(uh, gen);
  158.          break;
  159.       case 2:
  160.          doTransaction_T2(uh, gen);
  161.          break;
  162.       case 3:
  163.          doTransaction_T3(uh, gen);
  164.          break;
  165.       case 4:
  166.          doTransaction_T4(uh, gen);
  167.          break;
  168.       case 5:
  169.          doTransaction_T5(uh, gen);
  170.          break;
  171.       default:
  172.          printf("Unknown transaction type: %dn", transactionType);
  173.    }
  174.    gen->totalTransactions++;
  175. }
  176. static void doTransaction_T1(UserHandle *uh, GeneratorStatistics *gen)
  177. {
  178.    SubscriberNumber number;
  179.    Location         new_location;
  180.    ChangedBy        changed_by; 
  181.    ChangedTime      changed_time;
  182.    double start_time;
  183.    double end_time;
  184.    double transaction_time;
  185.    unsigned int tid = 0;
  186.    /*----------------*/
  187.    /* Init arguments */
  188.    /*----------------*/
  189.    getRandomSubscriberNumber(number);
  190.    getRandomChangedBy(changed_by);
  191.    getRandomChangedTime(changed_time);
  192.    new_location = changed_by[0];
  193.    /*-----------------*/
  194.    /* Run transaction */
  195.    /*-----------------*/
  196.    start_time = userGetTimeSync();
  197.    userTransaction_T1(uh,
  198.                       number,
  199.                       new_location,
  200.                       changed_by,
  201.                       changed_time);
  202.    end_time = userGetTimeSync();
  203.    /*-------------------*/
  204.    /* Update Statistics */
  205.    /*-------------------*/
  206.    transaction_time = end_time - start_time;
  207.    gen->transactions[tid].benchTime += transaction_time;
  208.    gen->transactions[tid].count++;
  209. }
  210. static void doTransaction_T2(UserHandle *uh, GeneratorStatistics *gen)
  211. {
  212.    SubscriberNumber number;
  213.    Location         new_location;
  214.    ChangedBy        changed_by; 
  215.    ChangedTime      changed_time;
  216.    SubscriberName   subscriberName;
  217.    double start_time;
  218.    double end_time;
  219.    double transaction_time;
  220.    unsigned int tid = 1;
  221.    /*----------------*/
  222.    /* Init arguments */
  223.    /*----------------*/
  224.    getRandomSubscriberNumber(number);
  225.    /*-----------------*/
  226.    /* Run transaction */
  227.    /*-----------------*/
  228.    start_time = userGetTimeSync();
  229.    userTransaction_T2(uh,
  230.                       number,
  231.                      &new_location,
  232.                       changed_by,
  233.                       changed_time,
  234.                       subscriberName);
  235.    end_time = userGetTimeSync();
  236.    /*-------------------*/
  237.    /* Update Statistics */
  238.    /*-------------------*/
  239.    transaction_time = end_time - start_time;
  240.    gen->transactions[tid].benchTime += transaction_time;
  241.    gen->transactions[tid].count++;
  242. }
  243. static void doTransaction_T3(UserHandle *uh, GeneratorStatistics *gen)
  244. {
  245.    SubscriberNumber number;
  246.    ServerId         serverId;
  247.    ServerBit        serverBit;
  248.    SessionDetails   sessionDetails;
  249.    unsigned int     branchExecuted;
  250.    SessionElement  *se;
  251.    double start_time;
  252.    double end_time;
  253.    double transaction_time;
  254.    unsigned int tid = 2;
  255.    /*----------------*/
  256.    /* Init arguments */
  257.    /*----------------*/
  258.    se = getNextSession(&gen->activeSessions);
  259.    if( se ) {
  260.       strcpy(number, se->subscriberNumber);
  261.       serverId = se->serverId;
  262.    }
  263.    else {
  264.       getRandomSubscriberNumber(number);
  265.       getRandomServerId(&serverId);
  266.    }
  267.    serverBit = 1 << serverId;
  268.    /*-----------------*/
  269.    /* Run transaction */
  270.    /*-----------------*/
  271.    start_time = userGetTimeSync();
  272.    userTransaction_T3(uh,
  273.                       number,
  274.                       serverId,
  275.                       serverBit,
  276.                       sessionDetails,
  277.                       &branchExecuted);
  278.    end_time = userGetTimeSync();
  279.    /*-------------------*/
  280.    /* Update Statistics */
  281.    /*-------------------*/
  282.    transaction_time = end_time - start_time;
  283.    gen->transactions[tid].benchTime += transaction_time;
  284.    gen->transactions[tid].count++;
  285.    if(branchExecuted)
  286.       gen->transactions[tid].branchExecuted++;
  287. }
  288. static void doTransaction_T4(UserHandle *uh, GeneratorStatistics *gen)
  289. {
  290.    SubscriberNumber number;
  291.    ServerId         serverId;
  292.    ServerBit        serverBit;
  293.    SessionDetails   sessionDetails;
  294.    unsigned int     branchExecuted;
  295.    unsigned int     rollback;
  296.    double start_time;
  297.    double end_time;
  298.    double transaction_time;
  299.    unsigned int tid = 3;
  300.    /*----------------*/
  301.    /* Init arguments */
  302.    /*----------------*/
  303.    getRandomSubscriberNumber(number);
  304.    getRandomServerId(&serverId);
  305.    serverBit = 1 << serverId;
  306.    rollback  = getNextRandom(&gen->rollbackSequenceT4);
  307.    memset(sessionDetails, myRandom48(26)+'A', SESSION_DETAILS_LENGTH);
  308.    sessionDetails[SESSION_DETAILS_LENGTH] = 0;
  309.    /*-----------------*/
  310.    /* Run transaction */
  311.    /*-----------------*/
  312.    start_time = userGetTimeSync();
  313.    userTransaction_T4(uh,
  314.                       number,
  315.                       serverId,
  316.                       serverBit,
  317.                       sessionDetails,
  318.                       rollback,
  319.                       &branchExecuted);
  320.    end_time = userGetTimeSync();
  321.    /*-------------------*/
  322.    /* Update Statistics */
  323.    /*-------------------*/
  324.    transaction_time = end_time - start_time;
  325.    gen->transactions[tid].benchTime += transaction_time;
  326.    gen->transactions[tid].count++;
  327.    if(branchExecuted)
  328.       gen->transactions[tid].branchExecuted++;
  329.    if(rollback)
  330.       gen->transactions[tid].rollbackExecuted++;
  331.    if( branchExecuted && !rollback ) {
  332.       insertSession(&gen->activeSessions, number, serverId);
  333.    }
  334. }
  335. static void doTransaction_T5(UserHandle *uh, GeneratorStatistics *gen)
  336. {
  337.    SubscriberNumber number;
  338.    ServerId         serverId;
  339.    ServerBit        serverBit;
  340.    unsigned int     branchExecuted;
  341.    unsigned int     rollback;
  342.    SessionElement  *se;
  343.    double start_time;
  344.    double end_time;
  345.    double transaction_time;
  346.    unsigned int tid = 4;
  347.    /*----------------*/
  348.    /* Init arguments */
  349.    /*----------------*/
  350.    se = getNextSession(&gen->activeSessions);
  351.    if( se ) {
  352.       strcpy(number, se->subscriberNumber);
  353.       serverId = se->serverId;
  354.    }
  355.    else {
  356.       getRandomSubscriberNumber(number);
  357.       getRandomServerId(&serverId);
  358.    }
  359.    serverBit = 1 << serverId;
  360.    rollback  = getNextRandom(&gen->rollbackSequenceT5);
  361.    /*-----------------*/
  362.    /* Run transaction */
  363.    /*-----------------*/
  364.    start_time = userGetTimeSync();
  365.    userTransaction_T5(uh,
  366.                       number,
  367.                       serverId,
  368.                       serverBit,
  369.                       rollback,
  370.                       &branchExecuted);
  371.    end_time = userGetTimeSync();
  372.    /*-------------------*/
  373.    /* Update Statistics */
  374.    /*-------------------*/
  375.    transaction_time = end_time - start_time;
  376.    gen->transactions[tid].benchTime += transaction_time;
  377.    gen->transactions[tid].count++;
  378.    if(branchExecuted)
  379.       gen->transactions[tid].branchExecuted++;
  380.    if(rollback)
  381.       gen->transactions[tid].rollbackExecuted++;
  382.    if( se && !rollback) {
  383.       deleteSession(&gen->activeSessions);
  384.    }
  385. }
  386. /***************************************************************
  387. ****************************************************************
  388. * P U B L I C   F U N C T I O N S   C O D E   S E C T I O N    *
  389. ****************************************************************
  390. ***************************************************************/
  391. void dbGenerator(UserHandle *uh, ThreadData *data)
  392. {
  393.    GeneratorStatistics rg_warmUp;
  394.    GeneratorStatistics rg_coolDown;
  395.    GeneratorStatistics *st;
  396.    double periodStop;
  397.    double benchTimeStart;
  398.    double benchTimeEnd;
  399.    int i;
  400.    myRandom48Init(data->randomSeed);
  401.    initGeneratorStatistics(&rg_warmUp);
  402.    initGeneratorStatistics(&data->generator);
  403.    initGeneratorStatistics(&rg_coolDown);
  404.    /*----------------*/
  405.    /* warm up period */
  406.    /*----------------*/
  407.    periodStop = userGetTimeSync() + (double)data->warmUpSeconds;
  408.    while(userGetTimeSync() < periodStop){
  409.      doOneTransaction(uh, &rg_warmUp);
  410.    }
  411.    /*-------------------------*/
  412.    /* normal benchmark period */
  413.    /*-------------------------*/
  414.    benchTimeStart = userGetTimeSync();
  415.    if( data->numTransactions > 0 ) {
  416.       for(i = 0; i < data->numTransactions; i++)
  417.          doOneTransaction(uh, &data->generator);
  418.    }
  419.    else {
  420.       periodStop = benchTimeStart + (double)data->testSeconds;
  421.       while(userGetTimeSync() < periodStop)
  422.          doOneTransaction(uh, &data->generator);
  423.    }
  424.    benchTimeEnd = userGetTimeSync();
  425.    /*------------------*/
  426.    /* cool down period */
  427.    /*------------------*/
  428.    periodStop = benchTimeEnd + data->coolDownSeconds;
  429.    while(userGetTimeSync() < periodStop){
  430.      doOneTransaction(uh, &rg_coolDown);
  431.    }
  432.    /*---------------------------------------------------------*/
  433.    /* add the times for all transaction for inner loop timing */
  434.    /*---------------------------------------------------------*/
  435.    st = &data->generator;
  436.    st->innerLoopTime = 0.0;
  437.    for(i = 0 ; i < NUM_TRANSACTION_TYPES; i++) {
  438.       st->innerLoopTime += st->transactions[i].benchTime;
  439.       st->transactions[i].tps = getTps(st->transactions[i].count,
  440.                                        st->transactions[i].benchTime);
  441.    }
  442.    st->outerLoopTime = benchTimeEnd - benchTimeStart;
  443.    st->outerTps      = getTps(st->totalTransactions, st->outerLoopTime);
  444.    st->innerTps      = getTps(st->totalTransactions, st->innerLoopTime);
  445.    /* printf("maxsize = %dn",maxsize); */
  446. }