~ubuntu-branches/ubuntu/trusty/drizzle/trusty

« back to all changes in this revision

Viewing changes to plugin/archive/archive_test.cc

  • Committer: Bazaar Package Importer
  • Author(s): Monty Taylor
  • Date: 2010-03-18 12:12:31 UTC
  • Revision ID: james.westby@ubuntu.com-20100318121231-k6g1xe6cshbwa0f8
Tags: upstream-2010.03.1347
ImportĀ upstreamĀ versionĀ 2010.03.1347

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