~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
23
#define ARCHIVE_ROW_HEADER_SIZE 4
24
25
#define COMMENT_STRING "Your bases"
26
#define FRM_STRING "My bases"
27
#define TEST_FILENAME "test.az"
28
#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..."
29
#define TEST_LOOP_NUM 100
30
31
#define BUFFER_LEN 1024
32
33
char test_string[BUFFER_LEN];
34
80.1.1 by Brian Aker
LL() cleanup
35
unsigned long long row_lengths[]= {536870912LL, 2147483648LL, 4294967296LL, 8589934592LL};
36
unsigned long long row_numbers[]= {524288LL, 2097152LL, 4194304LL, 8388608LL};
1 by brian
clean slate
37
38
/* prototypes */
39
int size_test(unsigned long long length, unsigned long long rows_to_test_for, az_method method);
40
int small_test(az_method method);
41
long int timedif(struct timeval a, struct timeval b);
42
43
44
int main(int argc, char *argv[])
45
{
46
  az_method method;
47
  unsigned int x;
48
49
  if (argc > 2)
50
    return 0;
51
52
  my_init();
53
54
  MY_INIT(argv[0]);
55
56
  for (method= AZ_METHOD_BLOCK; method < AZ_METHOD_MAX; method++)
57
  {
58
    struct timeval start_time, end_time;
59
    long int timing;
60
61
    printf("Testing %d\n", (int)method);
62
    gettimeofday(&start_time, NULL);
63
    small_test(method);
64
    gettimeofday(&end_time, NULL);
65
    timing= timedif(end_time, start_time);
66
    printf("\tTime took %ld.%03ld seconds\n\n", timing / 1000, timing % 1000);
67
  }
68
69
  if (argc > 1)
70
    return 0;
71
72
  /* Start size tests */
73
  printf("About to run .5/2/4/8 gig tests now, you may want to hit CTRL-C\n");
74
  for (x= 0; x < 4; x++) /* 4 is the current size of the array we use */
75
  {
76
    for (method= AZ_METHOD_BLOCK; method < AZ_METHOD_MAX; method++)
77
    {
78
      struct timeval start_time, end_time;
79
      long int timing;
80
81
      printf("Testing %llu bytes with (%d)\n", row_lengths[x], (int)method);
82
      gettimeofday(&start_time, NULL);
83
      size_test(row_lengths[x], row_numbers[x], method);
84
      gettimeofday(&end_time, NULL);
85
      timing= timedif(end_time, start_time);
86
      printf("\tTime took %ld.%03ld seconds\n\n", timing / 1000, timing % 1000);
87
    }
88
  }
89
90
  my_end(0);
91
92
  return 0;
93
}
94
95
int small_test(az_method method)
96
{
97
  unsigned int ret;
98
  char comment_str[10];
99
100
  int error;
101
  unsigned int x;
102
  int written_rows= 0;
103
  azio_stream writer_handle, reader_handle;
104
105
  memcpy(test_string, TEST_STRING_INIT, 1024);
106
107
  unlink(TEST_FILENAME);
108
109
  if (!(ret= azopen(&writer_handle, TEST_FILENAME, O_CREAT|O_RDWR|O_BINARY,
110
                    method)))
111
  {
112
    printf("Could not create test file\n");
113
    return 0;
114
  }
115
116
  azwrite_comment(&writer_handle, (char *)COMMENT_STRING, 
117
                  (unsigned int)strlen(COMMENT_STRING));
118
  azread_comment(&writer_handle, comment_str);
119
  assert(!memcmp(COMMENT_STRING, comment_str,
120
                strlen(COMMENT_STRING)));
121
122
  azwrite_frm(&writer_handle, (char *)FRM_STRING, 
123
                  (unsigned int)strlen(FRM_STRING));
124
  azread_frm(&writer_handle, comment_str);
125
  assert(!memcmp(FRM_STRING, comment_str,
126
                strlen(FRM_STRING)));
127
128
129
  if (!(ret= azopen(&reader_handle, TEST_FILENAME, O_RDONLY|O_BINARY,
130
                    method)))
131
  {
132
    printf("Could not open test file\n");
133
    return 0;
134
  }
135
136
  assert(reader_handle.rows == 0);
137
  assert(reader_handle.auto_increment == 0);
138
  assert(reader_handle.check_point == 0);
139
  assert(reader_handle.forced_flushes == 0);
140
  assert(reader_handle.dirty == AZ_STATE_DIRTY);
141
142
  for (x= 0; x < TEST_LOOP_NUM; x++)
143
  {
144
    ret= azwrite_row(&writer_handle, test_string, BUFFER_LEN);
145
    assert(ret == BUFFER_LEN);
146
    written_rows++;
147
  }
148
  azflush(&writer_handle,  Z_SYNC_FLUSH);
149
150
  azread_comment(&writer_handle, comment_str);
151
  assert(!memcmp(COMMENT_STRING, comment_str,
152
                strlen(COMMENT_STRING)));
153
154
  /* Lets test that our internal stats are good */
155
  assert(writer_handle.rows == TEST_LOOP_NUM);
156
157
  /* Reader needs to be flushed to make sure it is up to date */
158
  azflush(&reader_handle,  Z_SYNC_FLUSH);
159
  assert(reader_handle.rows == TEST_LOOP_NUM);
160
  assert(reader_handle.auto_increment == 0);
161
  assert(reader_handle.check_point == 1269);
162
  assert(reader_handle.forced_flushes == 1);
163
  assert(reader_handle.comment_length == 10);
164
  assert(reader_handle.dirty == AZ_STATE_SAVED);
165
166
  writer_handle.auto_increment= 4;
167
  azflush(&writer_handle, Z_SYNC_FLUSH);
168
  assert(writer_handle.rows == TEST_LOOP_NUM);
169
  assert(writer_handle.auto_increment == 4);
170
  assert(writer_handle.check_point == 1269);
171
  assert(writer_handle.forced_flushes == 2);
172
  assert(writer_handle.dirty == AZ_STATE_SAVED);
173
174
  azclose(&reader_handle);
175
176
  if (!(ret= azopen(&reader_handle, TEST_FILENAME, O_RDONLY|O_BINARY,
177
                    method)))
178
  {
179
    printf("Could not open test file\n");
180
    return 0;
181
  }
182
183
184
  /* Read the original data */
185
  azread_init(&reader_handle);
186
  for (x= 0; x < writer_handle.rows; x++)
187
  {
188
    ret= azread_row(&reader_handle, &error);
189
    assert(!error);
190
    assert(ret == BUFFER_LEN);
191
    assert(!memcmp(reader_handle.row_ptr, test_string, ret));
192
  }
193
  assert(writer_handle.rows == TEST_LOOP_NUM);
194
195
196
  /* Test here for falling off the planet */
197
198
  /* Final Write before closing */
199
  ret= azwrite_row(&writer_handle, test_string, BUFFER_LEN);
200
  assert(ret == BUFFER_LEN);
201
202
  /* We don't use FINISH, but I want to have it tested */
203
  azflush(&writer_handle,  Z_FINISH);
204
205
  assert(writer_handle.rows == TEST_LOOP_NUM+1);
206
207
  /* Read final write */
208
  azread_init(&reader_handle);
209
  for (x= 0; x < writer_handle.rows; x++)
210
  {
211
    ret= azread_row(&reader_handle, &error);
212
    assert(ret == BUFFER_LEN);
213
    assert(!error);
214
    assert(!memcmp(reader_handle.row_ptr, test_string, ret));
215
  }
216
217
218
  azclose(&writer_handle);
219
220
221
  /* Rewind and full test */
222
  azread_init(&reader_handle);
223
  for (x= 0; x < writer_handle.rows; x++)
224
  {
225
    ret= azread_row(&reader_handle, &error);
226
    assert(ret == BUFFER_LEN);
227
    assert(!error);
228
    assert(!memcmp(reader_handle.row_ptr, test_string, ret));
229
  }
230
231
  if (!(ret= azopen(&writer_handle, TEST_FILENAME, O_RDWR|O_BINARY, method)))
232
  {
233
    printf("Could not open file (%s) for appending\n", TEST_FILENAME);
234
    return 0;
235
  }
236
  ret= azwrite_row(&writer_handle, test_string, BUFFER_LEN);
237
  assert(ret == BUFFER_LEN);
238
  azflush(&writer_handle,  Z_SYNC_FLUSH);
239
  azflush(&reader_handle,  Z_SYNC_FLUSH);
240
241
  /* Rewind and full test */
242
  azread_init(&reader_handle);
243
  for (x= 0; x < writer_handle.rows; x++)
244
  {
245
    ret= azread_row(&reader_handle, &error);
246
    assert(!error);
247
    assert(ret == BUFFER_LEN);
248
    assert(!memcmp(reader_handle.row_ptr, test_string, ret));
249
  }
250
251
  /* Reader needs to be flushed to make sure it is up to date */
252
  azflush(&reader_handle,  Z_SYNC_FLUSH);
253
  assert(reader_handle.rows == 102);
254
  assert(reader_handle.auto_increment == 4);
255
  assert(reader_handle.check_point == 1829);
256
  assert(reader_handle.forced_flushes == 4);
257
  assert(reader_handle.dirty == AZ_STATE_SAVED);
258
259
  azflush(&writer_handle, Z_SYNC_FLUSH);
260
  assert(writer_handle.rows == reader_handle.rows);
261
  assert(writer_handle.auto_increment == reader_handle.auto_increment);
262
  assert(writer_handle.check_point == reader_handle.check_point);
263
  /* This is +1 because  we do a flush right before we read */
264
  assert(writer_handle.forced_flushes == reader_handle.forced_flushes + 1);
265
  assert(writer_handle.dirty == reader_handle.dirty);
266
267
  azclose(&writer_handle);
268
  azclose(&reader_handle);
269
  unlink(TEST_FILENAME);
270
271
  return 0;
272
}
273
274
int size_test(unsigned long long length, unsigned long long rows_to_test_for, 
275
              az_method method)
276
{
277
  azio_stream writer_handle, reader_handle;
278
  unsigned long long write_length;
279
  unsigned long long read_length;
280
  unsigned long long count;
281
  unsigned int ret;
282
  int error;
283
  int x;
284
285
  if (!(ret= azopen(&writer_handle, TEST_FILENAME, 
286
                    O_CREAT|O_RDWR|O_TRUNC|O_BINARY,
287
                    method)))
288
  {
289
    printf("Could not create test file\n");
290
    exit(1);
291
  }
292
293
  for (count= 0, write_length= 0; write_length < length ; 
294
       write_length+= ret)
295
  {
296
    count++;
297
    ret= azwrite_row(&writer_handle, test_string, BUFFER_LEN);
298
    if (ret != BUFFER_LEN)
299
    {
300
      printf("Size %u\n", ret);
301
      assert(ret != BUFFER_LEN);
302
    }
303
    if ((write_length % 14031) == 0)
304
    {
305
      azflush(&writer_handle,  Z_SYNC_FLUSH);
306
    }
307
  }
308
  assert(write_length == count * BUFFER_LEN); /* Number of rows time BUFFER_LEN */
309
  azflush(&writer_handle,  Z_SYNC_FLUSH);
310
311
  if (!(ret= azopen(&reader_handle, TEST_FILENAME, O_RDONLY|O_BINARY,
312
                    method)))
313
  {
314
    printf("Could not open test file\n");
315
    exit(1);
316
  }
317
318
  /* We do a double loop to test speed */
319
  for (x= 0, read_length= 0; x < 2; x++, read_length= 0)
320
  {
321
    unsigned long long count;
322
323
    azread_init(&reader_handle);
324
    for (count= 0; count < writer_handle.rows; count++)
325
    {
326
      ret= azread_row(&reader_handle, &error);
327
      read_length+= ret;
328
      assert(!memcmp(reader_handle.row_ptr, test_string, ret));
329
      if (ret != BUFFER_LEN)
330
      {
331
        printf("Size %u\n", ret);
332
        assert(ret != BUFFER_LEN);
333
      }
334
    }
335
    azread_init(&reader_handle);
336
337
    assert(read_length == write_length);
338
    assert(writer_handle.rows == rows_to_test_for);
339
  }
340
  azclose(&writer_handle);
341
  azclose(&reader_handle);
342
343
  unlink(TEST_FILENAME);
344
345
  return 0;
346
}
347
348
long int timedif(struct timeval a, struct timeval b)
349
{
350
    register int us, s;
351
 
352
    us = a.tv_usec - b.tv_usec;
353
    us /= 1000;
354
    s = a.tv_sec - b.tv_sec;
355
    s *= 1000;
356
    return s + us;
357
}