test_table_count.py 14.9 KB
Newer Older
J
JinHai-CN 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
import random
import pdb

import pytest
import logging
import itertools

from time import sleep
from multiprocessing import Process
from milvus import Milvus
from utils import *
from milvus import IndexType, MetricType

dim = 128
index_file_size = 10
Z
zhenwu 已提交
16 17
add_time_interval = 3
tag = "1970-01-01"
J
JinHai-CN 已提交
18 19 20 21 22 23 24 25

class TestTableCount:
    """
    params means different nb, the nb value may trigger merge, or not
    """
    @pytest.fixture(
        scope="function",
        params=[
Z
zhenwu 已提交
26
            1,
J
JinHai-CN 已提交
27 28 29 30 31 32 33 34 35 36 37 38
            5000,
            100000,
        ],
    )
    def add_vectors_nb(self, request):
        yield request.param

    """
    generate valid create_index params
    """
    @pytest.fixture(
        scope="function",
Z
zhenwu 已提交
39
        params=gen_simple_index_params()
J
JinHai-CN 已提交
40
    )
Z
zhenwu 已提交
41
    def get_simple_index_params(self, request, args):
42
        if "internal" not in args:
43 44 45
            if request.param["index_type"] == IndexType.IVF_SQ8H:
                pytest.skip("sq8h not support in open source")
        return request.param
J
JinHai-CN 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60

    def test_table_rows_count(self, connect, table, add_vectors_nb):
        '''
        target: test table rows_count is correct or not
        method: create table and add vectors in it,
            assert the value returned by get_table_row_count method is equal to length of vectors
        expected: the count is equal to the length of vectors
        '''
        nb = add_vectors_nb
        vectors = gen_vectors(nb, dim)
        res = connect.add_vectors(table_name=table, records=vectors)
        time.sleep(add_time_interval)
        status, res = connect.get_table_row_count(table)
        assert res == nb

Z
zhenwu 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
    def test_table_rows_count_partition(self, connect, table, add_vectors_nb):
        '''
        target: test table rows_count is correct or not
        method: create table, create partition and add vectors in it,
            assert the value returned by get_table_row_count method is equal to length of vectors
        expected: the count is equal to the length of vectors
        '''
        nb = add_vectors_nb
        partition_name = gen_unique_str()
        vectors = gen_vectors(nb, dim)
        status = connect.create_partition(table, partition_name, tag)
        assert status.OK()
        res = connect.add_vectors(table_name=table, records=vectors, partition_tag=tag)
        time.sleep(add_time_interval)
        status, res = connect.get_table_row_count(table)
        assert res == nb

    def test_table_rows_count_multi_partitions_A(self, connect, table, add_vectors_nb):
        '''
        target: test table rows_count is correct or not
        method: create table, create partitions and add vectors in it,
            assert the value returned by get_table_row_count method is equal to length of vectors
        expected: the count is equal to the length of vectors
        '''
        new_tag = "new_tag"
        nb = add_vectors_nb
        partition_name = gen_unique_str()
        new_partition_name = gen_unique_str()
        vectors = gen_vectors(nb, dim)
        status = connect.create_partition(table, partition_name, tag)
        status = connect.create_partition(table, new_partition_name, new_tag)
        assert status.OK()
        res = connect.add_vectors(table_name=table, records=vectors)
        time.sleep(add_time_interval)
        status, res = connect.get_table_row_count(table)
        assert res == nb

    def test_table_rows_count_multi_partitions_B(self, connect, table, add_vectors_nb):
        '''
        target: test table rows_count is correct or not
        method: create table, create partitions and add vectors in one of the partitions,
            assert the value returned by get_table_row_count method is equal to length of vectors
        expected: the count is equal to the length of vectors
        '''
        new_tag = "new_tag"
        nb = add_vectors_nb
        partition_name = gen_unique_str()
        new_partition_name = gen_unique_str()
        vectors = gen_vectors(nb, dim)
        status = connect.create_partition(table, partition_name, tag)
        status = connect.create_partition(table, new_partition_name, new_tag)
        assert status.OK()
        res = connect.add_vectors(table_name=table, records=vectors, partition_tag=tag)
        time.sleep(add_time_interval)
        status, res = connect.get_table_row_count(partition_name)
        assert res == nb
        status, res = connect.get_table_row_count(new_partition_name)
        assert res == 0

    def test_table_rows_count_multi_partitions_C(self, connect, table, add_vectors_nb):
        '''
        target: test table rows_count is correct or not
        method: create table, create partitions and add vectors in one of the partitions,
            assert the value returned by get_table_row_count method is equal to length of vectors
        expected: the table count is equal to the length of vectors
        '''
        new_tag = "new_tag"
        nb = add_vectors_nb
        partition_name = gen_unique_str()
        new_partition_name = gen_unique_str()
        vectors = gen_vectors(nb, dim)
        status = connect.create_partition(table, partition_name, tag)
        status = connect.create_partition(table, new_partition_name, new_tag)
        assert status.OK()
        res = connect.add_vectors(table_name=table, records=vectors, partition_tag=tag)
        res = connect.add_vectors(table_name=table, records=vectors, partition_tag=new_tag)
        time.sleep(add_time_interval)
        status, res = connect.get_table_row_count(partition_name)
        assert res == nb
        status, res = connect.get_table_row_count(new_partition_name)
        assert res == nb
        status, res = connect.get_table_row_count(table)
        assert res == nb * 2

Z
zhenwu 已提交
145
    def test_table_rows_count_after_index_created(self, connect, table, get_simple_index_params):
J
JinHai-CN 已提交
146 147 148 149 150 151
        '''
        target: test get_table_row_count, after index have been created
        method: add vectors in db, and create index, then calling get_table_row_count with correct params 
        expected: get_table_row_count raise exception
        '''
        nb = 100
Z
zhenwu 已提交
152
        index_params = get_simple_index_params
J
JinHai-CN 已提交
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
        vectors = gen_vectors(nb, dim)
        res = connect.add_vectors(table_name=table, records=vectors)
        time.sleep(add_time_interval)
        # logging.getLogger().info(index_params)
        connect.create_index(table, index_params)
        status, res = connect.get_table_row_count(table)
        assert res == nb

    @pytest.mark.level(2)
    def test_count_without_connection(self, table, dis_connect):
        '''
        target: test get_table_row_count, without connection
        method: calling get_table_row_count with correct params, with a disconnected instance
        expected: get_table_row_count raise exception
        '''
        with pytest.raises(Exception) as e:
            status = dis_connect.get_table_row_count(table)

    def test_table_rows_count_no_vectors(self, connect, table):
        '''
        target: test table rows_count is correct or not, if table is empty
        method: create table and no vectors in it,
            assert the value returned by get_table_row_count method is equal to 0
        expected: the count is equal to 0
        '''
Z
zhenwu 已提交
178
        table_name = gen_unique_str()
J
JinHai-CN 已提交
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
        param = {'table_name': table_name,
                 'dimension': dim,
                 'index_file_size': index_file_size}
        connect.create_table(param)        
        status, res = connect.get_table_row_count(table)
        assert res == 0

    # TODO: enable
    @pytest.mark.level(2)
    @pytest.mark.timeout(20)
    def _test_table_rows_count_multiprocessing(self, connect, table, args):
        '''
        target: test table rows_count is correct or not with multiprocess
        method: create table and add vectors in it,
            assert the value returned by get_table_row_count method is equal to length of vectors
        expected: the count is equal to the length of vectors
        '''
        nq = 2
        uri = "tcp://%s:%s" % (args["ip"], args["port"])
        vectors = gen_vectors(nq, dim)
        res = connect.add_vectors(table_name=table, records=vectors)
        time.sleep(add_time_interval)

        def rows_count(milvus):
            status, res = milvus.get_table_row_count(table)
            logging.getLogger().info(status)
            assert res == nq

        process_num = 8
        processes = []
        for i in range(process_num):
            milvus = Milvus()
            milvus.connect(uri=uri)
            p = Process(target=rows_count, args=(milvus, ))
            processes.append(p)
            p.start()
            logging.getLogger().info(p)
        for p in processes:
            p.join()

    def test_table_rows_count_multi_tables(self, connect):
        '''
        target: test table rows_count is correct or not with multiple tables of L2
        method: create table and add vectors in it,
            assert the value returned by get_table_row_count method is equal to length of vectors
        expected: the count is equal to the length of vectors
        '''
        nq = 100
        vectors = gen_vectors(nq, dim)
        table_list = []
Z
zhenwu 已提交
229 230
        for i in range(20):
            table_name = gen_unique_str()
J
JinHai-CN 已提交
231 232 233 234 235 236 237 238
            table_list.append(table_name)
            param = {'table_name': table_name,
                     'dimension': dim,
                     'index_file_size': index_file_size,
                     'metric_type': MetricType.L2}
            connect.create_table(param)
            res = connect.add_vectors(table_name=table_name, records=vectors)
        time.sleep(2)
Z
zhenwu 已提交
239
        for i in range(20):
J
JinHai-CN 已提交
240 241 242 243 244 245 246 247 248 249 250 251 252
            status, res = connect.get_table_row_count(table_list[i])
            assert status.OK()
            assert res == nq


class TestTableCountIP:
    """
    params means different nb, the nb value may trigger merge, or not
    """

    @pytest.fixture(
        scope="function",
        params=[
Z
zhenwu 已提交
253
            1,
J
JinHai-CN 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266
            5000,
            100000,
        ],
    )
    def add_vectors_nb(self, request):
        yield request.param

    """
    generate valid create_index params
    """

    @pytest.fixture(
        scope="function",
Z
zhenwu 已提交
267
        params=gen_simple_index_params()
J
JinHai-CN 已提交
268
    )
Z
zhenwu 已提交
269
    def get_simple_index_params(self, request, args):
270
        if "internal" not in args:
271 272 273
            if request.param["index_type"] == IndexType.IVF_SQ8H:
                pytest.skip("sq8h not support in open source")
        return request.param
J
JinHai-CN 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288

    def test_table_rows_count(self, connect, ip_table, add_vectors_nb):
        '''
        target: test table rows_count is correct or not
        method: create table and add vectors in it,
            assert the value returned by get_table_row_count method is equal to length of vectors
        expected: the count is equal to the length of vectors
        '''
        nb = add_vectors_nb
        vectors = gen_vectors(nb, dim)
        res = connect.add_vectors(table_name=ip_table, records=vectors)
        time.sleep(add_time_interval)
        status, res = connect.get_table_row_count(ip_table)
        assert res == nb

Z
zhenwu 已提交
289
    def test_table_rows_count_after_index_created(self, connect, ip_table, get_simple_index_params):
J
JinHai-CN 已提交
290 291 292 293 294 295
        '''
        target: test get_table_row_count, after index have been created
        method: add vectors in db, and create index, then calling get_table_row_count with correct params
        expected: get_table_row_count raise exception
        '''
        nb = 100
Z
zhenwu 已提交
296
        index_params = get_simple_index_params
J
JinHai-CN 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329
        vectors = gen_vectors(nb, dim)
        res = connect.add_vectors(table_name=ip_table, records=vectors)
        time.sleep(add_time_interval)
        # logging.getLogger().info(index_params)
        connect.create_index(ip_table, index_params)
        status, res = connect.get_table_row_count(ip_table)
        assert res == nb

    @pytest.mark.level(2)
    def test_count_without_connection(self, ip_table, dis_connect):
        '''
        target: test get_table_row_count, without connection
        method: calling get_table_row_count with correct params, with a disconnected instance
        expected: get_table_row_count raise exception
        '''
        with pytest.raises(Exception) as e:
            status = dis_connect.get_table_row_count(ip_table)

    def test_table_rows_count_no_vectors(self, connect, ip_table):
        '''
        target: test table rows_count is correct or not, if table is empty
        method: create table and no vectors in it,
            assert the value returned by get_table_row_count method is equal to 0
        expected: the count is equal to 0
        '''
        table_name = gen_unique_str("test_table")
        param = {'table_name': table_name,
                 'dimension': dim,
                 'index_file_size': index_file_size}
        connect.create_table(param)
        status, res = connect.get_table_row_count(ip_table)
        assert res == 0

Z
zhenwu 已提交
330
    # TODO: enable
Z
zhenwu 已提交
331
    @pytest.mark.timeout(60)
Z
zhenwu 已提交
332
    def _test_table_rows_count_multiprocessing(self, connect, ip_table, args):
J
JinHai-CN 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
        '''
        target: test table rows_count is correct or not with multiprocess
        method: create table and add vectors in it,
            assert the value returned by get_table_row_count method is equal to length of vectors
        expected: the count is equal to the length of vectors
        '''
        nq = 2
        uri = "tcp://%s:%s" % (args["ip"], args["port"])
        vectors = gen_vectors(nq, dim)
        res = connect.add_vectors(table_name=ip_table, records=vectors)
        time.sleep(add_time_interval)

        def rows_count(milvus):
            status, res = milvus.get_table_row_count(ip_table)
            logging.getLogger().info(status)
            assert res == nq

        process_num = 8
        processes = []
        for i in range(process_num):
            milvus = Milvus()
            milvus.connect(uri=uri)
            p = Process(target=rows_count, args=(milvus,))
            processes.append(p)
            p.start()
            logging.getLogger().info(p)
        for p in processes:
            p.join()

    def test_table_rows_count_multi_tables(self, connect):
        '''
        target: test table rows_count is correct or not with multiple tables of IP
        method: create table and add vectors in it,
            assert the value returned by get_table_row_count method is equal to length of vectors
        expected: the count is equal to the length of vectors
        '''
        nq = 100
        vectors = gen_vectors(nq, dim)
        table_list = []
Z
zhenwu 已提交
372
        for i in range(20):
J
JinHai-CN 已提交
373 374 375 376 377 378 379 380 381
            table_name = gen_unique_str('test_table_rows_count_multi_tables')
            table_list.append(table_name)
            param = {'table_name': table_name,
                     'dimension': dim,
                     'index_file_size': index_file_size,
                     'metric_type': MetricType.IP}
            connect.create_table(param)
            res = connect.add_vectors(table_name=table_name, records=vectors)
        time.sleep(2)
Z
zhenwu 已提交
382
        for i in range(20):
J
JinHai-CN 已提交
383 384 385
            status, res = connect.get_table_row_count(table_list[i])
            assert status.OK()
            assert res == nq