test_dog_segment.cpp 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// Copyright (C) 2019-2020 Zilliz. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software distributed under the License
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
// or implied. See the License for the specific language governing permissions and limitations under the License.

F
FluorineDog 已提交
12
#include <gtest/gtest.h>
13 14 15 16 17 18 19

#include <iostream>
#include <string>

// #include "knowhere/index/vector_index/helpers/IndexParameter.h"
// #include "segment/SegmentReader.h"
// #include "segment/SegmentWriter.h"
F
FluorineDog 已提交
20
#include "dog_segment/SegmentBase.h"
21
// #include "utils/Json.h"
F
FluorineDog 已提交
22
#include <random>
23 24 25 26
using std::cin;
using std::cout;
using std::endl;

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
namespace {
auto
generate_data(int N) {
    std::vector<char> raw_data;
    std::vector<uint64_t> timestamps;
    std::vector<int64_t> uids;
    std::default_random_engine er(42);
    std::normal_distribution<> distribution(0.0, 1.0);
    std::default_random_engine ei(42);
    
    for (int i = 0; i < N; ++i) {
        uids.push_back(10 * N + i);
        timestamps.push_back(0);
        // append vec
        float vec[16];
        for (auto& x : vec) {
            x = distribution(er);
        }
        raw_data.insert(
                raw_data.end(), (const char*)std::begin(vec), (const char*)std::end(vec));
        int age = ei() % 100;
        raw_data.insert(
                raw_data.end(), (const char*)&age, ((const char*)&age) + sizeof(age));
    }
    return std::make_tuple(raw_data, timestamps, uids);
}
}    // namespace

55

F
FluorineDog 已提交
56
TEST(DogSegmentTest, TestABI) {
57 58 59 60 61 62
    using namespace milvus::engine;
    using namespace milvus::dog_segment;
    ASSERT_EQ(TestABI(), 42);
    assert(true);
}

63 64 65 66 67 68 69 70 71 72 73 74 75 76
TEST(DogSegmentTest, NormalDistributionTest) {
    using namespace milvus::dog_segment;
    using namespace milvus::engine;
    auto schema = std::make_shared<Schema>();
    schema->AddField("fakevec", DataType::VECTOR_FLOAT, 16);
    schema->AddField("age", DataType::INT32);
    int N = 1000* 1000;
    auto [raw_data, timestamps, uids] = generate_data(N);
    auto segment = CreateSegment(schema);
    segment->PreInsert(N);
    segment->PreDelete(N);

}

77

F
FluorineDog 已提交
78
TEST(DogSegmentTest, MockTest) {
79 80 81 82 83 84 85
    using namespace milvus::dog_segment;
    using namespace milvus::engine;
    auto schema = std::make_shared<Schema>();
    schema->AddField("fakevec", DataType::VECTOR_FLOAT, 16);
    schema->AddField("age", DataType::INT32);
    std::vector<char> raw_data;
    std::vector<Timestamp> timestamps;
F
FluorineDog 已提交
86
    std::vector<int64_t> uids;
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
    int N = 10000;
    std::default_random_engine e(67);
    for(int i = 0; i < N; ++i) {
        uids.push_back(100000 + i);
        timestamps.push_back(0);
        // append vec
        float vec[16];
        for(auto &x: vec) {
            x = e() % 2000 * 0.001 - 1.0;
        }
        raw_data.insert(raw_data.end(), (const char*)std::begin(vec), (const char*)std::end(vec));
        int age = e() % 100;
        raw_data.insert(raw_data.end(), (const char*)&age, ((const char*)&age) + sizeof(age));
    }
    auto line_sizeof = (sizeof(int) + sizeof(float) * 16);
    assert(raw_data.size() == line_sizeof * N);

F
FluorineDog 已提交
104 105

    // auto index_meta = std::make_shared<IndexMeta>(schema);
106
    auto segment = CreateSegment(schema);
F
FluorineDog 已提交
107

108
    DogDataChunk data_chunk{raw_data.data(), (int)line_sizeof, N};
F
FluorineDog 已提交
109 110
    auto offset = segment->PreInsert(N);
    segment->Insert(offset, N, uids.data(), timestamps.data(), data_chunk);
111
    QueryResult query_result;
F
FluorineDog 已提交
112 113 114
//    segment->Query(nullptr, 0, query_result);
    segment->Close();
//    segment->BuildIndex();
115 116 117 118
    int i = 0;
    i++;
}