Generates num strings between min_len and max_len. Outputs the uncompressed/compressed/sorted_compressed sizes.
| 45 | // Generates num strings between min_len and max_len. |
| 46 | // Outputs the uncompressed/compressed/sorted_compressed sizes. |
| 47 | void TestCompression(int num, int min_len, int max_len, THdfsCompression::type format) { |
| 48 | vector<string> strings; |
| 49 | uint8_t* buffer = (uint8_t*)malloc(max_len * num); |
| 50 | int offset = 0; |
| 51 | int len_delta = max_len - min_len; |
| 52 | len_delta = max(len_delta, 1); |
| 53 | for (int i = 0; i < num; ++i) { |
| 54 | int len = rand() % len_delta + min_len; |
| 55 | int start = offset; |
| 56 | for (int j = 0; j < len; ++j) { |
| 57 | buffer[offset++] = rand() % 26 + 'a'; |
| 58 | } |
| 59 | strings.push_back(string((char*)buffer + start, len)); |
| 60 | } |
| 61 | |
| 62 | // Sort the input and make a new buffer |
| 63 | uint8_t* sorted_buffer = (uint8_t*)malloc(offset); |
| 64 | int sorted_offset = 0; |
| 65 | sort(strings.begin(), strings.end()); |
| 66 | for (int i = 0; i < strings.size(); ++i) { |
| 67 | memcpy(sorted_buffer + sorted_offset, strings[i].data(), strings[i].size()); |
| 68 | sorted_offset += strings[i].size(); |
| 69 | } |
| 70 | |
| 71 | scoped_ptr<Codec> compressor; |
| 72 | Codec::CodecInfo codec_info(format); |
| 73 | Status status = Codec::CreateCompressor(NULL, false, codec_info, &compressor); |
| 74 | DCHECK(status.ok()); |
| 75 | |
| 76 | int64_t compressed_len = compressor->MaxOutputLen(offset); |
| 77 | uint8_t* compressed_buffer = (uint8_t*)malloc(compressed_len); |
| 78 | ABORT_IF_ERROR( |
| 79 | compressor->ProcessBlock(true, offset, buffer, &compressed_len, &compressed_buffer)); |
| 80 | |
| 81 | int64_t sorted_compressed_len = compressor->MaxOutputLen(offset); |
| 82 | uint8_t* sorted_compressed_buffer = (uint8_t*)malloc(sorted_compressed_len); |
| 83 | ABORT_IF_ERROR(compressor->ProcessBlock(true, offset, sorted_buffer, |
| 84 | &sorted_compressed_len, &sorted_compressed_buffer)); |
| 85 | |
| 86 | cout << "NumStrings=" << num << " MinLen=" << min_len << " MaxLen=" << max_len |
| 87 | << " Codec=" << codec_info.format_ << endl; |
| 88 | cout << " Uncompressed len: " << offset << endl; |
| 89 | cout << " Compressed len: " << compressed_len << endl; |
| 90 | cout << " Sorted Compressed len: " << sorted_compressed_len << endl; |
| 91 | |
| 92 | compressor->Close(); |
| 93 | free(buffer); |
| 94 | free(compressed_buffer); |
| 95 | free(sorted_buffer); |
| 96 | free(sorted_compressed_buffer); |
| 97 | } |
| 98 | |
| 99 | } |
| 100 |