Пример #1
0
  @Test
  public void testOptimizedSerializationDeserialization() throws Exception {
    TestAllTypes message = createSampleMessage();

    byte[] data = TestAllTypesSerializer.serialize(message);

    TestAllTypes result = TestAllTypesSerializer.parseFrom(data);
    assertNotNull(result);
    assertDeepEquals(message, result);
  }
Пример #2
0
  @Test
  public void testOptimizedSerializationDefaultDeserialization() throws Exception {

    TestAllTypes message = createSampleMessage();

    byte[] data = TestAllTypesSerializer.serialize(message);

    protobuf_unittest.UnittestProto.TestAllTypes result =
        protobuf_unittest.UnittestProto.TestAllTypes.parseFrom(data);
    assertNotNull(result);
    assertDeepEquals(result, message);
    assertTrue(result.getUnknownFields().asMap().isEmpty());
  }
Пример #3
0
  @Test
  public void testOptimizedSerializeToStreamDefaultDeserialization() throws Exception {

    TestAllTypes message = createSampleMessage();

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    TestAllTypesSerializer.serialize(message, baos);

    protobuf_unittest.UnittestProto.TestAllTypes result =
        protobuf_unittest.UnittestProto.TestAllTypes.parseFrom(baos.toByteArray());
    assertNotNull(result);
    assertDeepEquals(result, message);
    assertTrue(result.getUnknownFields().asMap().isEmpty());
  }
Пример #4
0
  @Test
  public void testDefaultSerializationOptimizedDeserialization() throws Exception {
    TestAllTypes message = createSampleMessage();
    byte[] data = TestAllTypesSerializer.serialize(message);

    protobuf_unittest.UnittestProto.TestAllTypes result =
        protobuf_unittest.UnittestProto.TestAllTypes.parseFrom(data);

    byte[] defaultSerializationData = result.toByteArray();

    TestAllTypes optimizedResult = TestAllTypesSerializer.parseFrom(defaultSerializationData);
    assertDeepEquals(result, optimizedResult);
    assertEquals(2, optimizedResult.getRepeatedForeignMessage().size());
    assertEquals(1, optimizedResult.getRepeatedForeignMessage().get(0).getC());
    assertEquals(2, optimizedResult.getRepeatedForeignMessage().get(1).getC());
  }
Пример #5
0
  public static void main(String[] args) throws Exception {

    long times = 1000000;

    long start;

    TestAllTypes message = createSampleMessage();
    byte[] data = TestAllTypesSerializer.serialize(message);

    start = System.currentTimeMillis();
    for (int i = 0; i < times; i++) {
      TestAllTypesSerializer.serialize(message);
    }
    System.out.println(" * Optimized version(serialize): " + (System.currentTimeMillis() - start));
    protobuf_unittest.UnittestProto.TestAllTypes result =
        protobuf_unittest.UnittestProto.TestAllTypes.parseFrom(data);

    start = System.currentTimeMillis();
    for (int i = 0; i < times; i++) {
      result.toByteArray();
    }
    System.out.println(" * Default version(serialize): " + (System.currentTimeMillis() - start));

    ByteArrayOutputStream baos =
        new ByteArrayOutputStream(
            data.length
                + 10); // little trick to remove stream io issues and measure pure serialization
    // power.

    start = System.currentTimeMillis();
    for (int i = 0; i < times; i++) {
      TestAllTypesSerializer.serialize(message, baos);
      baos.reset();
    }
    System.out.println(
        " * Optimized streamed version(serialize): " + (System.currentTimeMillis() - start));

    start = System.currentTimeMillis();
    for (int i = 0; i < times; i++) {
      result.writeTo(baos);
      baos.reset();
    }
    System.out.println(
        " * Default streamed version(serialize): " + (System.currentTimeMillis() - start));

    start = System.currentTimeMillis();
    for (int i = 0; i < times; i++) {
      TestAllTypesSerializer.parseFrom(data);
    }
    System.out.println(
        " * Optimized version(de-serialize): " + (System.currentTimeMillis() - start));

    start = System.currentTimeMillis();
    for (int i = 0; i < times; i++) {
      protobuf_unittest.UnittestProto.TestAllTypes.parseFrom(data);
    }
    System.out.println(" * Default version(de-serialize): " + (System.currentTimeMillis() - start));

    ByteArrayInputStream bais = new ByteArrayInputStream(data);

    start = System.currentTimeMillis();
    for (int i = 0; i < times; i++) {
      TestAllTypesSerializer.parseFrom(bais);
      bais.reset();
    }
    System.out.println(
        " * Optimized streamed version(de-serialize): " + (System.currentTimeMillis() - start));

    start = System.currentTimeMillis();
    for (int i = 0; i < times; i++) {
      protobuf_unittest.UnittestProto.TestAllTypes.parseFrom(bais);
      bais.reset();
    }
    System.out.println(
        " * Default streamed version(de-serialize): " + (System.currentTimeMillis() - start));
  }