~linuxjedi/drizzle/trunk-bug-667053

« back to all changes in this revision

Viewing changes to storage/archive/archive_test.c

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

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