~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/archive/archive_test.cc

  • Committer: Monty Taylor
  • Date: 2011-03-09 20:59:40 UTC
  • mfrom: (2226.1.14 build)
  • Revision ID: mordred@inaugust.com-20110309205940-7f5mk6zba2u7bawa
Merged Dave - Filtered Replication docs
Merged Olaf - Refactoring work
Removed archive, blackhole, filesystem_engine, blitzdb, csv and pbxt from
the tree pre-GA as we have no interest in supporting them moving forward.

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