增加测试过程熟悉clickhouse源码

举报
陈方业 发表于 2021/12/25 15:28:13 2021/12/25
【摘要】 在分析clickhouse内存分配部分代码时,想深入了解clickhouse内存池,于是写一些代码进行测试

增加测试过程熟悉clickhouse源码

在分析clickhouse内存分配部分代码时,想深入了解clickhouse内存池,于是写一些代码进行测试,参考代码:

ClickHouse_Kernel_Tag_v21.3.4.25-lts/src/Common/tests/arena_with_free_lists.cpp

同级目录下增加测试CPP:read_bin_file_cfytest.cpp

ClickHouse_Kernel_Tag_v21.3.4.25-lts/src/Common/tests/CMakeLists.txt文件中增加如下指令:

add_executable (arena_with_free_lists arena_with_free_lists.cpp)
target_link_libraries (arena_with_free_lists PRIVATE dbms)

编译生成二进制文件:ClickHouse_Kernel_21.3.x/build/src/Common/tests/read_bin_file_cfytest

cmake .. -DCMAKE_BUILD_TYPE="Debug" -DENABLE_TESTS=ON

截取源码arena_with_free_lists.cpp中的部分代码做测试:

#include <variant>
#include <memory>
#include <array>
#include <sys/resource.h>
#include <ext/bit_cast.h>
#include <ext/size.h>
#include <Common/Arena.h>
#include <Common/ArenaWithFreeLists.h>
#include <common/StringRef.h>
#include <Common/Stopwatch.h>
#include <IO/ReadBufferFromFileDescriptor.h>
#include <IO/ReadHelpers.h>
#include <Core/Field.h>
#include <Compression/CompressedReadBuffer.h>

using namespace DB;

namespace DB
{
    namespace ErrorCodes
    {
        extern const int SYSTEM_ERROR;
    }
}

int main(int argc, char ** argv)
{
    if (argc < 2)
    {
        std::cerr << "Usage: program n\n";
        return 1;
    }

    std::cerr << std::fixed << std::setprecision(2);

    size_t n = parse<size_t>(argv[1]);
    std::vector<std::string> data;  //  如果获取datetime类型,修改成time_t
    size_t sum_strings_size = 0;

    // string类型字段的bin文件读取
    {
        Stopwatch watch;
        DB::ReadBufferFromFileDescriptor in1(STDIN_FILENO);
        DB::CompressedReadBuffer in2(in1);

        for (size_t i = 0; i < n && !in2.eof(); ++i)
        {
            data.emplace_back();
            readStringBinary(data.back(), in2);  // 如果获取datetime类型,修改成readDateTimeText(data.back(), in2);
            // 打印列的每行数据到终端
            std::cerr << "data :" << data.back() << std::endl;
            sum_strings_size += data.back().size() + 1;
        }

        watch.stop();
        std::cerr
            << "Read. Elements: " << data.size() << ", bytes: " << sum_strings_size
            << ", elapsed: " << watch.elapsedSeconds()
            << " (" << data.size() / watch.elapsedSeconds() << " elem/sec.,"
            << " " << sum_strings_size / 1048576.0 / watch.elapsedSeconds() << " MiB/sec.)"
            << std::endl;

        rusage resource_usage;
        if (0 != getrusage(RUSAGE_SELF, &resource_usage))
            throwFromErrno("Cannot getrusage", ErrorCodes::SYSTEM_ERROR);

        size_t allocated_bytes = resource_usage.ru_maxrss * 1024;
        std::cerr << "Current memory usage: " << allocated_bytes << " bytes.\n";
    }
}

测试读取query_log表的query字段:

# ./read_bin_file_cfytest 5 < /home/cfy/var/lib/clickhouse/store/cc0/cc09aa5b-5680-4e91-b8ed-0f51449e2eb9/202112_162_975_82/query.bin 
data :select timezone()
FORMAT TabSeparatedWithNamesAndTypes;
data :select timezone()
FORMAT TabSeparatedWithNamesAndTypes;
data :select timezone()
FORMAT TabSeparatedWithNamesAndTypes;
data :select timezone()
FORMAT TabSeparatedWithNamesAndTypes;
data :select version()
FORMAT TabSeparatedWithNamesAndTypes;
Read. Elements: 5, bytes: 279, elapsed: 0.00 (5380.14 elem/sec., 0.29 MiB/sec.)
Current memory usage: 40493056 bytes.
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。