util_test.cpp 8.9 KB
Newer Older
G
groot 已提交
1 2 3 4 5 6
////////////////////////////////////////////////////////////////////////////////
// Copyright 上海赜睿信息科技有限公司(Zilliz) - All Rights Reserved
// Unauthorized copying of this file, via any medium is strictly prohibited.
// Proprietary and confidential.
////////////////////////////////////////////////////////////////////////////////
#include <gtest/gtest.h>
G
groot 已提交
7
#include <thread>
G
groot 已提交
8
#include <easylogging++.h>
G
groot 已提交
9

G
groot 已提交
10 11
#include "utils/CommonUtil.h"
#include "utils/Error.h"
G
groot 已提交
12
#include "utils/StringHelpFunctions.h"
G
groot 已提交
13 14
#include "utils/TimeRecorder.h"
#include "utils/BlockingQueue.h"
G
groot 已提交
15
#include "utils/LogUtil.h"
G
groot 已提交
16 17
#include "utils/ValidationUtil.h"
#include "db/engine/ExecutionEngine.h"
G
groot 已提交
18

J
jinhai 已提交
19
using namespace zilliz::milvus;
G
groot 已提交
20

G
groot 已提交
21
namespace {
G
groot 已提交
22

J
jinhai 已提交
23
static const char* LOG_FILE_PATH = "./milvus/conf/log_config.conf";
G
groot 已提交
24

G
groot 已提交
25 26
}

G
groot 已提交
27 28
TEST(UtilTest, EXCEPTION_TEST) {
    std::string err_msg = "failed";
G
groot 已提交
29 30
    server::ServerException ex(SERVER_UNEXPECTED_ERROR, err_msg);
    ASSERT_EQ(ex.error_code(), SERVER_UNEXPECTED_ERROR);
G
groot 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44
    std::string msg = ex.what();
    ASSERT_EQ(msg, err_msg);
}

TEST(UtilTest, COMMON_TEST) {
    unsigned long total_mem = 0, free_mem = 0;
    server::CommonUtil::GetSystemMemInfo(total_mem, free_mem);
    ASSERT_GT(total_mem, 0);
    ASSERT_GT(free_mem, 0);

    unsigned int thread_cnt = 0;
    server::CommonUtil::GetSystemAvailableThreads(thread_cnt);
    ASSERT_GT(thread_cnt, 0);

G
groot 已提交
45 46 47
    std::string path1 = "/tmp/milvus_test/";
    std::string path2 = path1 + "common_test_12345/";
    std::string path3 = path2 + "abcdef";
G
groot 已提交
48 49
    ErrorCode err = server::CommonUtil::CreateDirectory(path3);
    ASSERT_EQ(err, SERVER_SUCCESS);
G
groot 已提交
50 51
    //test again
    err = server::CommonUtil::CreateDirectory(path3);
G
groot 已提交
52
    ASSERT_EQ(err, SERVER_SUCCESS);
G
groot 已提交
53

G
groot 已提交
54
    ASSERT_TRUE(server::CommonUtil::IsDirectoryExist(path3));
G
groot 已提交
55

G
groot 已提交
56
    err = server::CommonUtil::DeleteDirectory(path1);
G
groot 已提交
57
    ASSERT_EQ(err, SERVER_SUCCESS);
G
groot 已提交
58
    //test again
G
groot 已提交
59
    err = server::CommonUtil::DeleteDirectory(path1);
G
groot 已提交
60
    ASSERT_EQ(err, SERVER_SUCCESS);
G
groot 已提交
61

G
groot 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
    ASSERT_FALSE(server::CommonUtil::IsDirectoryExist(path1));
    ASSERT_FALSE(server::CommonUtil::IsFileExist(path1));

    std::string exe_path = server::CommonUtil::GetExePath();
    ASSERT_FALSE(exe_path.empty());

    time_t tt;
    time( &tt );
    tm time_struct;
    memset(&time_struct, 0, sizeof(tm));
    server::CommonUtil::ConvertTime(tt, time_struct);
    ASSERT_GT(time_struct.tm_year, 0);
    ASSERT_GT(time_struct.tm_mon, 0);
    ASSERT_GT(time_struct.tm_mday, 0);
    server::CommonUtil::ConvertTime(time_struct, tt);
    ASSERT_GT(tt, 0);

    bool res = server::CommonUtil::TimeStrToTime("2019-03-23", tt, time_struct);
    ASSERT_EQ(time_struct.tm_year, 119);
    ASSERT_EQ(time_struct.tm_mon, 2);
    ASSERT_EQ(time_struct.tm_mday, 23);
    ASSERT_GT(tt, 0);
    ASSERT_TRUE(res);
G
groot 已提交
85 86 87 88 89 90 91
}

TEST(UtilTest, STRINGFUNCTIONS_TEST) {
    std::string str = " test zilliz";
    server::StringHelpFunctions::TrimStringBlank(str);
    ASSERT_EQ(str, "test zilliz");

G
groot 已提交
92 93 94 95
    str = "\"test zilliz\"";
    server::StringHelpFunctions::TrimStringQuote(str, "\"");
    ASSERT_EQ(str, "test zilliz");

G
groot 已提交
96 97
    str = "a,b,c";
    std::vector<std::string> result;
G
groot 已提交
98 99
    ErrorCode err = server::StringHelpFunctions::SplitStringByDelimeter(str , ",", result);
    ASSERT_EQ(err, SERVER_SUCCESS);
G
groot 已提交
100 101 102 103
    ASSERT_EQ(result.size(), 3UL);

    result.clear();
    err = server::StringHelpFunctions::SplitStringByQuote(str , ",", "\"", result);
G
groot 已提交
104
    ASSERT_EQ(err, SERVER_SUCCESS);
G
groot 已提交
105 106 107 108
    ASSERT_EQ(result.size(), 3UL);

    result.clear();
    err = server::StringHelpFunctions::SplitStringByQuote(str , ",", "", result);
G
groot 已提交
109
    ASSERT_EQ(err, SERVER_SUCCESS);
G
groot 已提交
110 111 112 113
    ASSERT_EQ(result.size(), 3UL);

    str = "55,\"aa,gg,yy\",b";
    result.clear();
G
groot 已提交
114
    err = server::StringHelpFunctions::SplitStringByQuote(str , ",", "\"", result);
G
groot 已提交
115
    ASSERT_EQ(err, SERVER_SUCCESS);
G
groot 已提交
116
    ASSERT_EQ(result.size(), 3UL);
G
groot 已提交
117 118


G
groot 已提交
119 120 121 122 123 124 125 126 127 128 129
}

TEST(UtilTest, BLOCKINGQUEUE_TEST) {
    server::BlockingQueue<std::string> bq;

    static const size_t count = 10;
    bq.SetCapacity(count);

    for(size_t i = 1; i <= count; i++) {
        std::string id = "No." + std::to_string(i);
        bq.Put(id);
G
groot 已提交
130 131
    }

G
groot 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144 145
    ASSERT_EQ(bq.Size(), count);
    ASSERT_FALSE(bq.Empty());

    std::string str = bq.Front();
    ASSERT_EQ(str, "No.1");

    str = bq.Back();
    ASSERT_EQ(str, "No." + std::to_string(count));

    for(size_t i = 1; i <= count; i++) {
        std::string id = "No." + std::to_string(i);
        str = bq.Take();
        ASSERT_EQ(id, str);
    }
G
groot 已提交
146 147

    ASSERT_EQ(bq.Size(), 0);
G
groot 已提交
148 149
}

G
groot 已提交
150 151 152 153
TEST(UtilTest, LOG_TEST) {
    int32_t res = server::InitLog(LOG_FILE_PATH);
    ASSERT_EQ(res, 0);

G
groot 已提交
154 155 156
    EXPECT_FALSE(el::Loggers::hasFlag(el::LoggingFlag::NewLineForContainer));
    EXPECT_FALSE(el::Loggers::hasFlag(el::LoggingFlag::LogDetailedCrashReason));

G
groot 已提交
157 158 159
    std::string fname = server::GetFileName(LOG_FILE_PATH);
    ASSERT_EQ(fname, "log_config.conf");
}
G
groot 已提交
160 161 162

TEST(UtilTest, VALIDATE_TABLENAME_TEST) {
    std::string table_name = "Normal123_";
G
groot 已提交
163 164
    ErrorCode res = server::ValidationUtil::ValidateTableName(table_name);
    ASSERT_EQ(res, SERVER_SUCCESS);
G
groot 已提交
165 166 167

    table_name = "12sds";
    res = server::ValidationUtil::ValidateTableName(table_name);
G
groot 已提交
168
    ASSERT_EQ(res, SERVER_INVALID_TABLE_NAME);
G
groot 已提交
169 170 171

    table_name = "";
    res = server::ValidationUtil::ValidateTableName(table_name);
G
groot 已提交
172
    ASSERT_EQ(res, SERVER_INVALID_TABLE_NAME);
G
groot 已提交
173 174 175

    table_name = "_asdasd";
    res = server::ValidationUtil::ValidateTableName(table_name);
G
groot 已提交
176
    ASSERT_EQ(res, SERVER_SUCCESS);
G
groot 已提交
177 178 179

    table_name = "!@#!@";
    res = server::ValidationUtil::ValidateTableName(table_name);
G
groot 已提交
180
    ASSERT_EQ(res, SERVER_INVALID_TABLE_NAME);
G
groot 已提交
181

G
groot 已提交
182
    table_name = "_!@#!@";
G
groot 已提交
183
    res = server::ValidationUtil::ValidateTableName(table_name);
G
groot 已提交
184
    ASSERT_EQ(res, SERVER_INVALID_TABLE_NAME);
G
groot 已提交
185

G
groot 已提交
186 187
    table_name = "中文";
    res = server::ValidationUtil::ValidateTableName(table_name);
G
groot 已提交
188
    ASSERT_EQ(res, SERVER_INVALID_TABLE_NAME);
G
groot 已提交
189

G
groot 已提交
190
    table_name = std::string(10000, 'a');
G
groot 已提交
191
    res = server::ValidationUtil::ValidateTableName(table_name);
G
groot 已提交
192
    ASSERT_EQ(res, SERVER_INVALID_TABLE_NAME);
G
groot 已提交
193 194 195
}

TEST(UtilTest, VALIDATE_DIMENSIONTEST) {
G
groot 已提交
196 197 198 199 200
    ASSERT_EQ(server::ValidationUtil::ValidateTableDimension(-1), SERVER_INVALID_VECTOR_DIMENSION);
    ASSERT_EQ(server::ValidationUtil::ValidateTableDimension(0), SERVER_INVALID_VECTOR_DIMENSION);
    ASSERT_EQ(server::ValidationUtil::ValidateTableDimension(16385), SERVER_INVALID_VECTOR_DIMENSION);
    ASSERT_EQ(server::ValidationUtil::ValidateTableDimension(16384), SERVER_SUCCESS);
    ASSERT_EQ(server::ValidationUtil::ValidateTableDimension(1), SERVER_SUCCESS);
G
groot 已提交
201 202
}

G
groot 已提交
203
TEST(UtilTest, VALIDATE_INDEX_TEST) {
G
groot 已提交
204
    ASSERT_EQ(server::ValidationUtil::ValidateTableIndexType((int)engine::EngineType::INVALID), SERVER_INVALID_INDEX_TYPE);
G
groot 已提交
205
    for(int i = 1; i <= (int)engine::EngineType::MAX_VALUE; i++) {
G
groot 已提交
206
        ASSERT_EQ(server::ValidationUtil::ValidateTableIndexType(i), SERVER_SUCCESS);
G
groot 已提交
207
    }
G
groot 已提交
208
    ASSERT_EQ(server::ValidationUtil::ValidateTableIndexType((int)engine::EngineType::MAX_VALUE + 1), SERVER_INVALID_INDEX_TYPE);
G
groot 已提交
209

G
groot 已提交
210 211
    ASSERT_EQ(server::ValidationUtil::ValidateTableIndexNlist(0), SERVER_INVALID_INDEX_NLIST);
    ASSERT_EQ(server::ValidationUtil::ValidateTableIndexNlist(100), SERVER_SUCCESS);
G
groot 已提交
212

G
groot 已提交
213 214
    ASSERT_EQ(server::ValidationUtil::ValidateTableIndexFileSize(0), SERVER_INVALID_INDEX_FILE_SIZE);
    ASSERT_EQ(server::ValidationUtil::ValidateTableIndexFileSize(100), SERVER_SUCCESS);
G
groot 已提交
215

G
groot 已提交
216 217 218
    ASSERT_EQ(server::ValidationUtil::ValidateTableIndexMetricType(0), SERVER_INVALID_INDEX_METRIC_TYPE);
    ASSERT_EQ(server::ValidationUtil::ValidateTableIndexMetricType(1), SERVER_SUCCESS);
    ASSERT_EQ(server::ValidationUtil::ValidateTableIndexMetricType(2), SERVER_SUCCESS);
G
groot 已提交
219 220
}

221 222
TEST(ValidationUtilTest, ValidateTopkTest) {
    engine::meta::TableSchema schema;
G
groot 已提交
223 224 225
    ASSERT_EQ(server::ValidationUtil::ValidateSearchTopk(10, schema), SERVER_SUCCESS);
    ASSERT_NE(server::ValidationUtil::ValidateSearchTopk(65536, schema), SERVER_SUCCESS);
    ASSERT_NE(server::ValidationUtil::ValidateSearchTopk(0, schema), SERVER_SUCCESS);
226 227 228 229 230
}

TEST(ValidationUtilTest, ValidateNprobeTest) {
    engine::meta::TableSchema schema;
    schema.nlist_ = 100;
G
groot 已提交
231 232 233
    ASSERT_EQ(server::ValidationUtil::ValidateSearchNprobe(10, schema), SERVER_SUCCESS);
    ASSERT_NE(server::ValidationUtil::ValidateSearchNprobe(0, schema), SERVER_SUCCESS);
    ASSERT_NE(server::ValidationUtil::ValidateSearchNprobe(101, schema), SERVER_SUCCESS);
234 235
}

G
groot 已提交
236
TEST(ValidationUtilTest, ValidateGpuTest) {
G
groot 已提交
237 238
    ASSERT_EQ(server::ValidationUtil::ValidateGpuIndex(0), SERVER_SUCCESS);
    ASSERT_NE(server::ValidationUtil::ValidateGpuIndex(100), SERVER_SUCCESS);
G
groot 已提交
239 240

    size_t memory = 0;
G
groot 已提交
241 242
    ASSERT_EQ(server::ValidationUtil::GetGpuMemory(0, memory), SERVER_SUCCESS);
    ASSERT_NE(server::ValidationUtil::GetGpuMemory(100, memory), SERVER_SUCCESS);
G
groot 已提交
243 244 245 246 247 248 249 250 251 252 253
}

TEST(UtilTest, TIMERECORDER_TEST) {
    for(int64_t log_level = 0; log_level <= 6; log_level++) {
        if(log_level == 5) {
            continue; //skip fatal
        }
        server::TimeRecorder rc("time", log_level);
        rc.RecordSection("end");
    }
}