~paul-mccullagh/perconatools/tpcc-fix-mac-build

« back to all changes in this revision

Viewing changes to src/support.c

  • Committer: Vadim Tkachenko
  • Date: 2008-12-04 02:50:59 UTC
  • Revision ID: vadim@percona.com-20081204025059-7qdugxws565smubk
Initial import

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * support.c
 
3
 * routines needed for the tpcc loading and transaction programs
 
4
 */
 
5
 
 
6
#include <stdlib.h>
 
7
#include <stdio.h>
 
8
#include <string.h>
 
9
#include <time.h>
 
10
#include <ctype.h>
 
11
 
 
12
#include "tpc.h"
 
13
 
 
14
static int nums[CUST_PER_DIST];
 
15
static int perm_count;
 
16
 
 
17
void SetSeed (int seed)
 
18
{
 
19
        srand(seed);
 
20
}
 
21
 
 
22
/*
 
23
 * return number uniformly distributed b/w min and max, inclusive
 
24
 */
 
25
int RandomNumber (int min, int max)
 
26
{
 
27
        return min + (rand() % ((max - min) + 1));
 
28
}
 
29
 
 
30
/*
 
31
 * non uniform random -- see p. 15
 
32
 *
 
33
 * the constant C depends on which value of A is passed, but the same
 
34
 * value of C should be used for all calls with the same value of
 
35
 * A.  however, we know in advance which values of A will be used.
 
36
 */
 
37
int NURand (unsigned A, unsigned x, unsigned y)
 
38
{
 
39
        static int first = 1;
 
40
        unsigned C, C_255, C_1023, C_8191;
 
41
 
 
42
        if (first) {
 
43
                C_255 = RandomNumber(0, 255);
 
44
                C_1023 = RandomNumber(0, 1023);
 
45
                C_8191 = RandomNumber(0, 8191);
 
46
                first = 0;
 
47
        }
 
48
 
 
49
        switch (A) {
 
50
        case 255: C = C_255; break;
 
51
        case 1023: C = C_1023; break;
 
52
        case 8191: C = C_8191; break;
 
53
        default:
 
54
                fprintf(stderr,
 
55
                        "NURand: unexpected value (%d) of A used\n",
 
56
                        A);
 
57
                abort();
 
58
        }
 
59
 
 
60
        return (int)
 
61
               (((RandomNumber(0, A) | RandomNumber(x, y)) + C) % (y-x+1)) + x;
 
62
}
 
63
 
 
64
/*
 
65
 * p. 54
 
66
 *
 
67
 * make a ``random a-string'': a string of random alphanumeric
 
68
 * characters of a random length of minimum x, maximum y, and
 
69
 * mean (y+x)/2
 
70
 */
 
71
int MakeAlphaString (int x, int y, char str[])
 
72
{
 
73
        static char *alphanum = "0123456789"
 
74
                                "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 
75
                                "abcdefghijklmnopqrstuvwxyz";
 
76
        int arrmax = 61;  /* index of last array element */
 
77
        register int i, len;
 
78
 
 
79
        len = RandomNumber(x, y);
 
80
 
 
81
        for (i = 0; i < len; i++)
 
82
                str[i] = alphanum[RandomNumber(0, arrmax)];
 
83
 
 
84
        return len;
 
85
}
 
86
 
 
87
/*
 
88
 * like MakeAlphaString, only numeric characters only
 
89
 */
 
90
int MakeNumberString (int x, int y, char str[])
 
91
{
 
92
        static char *numeric = "0123456789";
 
93
        int arrmax = 9;
 
94
        register int i, len;
 
95
 
 
96
        len = RandomNumber(x, y);
 
97
 
 
98
        for (i = 0; i < len; i++)
 
99
                str[i] = numeric[RandomNumber(0, arrmax)];
 
100
 
 
101
        return len;
 
102
}
 
103
 
 
104
/*
 
105
 * turn system time into database format
 
106
 * the format argument should be a strftime() format string that produces
 
107
 *   a datetime string acceptable to the database
 
108
 */
 
109
void gettimestamp (char str[], char *format, size_t len)
 
110
{
 
111
        time_t t;
 
112
        struct tm *datetime;
 
113
 
 
114
        t = time(NULL);
 
115
        datetime = localtime(&t);
 
116
 
 
117
        if ( !strftime(str, len, format, datetime) ) {
 
118
                fprintf(stderr, "error writing timestamp to string\n");
 
119
                abort();
 
120
        }
 
121
}
 
122
 
 
123
/*
 
124
 * permute the list of customer ids for the order table
 
125
 */
 
126
void InitPermutation (void)
 
127
{
 
128
        int *cur;
 
129
        int i,j;
 
130
 
 
131
        perm_count = 0;
 
132
 
 
133
        /* initialize with consecutive values [1..ORD_PER_DIST] */
 
134
        for (i = 0, cur = nums; i < ORD_PER_DIST; i++, cur++) {
 
135
                *cur = i + 1;
 
136
        }
 
137
 
 
138
        /* now, shuffle */
 
139
        for (i = 0; i < ORD_PER_DIST-1; i++) {
 
140
                j = (int)RandomNumber(i+1, ORD_PER_DIST-1);
 
141
                swap_int(nums[i], nums[j]);
 
142
        }
 
143
}
 
144
 
 
145
int GetPermutation (void)
 
146
{
 
147
        if ( perm_count >= ORD_PER_DIST ) {
 
148
                fprintf(stderr, "GetPermutation: past end of list!\n");
 
149
                abort();
 
150
        }
 
151
        return nums[perm_count++];
 
152
}
 
153
 
 
154
/*==================================================================+
 
155
 | ROUTINE NAME
 
156
 |      Lastname
 
157
 | DESCRIPTION
 
158
 |      TPC-C Lastname Function.
 
159
 | ARGUMENTS 
 
160
 |      num  - non-uniform random number
 
161
 |      name - last name string
 
162
 +==================================================================*/
 
163
void Lastname(num, name)
 
164
  int num;
 
165
  char *name;
 
166
{
 
167
  static char *n[] = 
 
168
    {"BAR", "OUGHT", "ABLE", "PRI", "PRES", 
 
169
     "ESE", "ANTI", "CALLY", "ATION", "EING"};
 
170
 
 
171
  strcpy(name,n[num/100]);
 
172
  strcat(name,n[(num/10)%10]);
 
173
  strcat(name,n[num%10]);
 
174
 
 
175
 return;
 
176
}
 
177