aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/comptable/usage/usage.cpp
blob: 7670d1591377bfaff708e835814ddf658cfdcc8b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
#include <library/cpp/comptable/comptable.h>

#include <util/random/random.h>
#include <util/random/fast.h>

#include <time.h>
#include <stdlib.h>

using namespace NCompTable;

template <bool HQ>
void DoTest(const TCompressorTable& table, const TVector<TString>& lines) {
    TVector<char> compressed;
    TVector<char> decompressed;

    TChunkCompressor compressor(HQ, table);
    TChunkDecompressor deCompressor(HQ, table);

    size_t origSize = 0;
    size_t compSize = 0;
    float cl1 = clock();
    for (size_t i = 0; i < lines.size(); ++i) {
        const TString& line = lines[i];
        compressor.Compress(line, &compressed);
        origSize += line.size();
        compSize += compressed.size();
        TStringBuf in(compressed.data(), compressed.size());
        deCompressor.Decompress(in, &decompressed);
        if (decompressed.size() != line.size() || memcmp(decompressed.data(), line.data(), decompressed.size())) { 
            Cout << i << "\n";
            Cout << line << "\n"
                 << TString(decompressed.data(), decompressed.size()) << "\n";
            abort();
        }
    }
    float cl2 = clock();
    float secs = (cl2 - cl1) / CLOCKS_PER_SEC;
    Cout << "origSize: " << origSize << "\tcompSize: " << compSize << Endl;
    Cout << "yep! compression + decompression speed " << origSize / 1024.0f / 1024.0f / secs << " mbps\n";
    Cout << "yep! compression ratio " << double(origSize) / double(compSize + 1) << "\n";
}

int main(int argc, const char* argv[]) {
    TReallyFastRng32 rr(17);
    TVector<TString> lines;
    /*FILE *fp = fopen("res", "rb");
    while (!feof(fp)) {
        char buff[4096];
        fscanf(fp, "%s", buff);
        lines.push_back(TString(buff));
    }*/
    //for (size_t i = 0; i < 10000000; ++i) {
    //for (size_t i = 0; i < 1000000; ++i) {
    for (size_t i = 0; i < 1000000; ++i) {
        size_t size = rr.Uniform(32);
        TString res = "www.yandex.ru/yandsearch?text=";
        for (size_t j = 0; j < size; ++j) {
            res += "qwer"[rr.Uniform(4)];
        }
        lines.push_back(res);
    }
    THolder<TDataSampler> sampler(new TDataSampler);
    for (size_t i = 0; i < lines.size(); ++i) {
        sampler->AddStat(lines[i]);
    }
    TCompressorTable table;
    sampler->BuildTable(table);

    DoTest<true>(table, lines);
    DoTest<false>(table, lines);

    Y_UNUSED(argc);
    Y_UNUSED(argv);
    return 0;
}