MainClass.java 7.1 KB
Newer Older
J
JinHai-CN 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
package com;

import io.milvus.client.*;
import org.apache.commons.cli.*;
import org.apache.commons.lang3.RandomStringUtils;
import org.testng.SkipException;
import org.testng.TestNG;
import org.testng.annotations.DataProvider;
import org.testng.xml.XmlClass;
import org.testng.xml.XmlSuite;
import org.testng.xml.XmlTest;

import java.util.ArrayList;
import java.util.List;

public class MainClass {
    private static String host = "127.0.0.1";
D
del-zhenwu 已提交
18
    private static int port = 19530;
19 20
    private int index_file_size = 50;
    public int dimension = 128;
J
JinHai-CN 已提交
21 22 23 24 25

    public static void setHost(String host) {
        MainClass.host = host;
    }

D
del-zhenwu 已提交
26
    public static void setPort(int port) {
J
JinHai-CN 已提交
27 28 29 30 31 32 33 34 35
        MainClass.port = port;
    }

    @DataProvider(name="DefaultConnectArgs")
    public static Object[][] defaultConnectArgs(){
        return new Object[][]{{host, port}};
    }

    @DataProvider(name="ConnectInstance")
Z
zhenwu 已提交
36
    public Object[][] connectInstance() throws ConnectFailedException {
J
JinHai-CN 已提交
37 38 39 40 41 42
        MilvusClient client = new MilvusGrpcClient();
        ConnectParam connectParam = new ConnectParam.Builder()
                .withHost(host)
                .withPort(port)
                .build();
        client.connect(connectParam);
D
del-zhenwu 已提交
43 44
        String collectionName = RandomStringUtils.randomAlphabetic(10);
        return new Object[][]{{client, collectionName}};
J
JinHai-CN 已提交
45 46 47
    }

    @DataProvider(name="DisConnectInstance")
Z
zhenwu 已提交
48
    public Object[][] disConnectInstance() throws ConnectFailedException {
J
JinHai-CN 已提交
49 50 51 52 53 54 55 56 57 58 59 60
        // Generate connection instance
        MilvusClient client = new MilvusGrpcClient();
        ConnectParam connectParam = new ConnectParam.Builder()
                .withHost(host)
                .withPort(port)
                .build();
        client.connect(connectParam);
        try {
            client.disconnect();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
D
del-zhenwu 已提交
61 62
        String collectionName = RandomStringUtils.randomAlphabetic(10);
        return new Object[][]{{client, collectionName}};
J
JinHai-CN 已提交
63 64
    }

D
del-zhenwu 已提交
65 66 67
    @DataProvider(name="Collection")
    public Object[][] provideCollection() throws ConnectFailedException, InterruptedException {
        Object[][] collections = new Object[2][2];
68 69
        MetricType[] metricTypes = { MetricType.L2, MetricType.IP };
        for (int i = 0; i < metricTypes.length; ++i) {
D
del-zhenwu 已提交
70
            String collectionName = metricTypes[i].toString()+"_"+RandomStringUtils.randomAlphabetic(10);
J
JinHai-CN 已提交
71 72 73 74 75 76 77
            // Generate connection instance
            MilvusClient client = new MilvusGrpcClient();
            ConnectParam connectParam = new ConnectParam.Builder()
                    .withHost(host)
                    .withPort(port)
                    .build();
            client.connect(connectParam);
D
del-zhenwu 已提交
78 79 80 81 82 83 84
//            List<String> tableNames = client.showCollections().getCollectionNames();
//            for (int j = 0; j < tableNames.size(); ++j
//                 ) {
//                client.dropCollection(tableNames.get(j));
//            }
//            Thread.currentThread().sleep(2000);
            CollectionMapping cm = new CollectionMapping.Builder(collectionName, dimension)
J
JinHai-CN 已提交
85 86 87
                    .withIndexFileSize(index_file_size)
                    .withMetricType(metricTypes[i])
                    .build();
D
del-zhenwu 已提交
88
            Response res = client.createCollection(cm);
J
JinHai-CN 已提交
89 90
            if (!res.ok()) {
                System.out.println(res.getMessage());
D
del-zhenwu 已提交
91
                throw new SkipException("Collection created failed");
J
JinHai-CN 已提交
92
            }
D
del-zhenwu 已提交
93
            collections[i] = new Object[]{client, collectionName};
J
JinHai-CN 已提交
94
        }
D
del-zhenwu 已提交
95
        return collections;
J
JinHai-CN 已提交
96 97
    }

D
del-zhenwu 已提交
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
    @DataProvider(name="BinaryCollection")
    public Object[][] provideBinaryCollection() throws ConnectFailedException, InterruptedException {
        Object[][] collections = new Object[3][2];
        MetricType[] metricTypes = { MetricType.JACCARD, MetricType.HAMMING, MetricType.TANIMOTO };
        for (int i = 0; i < metricTypes.length; ++i) {
            String collectionName = metricTypes[i].toString()+"_"+RandomStringUtils.randomAlphabetic(10);
            // Generate connection instance
            MilvusClient client = new MilvusGrpcClient();
            ConnectParam connectParam = new ConnectParam.Builder()
                    .withHost(host)
                    .withPort(port)
                    .build();
            client.connect(connectParam);
//            List<String> tableNames = client.showCollections().getCollectionNames();
//            for (int j = 0; j < tableNames.size(); ++j
//            ) {
//                client.dropCollection(tableNames.get(j));
//            }
//            Thread.currentThread().sleep(2000);
            CollectionMapping cm = new CollectionMapping.Builder(collectionName, dimension)
                    .withIndexFileSize(index_file_size)
                    .withMetricType(metricTypes[i])
                    .build();
            Response res = client.createCollection(cm);
            if (!res.ok()) {
                System.out.println(res.getMessage());
                throw new SkipException("Collection created failed");
            }
            collections[i] = new Object[]{client, collectionName};
        }
        return collections;
    }


J
JinHai-CN 已提交
132 133 134 135 136 137 138 139 140 141 142 143 144
    public static void main(String[] args) {
        CommandLineParser parser = new DefaultParser();
        Options options = new Options();
        options.addOption("h", "host", true, "milvus-server hostname/ip");
        options.addOption("p", "port", true, "milvus-server port");
        try {
            CommandLine cmd = parser.parse(options, args);
            String host = cmd.getOptionValue("host");
            if (host != null) {
                setHost(host);
            }
            String port = cmd.getOptionValue("port");
            if (port != null) {
D
del-zhenwu 已提交
145
                setPort(Integer.parseInt(port));
J
JinHai-CN 已提交
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
            }
            System.out.println("Host: "+host+", Port: "+port);
        }
        catch(ParseException exp) {
            System.err.println("Parsing failed.  Reason: " + exp.getMessage() );
        }

        XmlSuite suite = new XmlSuite();
        suite.setName("TmpSuite");

        XmlTest test = new XmlTest(suite);
        test.setName("TmpTest");
        List<XmlClass> classes = new ArrayList<XmlClass>();

        classes.add(new XmlClass("com.TestPing"));
        classes.add(new XmlClass("com.TestAddVectors"));
        classes.add(new XmlClass("com.TestConnect"));
        classes.add(new XmlClass("com.TestDeleteVectors"));
        classes.add(new XmlClass("com.TestIndex"));
D
del-zhenwu 已提交
165
        classes.add(new XmlClass("com.TestCompact"));
J
JinHai-CN 已提交
166
        classes.add(new XmlClass("com.TestSearchVectors"));
D
del-zhenwu 已提交
167 168 169 170 171 172 173
        classes.add(new XmlClass("com.TestCollection"));
        classes.add(new XmlClass("com.TestCollectionCount"));
        classes.add(new XmlClass("com.TestFlush"));
        classes.add(new XmlClass("com.TestPartition"));
        classes.add(new XmlClass("com.TestGetVectorByID"));
        classes.add(new XmlClass("com.TestCollectionInfo"));
        classes.add(new XmlClass("com.TestSearchByIds"));
J
JinHai-CN 已提交
174 175 176 177 178 179 180 181 182 183 184 185

        test.setXmlClasses(classes) ;

        List<XmlSuite> suites = new ArrayList<XmlSuite>();
        suites.add(suite);
        TestNG tng = new TestNG();
        tng.setXmlSuites(suites);
        tng.run();

    }

}