~drizzle-trunk/drizzle/development

1 by brian
clean slate
1
/* Copyright (C) 2006 MySQL AB
2
3
   This program is free software; you can redistribute it and/or modify
4
   it under the terms of the GNU General Public License as published by
5
   the Free Software Foundation; version 2 of the License.
6
7
   This program is distributed in the hope that it will be useful,
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10
   GNU General Public License for more details.
11
12
   You should have received a copy of the GNU General Public License
13
   along with this program; if not, write to the Free Software
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
15
16
#include "azio.h"
17
#include <string.h>
18
#include <assert.h>
19
#include <stdio.h>
20
#include <string.h>
212.5.21 by Monty Taylor
Moved my_getopt.h
21
#include <mysys/my_getopt.h>
1 by brian
clean slate
22
481.1.15 by Monty Taylor
Removed time.h and sys/time.h from global.h.
23
#if TIME_WITH_SYS_TIME
24
# include <sys/time.h>
25
# include <time.h>
26
#else
27
# if HAVE_SYS_TIME_H
28
#  include <sys/time.h>
29
# else
30
#  include <time.h>
31
# endif
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
32
#endif
481.1.15 by Monty Taylor
Removed time.h and sys/time.h from global.h.
33
1 by brian
clean slate
34
#define COMMENT_STRING "Your bases"
35
#define FRM_STRING "My bases"
36
#define TEST_FILENAME "test.az"
37
#define TEST_STRING_INIT "YOU don't know about me without you have read a book by the name of The Adventures of Tom Sawyer; but that ain't no matter.  That book was made by Mr. Mark Twain, and he told the truth, mainly.  There was things which he stretched, but mainly he told the truth.  That is nothing.  I never seen anybody but lied one time or another, without it was Aunt Polly, or the widow, or maybe Mary.  Aunt Polly--Tom's Aunt Polly, she is--and Mary, and the Widow Douglas is all told about in that book, which is mostly a true book, with some stretchers, as I said before.  Now the way that the book winds up is this:  Tom and me found the money that the robbers hid in the cave, and it made us rich.  We got six thousand dollars apiece--all gold.  It was an awful sight of money when it was piled up.  Well, Judge Thatcher he took it and put it out at interest, and it fetched us a dollar a day apiece all the year round --more than a body could tell what to do with.  The Widow Douglas she took me for her son, and allowed she would..."
38
#define TEST_LOOP_NUM 100
39
40
#define BUFFER_LEN 1024
41
42
char test_string[BUFFER_LEN];
43
481.1.2 by Monty Taylor
Replaced all unsigned long long with uint64_t.
44
uint64_t row_lengths[]= {536870912LL, 2147483648LL, 4294967296LL, 8589934592LL};
45
uint64_t row_numbers[]= {524288LL, 2097152LL, 4194304LL, 8388608LL};
1 by brian
clean slate
46
47
/* prototypes */
481.1.2 by Monty Taylor
Replaced all unsigned long long with uint64_t.
48
int size_test(uint64_t length, uint64_t rows_to_test_for, az_method method);
1 by brian
clean slate
49
int small_test(az_method method);
50
long int timedif(struct timeval a, struct timeval b);
51
52
53
int main(int argc, char *argv[])
54
{
55
  az_method method;
56
  unsigned int x;
57
58
  if (argc > 2)
59
    return 0;
60
61
  my_init();
62
63
  MY_INIT(argv[0]);
64
65
  for (method= AZ_METHOD_BLOCK; method < AZ_METHOD_MAX; method++)
66
  {
67
    struct timeval start_time, end_time;
68
    long int timing;
69
70
    printf("Testing %d\n", (int)method);
71
    gettimeofday(&start_time, NULL);
72
    small_test(method);
73
    gettimeofday(&end_time, NULL);
74
    timing= timedif(end_time, start_time);
75
    printf("\tTime took %ld.%03ld seconds\n\n", timing / 1000, timing % 1000);
76
  }
77
78
  if (argc > 1)
79
    return 0;
80
81
  /* Start size tests */
82
  printf("About to run .5/2/4/8 gig tests now, you may want to hit CTRL-C\n");
83
  for (x= 0; x < 4; x++) /* 4 is the current size of the array we use */
84
  {
85
    for (method= AZ_METHOD_BLOCK; method < AZ_METHOD_MAX; method++)
86
    {
87
      struct timeval start_time, end_time;
88
      long int timing;
89
481.1.2 by Monty Taylor
Replaced all unsigned long long with uint64_t.
90
      printf("Testing %"PRIu64" bytes with (%d)\n", row_lengths[x], (int)method);
1 by brian
clean slate
91
      gettimeofday(&start_time, NULL);
92
      size_test(row_lengths[x], row_numbers[x], method);
93
      gettimeofday(&end_time, NULL);
94
      timing= timedif(end_time, start_time);
95
      printf("\tTime took %ld.%03ld seconds\n\n", timing / 1000, timing % 1000);
96
    }
97
  }
98
99
  my_end(0);
100
101
  return 0;
102
}
103
104
int small_test(az_method method)
105
{
106
  unsigned int ret;
107
  char comment_str[10];
108
109
  int error;
110
  unsigned int x;
111
  int written_rows= 0;
112
  azio_stream writer_handle, reader_handle;
113
114
  memcpy(test_string, TEST_STRING_INIT, 1024);
115
116
  unlink(TEST_FILENAME);
117
492.1.14 by Monty Taylor
Removed O_BINARY and FILE_BINARY.
118
  if (!(ret= azopen(&writer_handle, TEST_FILENAME, O_CREAT|O_RDWR,
1 by brian
clean slate
119
                    method)))
120
  {
121
    printf("Could not create test file\n");
122
    return 0;
123
  }
124
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
125
  azwrite_comment(&writer_handle, (char *)COMMENT_STRING,
1 by brian
clean slate
126
                  (unsigned int)strlen(COMMENT_STRING));
127
  azread_comment(&writer_handle, comment_str);
128
  assert(!memcmp(COMMENT_STRING, comment_str,
129
                strlen(COMMENT_STRING)));
130
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
131
  azwrite_frm(&writer_handle, (char *)FRM_STRING,
1 by brian
clean slate
132
                  (unsigned int)strlen(FRM_STRING));
133
  azread_frm(&writer_handle, comment_str);
134
  assert(!memcmp(FRM_STRING, comment_str,
135
                strlen(FRM_STRING)));
136
137
492.1.14 by Monty Taylor
Removed O_BINARY and FILE_BINARY.
138
  if (!(ret= azopen(&reader_handle, TEST_FILENAME, O_RDONLY,
1 by brian
clean slate
139
                    method)))
140
  {
141
    printf("Could not open test file\n");
142
    return 0;
143
  }
144
145
  assert(reader_handle.rows == 0);
146
  assert(reader_handle.auto_increment == 0);
147
  assert(reader_handle.check_point == 0);
148
  assert(reader_handle.forced_flushes == 0);
149
  assert(reader_handle.dirty == AZ_STATE_DIRTY);
150
151
  for (x= 0; x < TEST_LOOP_NUM; x++)
152
  {
153
    ret= azwrite_row(&writer_handle, test_string, BUFFER_LEN);
154
    assert(ret == BUFFER_LEN);
155
    written_rows++;
156
  }
157
  azflush(&writer_handle,  Z_SYNC_FLUSH);
158
159
  azread_comment(&writer_handle, comment_str);
160
  assert(!memcmp(COMMENT_STRING, comment_str,
161
                strlen(COMMENT_STRING)));
162
163
  /* Lets test that our internal stats are good */
164
  assert(writer_handle.rows == TEST_LOOP_NUM);
165
166
  /* Reader needs to be flushed to make sure it is up to date */
167
  azflush(&reader_handle,  Z_SYNC_FLUSH);
168
  assert(reader_handle.rows == TEST_LOOP_NUM);
169
  assert(reader_handle.auto_increment == 0);
170
  assert(reader_handle.check_point == 1269);
171
  assert(reader_handle.forced_flushes == 1);
172
  assert(reader_handle.comment_length == 10);
173
  assert(reader_handle.dirty == AZ_STATE_SAVED);
174
175
  writer_handle.auto_increment= 4;
176
  azflush(&writer_handle, Z_SYNC_FLUSH);
177
  assert(writer_handle.rows == TEST_LOOP_NUM);
178
  assert(writer_handle.auto_increment == 4);
179
  assert(writer_handle.check_point == 1269);
180
  assert(writer_handle.forced_flushes == 2);
181
  assert(writer_handle.dirty == AZ_STATE_SAVED);
182
183
  azclose(&reader_handle);
184
492.1.14 by Monty Taylor
Removed O_BINARY and FILE_BINARY.
185
  if (!(ret= azopen(&reader_handle, TEST_FILENAME, O_RDONLY,
1 by brian
clean slate
186
                    method)))
187
  {
188
    printf("Could not open test file\n");
189
    return 0;
190
  }
191
192
193
  /* Read the original data */
194
  azread_init(&reader_handle);
195
  for (x= 0; x < writer_handle.rows; x++)
196
  {
197
    ret= azread_row(&reader_handle, &error);
198
    assert(!error);
199
    assert(ret == BUFFER_LEN);
200
    assert(!memcmp(reader_handle.row_ptr, test_string, ret));
201
  }
202
  assert(writer_handle.rows == TEST_LOOP_NUM);
203
204
205
  /* Test here for falling off the planet */
206
207
  /* Final Write before closing */
208
  ret= azwrite_row(&writer_handle, test_string, BUFFER_LEN);
209
  assert(ret == BUFFER_LEN);
210
211
  /* We don't use FINISH, but I want to have it tested */
212
  azflush(&writer_handle,  Z_FINISH);
213
214
  assert(writer_handle.rows == TEST_LOOP_NUM+1);
215
216
  /* Read final write */
217
  azread_init(&reader_handle);
218
  for (x= 0; x < writer_handle.rows; x++)
219
  {
220
    ret= azread_row(&reader_handle, &error);
221
    assert(ret == BUFFER_LEN);
222
    assert(!error);
223
    assert(!memcmp(reader_handle.row_ptr, test_string, ret));
224
  }
225
226
227
  azclose(&writer_handle);
228
229
230
  /* Rewind and full test */
231
  azread_init(&reader_handle);
232
  for (x= 0; x < writer_handle.rows; x++)
233
  {
234
    ret= azread_row(&reader_handle, &error);
235
    assert(ret == BUFFER_LEN);
236
    assert(!error);
237
    assert(!memcmp(reader_handle.row_ptr, test_string, ret));
238
  }
239
492.1.14 by Monty Taylor
Removed O_BINARY and FILE_BINARY.
240
  if (!(ret= azopen(&writer_handle, TEST_FILENAME, O_RDWR, method)))
1 by brian
clean slate
241
  {
242
    printf("Could not open file (%s) for appending\n", TEST_FILENAME);
243
    return 0;
244
  }
245
  ret= azwrite_row(&writer_handle, test_string, BUFFER_LEN);
246
  assert(ret == BUFFER_LEN);
247
  azflush(&writer_handle,  Z_SYNC_FLUSH);
248
  azflush(&reader_handle,  Z_SYNC_FLUSH);
249
250
  /* Rewind and full test */
251
  azread_init(&reader_handle);
252
  for (x= 0; x < writer_handle.rows; x++)
253
  {
254
    ret= azread_row(&reader_handle, &error);
255
    assert(!error);
256
    assert(ret == BUFFER_LEN);
257
    assert(!memcmp(reader_handle.row_ptr, test_string, ret));
258
  }
259
260
  /* Reader needs to be flushed to make sure it is up to date */
261
  azflush(&reader_handle,  Z_SYNC_FLUSH);
262
  assert(reader_handle.rows == 102);
263
  assert(reader_handle.auto_increment == 4);
264
  assert(reader_handle.check_point == 1829);
265
  assert(reader_handle.forced_flushes == 4);
266
  assert(reader_handle.dirty == AZ_STATE_SAVED);
267
268
  azflush(&writer_handle, Z_SYNC_FLUSH);
269
  assert(writer_handle.rows == reader_handle.rows);
270
  assert(writer_handle.auto_increment == reader_handle.auto_increment);
271
  assert(writer_handle.check_point == reader_handle.check_point);
272
  /* This is +1 because  we do a flush right before we read */
273
  assert(writer_handle.forced_flushes == reader_handle.forced_flushes + 1);
274
  assert(writer_handle.dirty == reader_handle.dirty);
275
276
  azclose(&writer_handle);
277
  azclose(&reader_handle);
278
  unlink(TEST_FILENAME);
279
280
  return 0;
281
}
282
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
283
int size_test(uint64_t length, uint64_t rows_to_test_for,
1 by brian
clean slate
284
              az_method method)
285
{
286
  azio_stream writer_handle, reader_handle;
481.1.2 by Monty Taylor
Replaced all unsigned long long with uint64_t.
287
  uint64_t write_length;
288
  uint64_t read_length;
289
  uint64_t count;
1 by brian
clean slate
290
  unsigned int ret;
291
  int error;
292
  int x;
293
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
294
  if (!(ret= azopen(&writer_handle, TEST_FILENAME,
492.1.14 by Monty Taylor
Removed O_BINARY and FILE_BINARY.
295
                    O_CREAT|O_RDWR|O_TRUNC,
1 by brian
clean slate
296
                    method)))
297
  {
298
    printf("Could not create test file\n");
299
    exit(1);
300
  }
301
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
302
  for (count= 0, write_length= 0; write_length < length ;
1 by brian
clean slate
303
       write_length+= ret)
304
  {
305
    count++;
306
    ret= azwrite_row(&writer_handle, test_string, BUFFER_LEN);
307
    if (ret != BUFFER_LEN)
308
    {
309
      printf("Size %u\n", ret);
310
      assert(ret != BUFFER_LEN);
311
    }
312
    if ((write_length % 14031) == 0)
313
    {
314
      azflush(&writer_handle,  Z_SYNC_FLUSH);
315
    }
316
  }
317
  assert(write_length == count * BUFFER_LEN); /* Number of rows time BUFFER_LEN */
318
  azflush(&writer_handle,  Z_SYNC_FLUSH);
319
492.1.14 by Monty Taylor
Removed O_BINARY and FILE_BINARY.
320
  if (!(ret= azopen(&reader_handle, TEST_FILENAME, O_RDONLY,
1 by brian
clean slate
321
                    method)))
322
  {
323
    printf("Could not open test file\n");
324
    exit(1);
325
  }
326
327
  /* We do a double loop to test speed */
328
  for (x= 0, read_length= 0; x < 2; x++, read_length= 0)
329
  {
481.1.2 by Monty Taylor
Replaced all unsigned long long with uint64_t.
330
    uint64_t count;
1 by brian
clean slate
331
332
    azread_init(&reader_handle);
333
    for (count= 0; count < writer_handle.rows; count++)
334
    {
335
      ret= azread_row(&reader_handle, &error);
336
      read_length+= ret;
337
      assert(!memcmp(reader_handle.row_ptr, test_string, ret));
338
      if (ret != BUFFER_LEN)
339
      {
340
        printf("Size %u\n", ret);
341
        assert(ret != BUFFER_LEN);
342
      }
343
    }
344
    azread_init(&reader_handle);
345
346
    assert(read_length == write_length);
347
    assert(writer_handle.rows == rows_to_test_for);
348
  }
349
  azclose(&writer_handle);
350
  azclose(&reader_handle);
351
352
  unlink(TEST_FILENAME);
353
354
  return 0;
355
}
356
357
long int timedif(struct timeval a, struct timeval b)
358
{
359
    register int us, s;
660.1.3 by Eric Herman
removed trailing whitespace with simple script:
360
1 by brian
clean slate
361
    us = a.tv_usec - b.tv_usec;
362
    us /= 1000;
363
    s = a.tv_sec - b.tv_sec;
364
    s *= 1000;
365
    return s + us;
366
}