• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2008 Google Inc.  All rights reserved.
3 //
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
7 
8 package com.google.protobuf;
9 
10 import static com.google.common.truth.Truth.assertThat;
11 
12 import com.google.protobuf.testing.textformat.performance.proto2.Proto2TextFormatPerformanceProto;
13 import com.google.protobuf.testing.textformat.performance.proto3.Proto3TextFormatPerformanceProto;
14 import org.junit.Test;
15 import org.junit.runner.RunWith;
16 import org.junit.runners.JUnit4;
17 
18 @RunWith(JUnit4.class)
19 public class TextFormatPerformanceTest {
20   // 10 Seconds is longer than we'd really like, but necesssary to keep this test from being flaky.
21   // This test is mostly to make sure it doesn't explode to many 10s of seconds for some reason.
22   private static final long MAX_PARSE_TIME_MS = 10_000L;
23 
24   private static final int REPEAT_COUNT = 400000;
25   private static final String CONTAINS_SUB_MESSAGE_WITH_REPEATED_INT32 =
26       repeat("sub_msg { value: 123 }", REPEAT_COUNT);
27   private static final String CONTAINS_EXTENSION_SUB_MESSAGE_WITH_REPEATED_INT32 =
28       repeat(
29           "[protobuf.testing.textformat.performance.proto2.sub_msg_ext] { value: 123 }",
30           REPEAT_COUNT);
31 
32   // OSS Tests are still using JDK 8, which doesn't have JDK 11 String.repeat()
repeat(String text, int count)33   private static String repeat(String text, int count) {
34     StringBuilder builder = new StringBuilder(text.length() * count);
35     for (int i = 0; i < count; ++i) {
36       builder.append(text);
37     }
38     return builder.toString();
39   }
40 
41   @Test(timeout = MAX_PARSE_TIME_MS)
testProto2ImmutableTextFormatParsing()42   public void testProto2ImmutableTextFormatParsing() throws Exception {
43     Proto2TextFormatPerformanceProto.ContainsSubMessageWithRepeatedInt32.Builder builder =
44         Proto2TextFormatPerformanceProto.ContainsSubMessageWithRepeatedInt32.newBuilder();
45 
46     TextFormat.merge(CONTAINS_SUB_MESSAGE_WITH_REPEATED_INT32, builder);
47 
48     Proto2TextFormatPerformanceProto.ContainsSubMessageWithRepeatedInt32 msg = builder.build();
49     assertThat(msg.getSubMsg().getValueCount()).isEqualTo(REPEAT_COUNT);
50     for (int i = 0; i < msg.getSubMsg().getValueCount(); ++i) {
51       assertThat(msg.getSubMsg().getValue(i)).isEqualTo(123);
52     }
53   }
54 
55   @Test(timeout = MAX_PARSE_TIME_MS)
testProto2ImmutableExtensionTextFormatParsing()56   public void testProto2ImmutableExtensionTextFormatParsing() throws Exception {
57     ExtensionRegistry registry = ExtensionRegistry.newInstance();
58     Proto2TextFormatPerformanceProto.registerAllExtensions(registry);
59 
60     Proto2TextFormatPerformanceProto.ContainsExtensionSubMessage.Builder builder =
61         Proto2TextFormatPerformanceProto.ContainsExtensionSubMessage.newBuilder();
62 
63     TextFormat.merge(CONTAINS_EXTENSION_SUB_MESSAGE_WITH_REPEATED_INT32, registry, builder);
64 
65     Proto2TextFormatPerformanceProto.ContainsExtensionSubMessage msg = builder.build();
66     assertThat(msg.getExtension(Proto2TextFormatPerformanceProto.subMsgExt).getValueCount())
67         .isEqualTo(REPEAT_COUNT);
68     for (int i = 0;
69         i < msg.getExtension(Proto2TextFormatPerformanceProto.subMsgExt).getValueCount();
70         ++i) {
71       assertThat(msg.getExtension(Proto2TextFormatPerformanceProto.subMsgExt).getValue(i))
72           .isEqualTo(123);
73     }
74   }
75 
76   @Test(timeout = MAX_PARSE_TIME_MS)
testProto3ImmutableTextFormatParsing()77   public void testProto3ImmutableTextFormatParsing() throws Exception {
78     Proto3TextFormatPerformanceProto.ContainsSubMessageWithRepeatedInt32.Builder builder =
79         Proto3TextFormatPerformanceProto.ContainsSubMessageWithRepeatedInt32.newBuilder();
80 
81     TextFormat.merge(CONTAINS_SUB_MESSAGE_WITH_REPEATED_INT32, builder);
82 
83     Proto3TextFormatPerformanceProto.ContainsSubMessageWithRepeatedInt32 msg = builder.build();
84     assertThat(msg.getSubMsg().getValueCount()).isEqualTo(REPEAT_COUNT);
85     for (int i = 0; i < msg.getSubMsg().getValueCount(); ++i) {
86       assertThat(msg.getSubMsg().getValue(i)).isEqualTo(123);
87     }
88   }
89 }
90