| 1 |
// Copyright (c) 2009, Whispersoft s.r.l. |
|---|
| 2 |
// All rights reserved. |
|---|
| 3 |
// |
|---|
| 4 |
// Redistribution and use in source and binary forms, with or without |
|---|
| 5 |
// modification, are permitted provided that the following conditions are |
|---|
| 6 |
// met: |
|---|
| 7 |
// |
|---|
| 8 |
// * Redistributions of source code must retain the above copyright |
|---|
| 9 |
// notice, this list of conditions and the following disclaimer. |
|---|
| 10 |
// * Redistributions in binary form must reproduce the above |
|---|
| 11 |
// copyright notice, this list of conditions and the following disclaimer |
|---|
| 12 |
// in the documentation and/or other materials provided with the |
|---|
| 13 |
// distribution. |
|---|
| 14 |
// * Neither the name of Whispersoft s.r.l. nor the names of its |
|---|
| 15 |
// contributors may be used to endorse or promote products derived from |
|---|
| 16 |
// this software without specific prior written permission. |
|---|
| 17 |
// |
|---|
| 18 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|---|
| 19 |
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|---|
| 20 |
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|---|
| 21 |
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|---|
| 22 |
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|---|
| 23 |
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|---|
| 24 |
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|---|
| 25 |
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|---|
| 26 |
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|---|
| 27 |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 28 |
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 29 |
// |
|---|
| 30 |
// Author: Catalin Popescu |
|---|
| 31 |
|
|---|
| 32 |
// Simple tests for recordio |
|---|
| 33 |
|
|---|
| 34 |
#include "common/base/types.h" |
|---|
| 35 |
#include "common/base/log.h" |
|---|
| 36 |
#include "common/base/timer.h" |
|---|
| 37 |
#include "common/base/system.h" |
|---|
| 38 |
|
|---|
| 39 |
#include "common/io/buffer/memory_stream.h" |
|---|
| 40 |
#include "common/io/logio/recordio.h" |
|---|
| 41 |
|
|---|
| 42 |
////////////////////////////////////////////////////////////////////// |
|---|
| 43 |
|
|---|
| 44 |
DEFINE_int32(num_records, |
|---|
| 45 |
1000, |
|---|
| 46 |
"Number of records for the test"); |
|---|
| 47 |
|
|---|
| 48 |
DEFINE_int32(rand_seed, |
|---|
| 49 |
3274, |
|---|
| 50 |
"Seed the random number generator w/ this"); |
|---|
| 51 |
|
|---|
| 52 |
DEFINE_int32(block_size, |
|---|
| 53 |
16384, |
|---|
| 54 |
"Create blocks of this size"); |
|---|
| 55 |
|
|---|
| 56 |
DEFINE_int32(max_record_size, |
|---|
| 57 |
65536, |
|---|
| 58 |
"Generate records of at most this size"); |
|---|
| 59 |
|
|---|
| 60 |
DEFINE_bool(deflate, |
|---|
| 61 |
false, |
|---|
| 62 |
"Zip records for writing"); |
|---|
| 63 |
|
|---|
| 64 |
////////////////////////////////////////////////////////////////////// |
|---|
| 65 |
|
|---|
| 66 |
static unsigned int rand_seed; |
|---|
| 67 |
|
|---|
| 68 |
bool GenerateRecord(io::RecordWriter* rw, |
|---|
| 69 |
io::MemoryStream* rec, |
|---|
| 70 |
io::MemoryStream* out) { |
|---|
| 71 |
const int32 rec_size = rand_r(&rand_seed) % |
|---|
| 72 |
(FLAGS_max_record_size / sizeof(rec_size)); |
|---|
| 73 |
int32* buf = new int32[rec_size]; |
|---|
| 74 |
for ( int32 i = 0; i < rec_size; ++i ) { |
|---|
| 75 |
buf[i] = rand_r(&rand_seed); |
|---|
| 76 |
} |
|---|
| 77 |
rec->Write(buf, rec_size * sizeof(*buf)); |
|---|
| 78 |
delete [] buf; |
|---|
| 79 |
rec->MarkerSet(); |
|---|
| 80 |
bool ret = rw->AppendRecord(rec, out); |
|---|
| 81 |
rec->MarkerRestore(); |
|---|
| 82 |
return ret; |
|---|
| 83 |
} |
|---|
| 84 |
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
int main(int argc, char* argv[]) { |
|---|
| 88 |
common::Init(argc, argv); |
|---|
| 89 |
|
|---|
| 90 |
rand_seed = FLAGS_rand_seed; |
|---|
| 91 |
srand(rand_seed); |
|---|
| 92 |
io::RecordWriter rw(FLAGS_block_size, FLAGS_deflate); |
|---|
| 93 |
vector<io::MemoryStream*> recs; |
|---|
| 94 |
io::MemoryStream out; |
|---|
| 95 |
|
|---|
| 96 |
for ( int32 i = 0; i < FLAGS_num_records; ++i ) { |
|---|
| 97 |
recs.push_back(new io::MemoryStream()); |
|---|
| 98 |
if ( GenerateRecord(&rw, recs.back(), &out) ) { |
|---|
| 99 |
LOG_INFO << " Another block generated @" << out.Size() |
|---|
| 100 |
<< " rec: " << i; |
|---|
| 101 |
} |
|---|
| 102 |
if ( recs.back()->IsEmpty() ) { |
|---|
| 103 |
LOG_INFO << "================ > EMPTY record at: " << i; |
|---|
| 104 |
} |
|---|
| 105 |
} |
|---|
| 106 |
rw.FinalizeContent(&out); |
|---|
| 107 |
LOG_INFO << " Final records size: " << out.Size(); |
|---|
| 108 |
|
|---|
| 109 |
io::RecordReader rd(FLAGS_block_size); |
|---|
| 110 |
io::MemoryStream in; |
|---|
| 111 |
io::MemoryStream crt; |
|---|
| 112 |
int32 rec_id = 0; |
|---|
| 113 |
while ( !out.IsEmpty() ) { |
|---|
| 114 |
int32 to_pull = min(rand_r(&rand_seed) % 2048, out.Size()); |
|---|
| 115 |
in.AppendStream(&out, to_pull); |
|---|
| 116 |
io::RecordReader::ReadResult err; |
|---|
| 117 |
do { |
|---|
| 118 |
err = rd.ReadRecord(&in, &crt); |
|---|
| 119 |
if ( err == io::RecordReader::READ_OK ) { |
|---|
| 120 |
CHECK_EQ(crt.Size(), recs[rec_id]->Size()) << " @ record: " << rec_id; |
|---|
| 121 |
string s1, s2; |
|---|
| 122 |
crt.ReadString(&s1); |
|---|
| 123 |
recs[rec_id]->ReadString(&s2); |
|---|
| 124 |
CHECK_EQ(s1, s2) << " @ record: " << rec_id; |
|---|
| 125 |
delete recs[rec_id]; |
|---|
| 126 |
++rec_id; |
|---|
| 127 |
} |
|---|
| 128 |
CHECK(err <= io::RecordReader::READ_NO_DATA) << err << " @ " << rec_id; |
|---|
| 129 |
} while ( err == io::RecordReader::READ_OK ); |
|---|
| 130 |
} |
|---|
| 131 |
CHECK_EQ(rec_id, recs.size()); |
|---|
| 132 |
|
|---|
| 133 |
printf("PASS\n"); |
|---|
| 134 |
} |
|---|