Skip to content

Conversation

@Genius-pig
Copy link
Contributor

@Genius-pig Genius-pig commented Sep 9, 2020

performance (comparing to session API)

insert

rest session
144848ms 75374ms
113287ms 89804ms

request and do nothing

rest session
14300ms 910ms
12044ms 684ms

You can see the IoTDB session API faster than Restful API, but send an HTTP request in java client is somewhat costly. So it's some reason for IoTDB session API is faster. Restful API adds a process for parsing JSON, so it's also a reason.

test code

package org.apache.iotdb.session;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.apache.iotdb.db.http.constant.HttpConstant;
import org.apache.iotdb.rpc.IoTDBConnectionException;
import org.apache.iotdb.rpc.StatementExecutionException;
import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
import org.junit.Assert;
import org.junit.Test;

public class IoTDBHttpBenchMark {

  private Client client = ClientBuilder.newClient();
  private static final String QUERY_URI
      = "http://localhost:8282/query";
  private static final String INSERT_URI
      = "http://localhost:8282/insert";
  private Session session;
  private static final String USER_LOGIN
      = "http://localhost:8282/user/login?username=root&password=root";
  private static final String SUCCESSFUL_RESPONSE = "{\"result\":\"successful operation\"}";
  private static final String URI = "http://localhost:8282/";

  @Test
  public void insertHttp() {
    login();
    long start = System.currentTimeMillis();
    for (int i = 0; i < 5000; i++) {
      JSONArray inserts = insertJsonExample(i);
      client.target(INSERT_URI).request(MediaType.APPLICATION_JSON).post(Entity.entity(inserts, MediaType.APPLICATION_JSON));
//      JSONObject query = queryJsonExample(i);
//      Response response = client.target(QUERY_URI)
//          .request(MediaType.APPLICATION_JSON).post(Entity.entity(query, MediaType.APPLICATION_JSON));
//      System.out.println(response.readEntity(String.class));
    }
    long end = System.currentTimeMillis();
    System.out.println(end - start);
  }

  @Test
  public void testHttp() {
    long start = System.currentTimeMillis();
    for (int i = 0; i < 5000; i ++) {
      client.target(URI).request().get();
    }
    long end = System.currentTimeMillis();
    System.out.println(end - start);
  }

  @Test
  public void testSession() throws Exception {
    session = new Session("127.0.0.1", 6667, "root", "root");
    session.open();
    long start = System.currentTimeMillis();
    for(int i = 0; i < 5000; i++) {
      session.testInsertRecord("", 0, new ArrayList<>(), new ArrayList<>());
    }
    long end = System.currentTimeMillis();
    System.out.println(end - start);
  }

  @Test
  public void insertSession() throws Exception{
    session = new Session("127.0.0.1", 6667, "root", "root");
    session.open();
    long start = System.currentTimeMillis();
    for(int i = 0; i < 5000; i++) {
      insertByStr(i);
    }
    long end = System.currentTimeMillis();
    System.out.println(end - start);
  }

  private JSONArray insertJsonExample(int i) {
    JSONArray inserts = new JSONArray();
    String device = "root.ln.wf01.wt0" + i;
    for(int j = 0; j < 3000; j++) {
      JSONObject row = new JSONObject();
      row.put(HttpConstant.IS_NEED_INFER_TYPE, false);
      row.put(HttpConstant.DEVICE_ID, device);
      JSONArray measurements = new JSONArray();
      measurements.add("temperature");
      measurements.add("status");
      measurements.add("hardware");
      row.put(HttpConstant.MEASUREMENTS, measurements);
      row.put(HttpConstant.TIMESTAMP, j);
      JSONArray values = new JSONArray();
      values.add(j);
      values.add(true);
      values.add(j);
      row.put(HttpConstant.VALUES, values);
      inserts.add(row);
    }
    return inserts;
  }

  private void insertByStr(int i) throws IoTDBConnectionException, StatementExecutionException {
    List<String> deviceIds = new ArrayList<>();
    List<List<Object>> valuesList = new ArrayList<>();
    List<Long> timestamps = new ArrayList<>();
    List<List<TSDataType>> typesList = new ArrayList<>();
    String deviceId = "root.ln.wf01.wt0" + i;
    List<List<String>> measurementsList = new ArrayList<>();
    for (int time = 0; time < 3000; time++) {
      timestamps.add((long) time);
      deviceIds.add(deviceId);
      List<Object> values = new ArrayList<>();
      List<TSDataType> types = new ArrayList<>();
      types.add(TSDataType.INT32);
      types.add(TSDataType.BOOLEAN);
      types.add(TSDataType.INT32);
      typesList.add(types);
      values.add(time);
      values.add(true);
      values.add(time);
      valuesList.add(values);
      List<String> measurements = new ArrayList<>();
      measurements.add("temperature");
      measurements.add("status");
      measurements.add("hardware");
      measurementsList.add(measurements);
    }
    session.insertRecords(deviceIds, timestamps, measurementsList,typesList, valuesList);

  }

  JSONObject queryJsonExample(int i) {
    JSONObject query = new JSONObject();
    query.put(HttpConstant.SELECT, "temperature");
    query.put(HttpConstant.FROM, "root.ln.wf01.wt0" + i);
    JSONObject range = new JSONObject();
    range.put(HttpConstant.START, "0");
    range.put(HttpConstant.END, "6");
    query.put(HttpConstant.RANGE, range);
    return query;
  }

  private void login() {
    Response response = client.target(USER_LOGIN).request(MediaType.APPLICATION_JSON).get();
    Assert.assertEquals(SUCCESSFUL_RESPONSE, response.readEntity(String.class));
  }
}

Document

you can see the document in Restful doc. you want to edit it, email me geniuspig@apache.org

@Genius-pig Genius-pig changed the title basic structure Restful API v2 Sep 9, 2020
@Genius-pig Genius-pig changed the title Restful API v2 Restful API v2 (using netty) Sep 16, 2020
@Genius-pig Genius-pig marked this pull request as ready for review September 16, 2020 04:53
@sonarqubecloud
Copy link

SonarCloud Quality Gate failed.

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot E 1 Security Hotspot
Code Smell A 12 Code Smells

No Coverage information No Coverage information
5.5% 5.5% Duplication

warning The version of Java (1.8.0_252) you have used to run this analysis is deprecated and we will stop accepting it soon. Please update to at least Java 11.
Read more here

@HTHou HTHou closed this Jun 24, 2021
@HTHou HTHou deleted the http_v2 branch August 15, 2021 13:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants