1+ /*
2+ * Copyright 2020 the original author or authors.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+ package com .apicatalog .jsonld .api ;
17+
18+ import static org .junit .jupiter .api .Assertions .assertNotNull ;
19+ import static org .junit .jupiter .api .Assertions .assertTrue ;
20+
21+ import org .junit .jupiter .api .Test ;
22+
23+ import com .apicatalog .jsonld .JsonLd ;
24+ import com .apicatalog .jsonld .JsonLdError ;
25+ import com .apicatalog .jsonld .document .JsonDocument ;
26+
27+ import jakarta .json .Json ;
28+ import jakarta .json .JsonArrayBuilder ;
29+ import jakarta .json .JsonObject ;
30+ import jakarta .json .JsonObjectBuilder ;
31+
32+ class FramingReversePerformanceTest {
33+
34+ @ Test
35+ void testFramingPerformance1000Elements () throws JsonLdError {
36+
37+ final long executionTime = measureFramingPerformance (1_000 );
38+
39+ System .out .println ("Framing 1_000 elements took: " + executionTime + " ms" );
40+
41+ assertTrue (executionTime < 1_000L ,
42+ "Framing 1_000 elements took too long: " + executionTime + " ms" );
43+ }
44+
45+ @ Test
46+ void testFramingPerformance10000Elements () throws JsonLdError {
47+
48+ final long executionTime = measureFramingPerformance (10_000 );
49+
50+ System .out .println ("Framing 10_000 elements took: " + executionTime + " ms" );
51+
52+ assertTrue (executionTime < 5_000L ,
53+ "Framing 10_000 elements took too long: " + executionTime + " ms" );
54+ }
55+
56+ private long measureFramingPerformance (final int elementCount ) throws JsonLdError {
57+
58+ final JsonObject document = buildTestDocument (elementCount );
59+ final JsonObject frame = buildTestFrame ();
60+
61+ final long startTime = System .currentTimeMillis ();
62+
63+ final JsonObject framed = JsonLd .frame (JsonDocument .of (document ), JsonDocument .of (frame )).get ();
64+
65+ final long endTime = System .currentTimeMillis ();
66+
67+ assertNotNull (framed );
68+
69+ return endTime - startTime ;
70+ }
71+
72+ private JsonObject buildTestDocument (final int elementCount ) {
73+
74+ final JsonObjectBuilder contextBuilder = Json .createObjectBuilder ()
75+ .add ("@vocab" , "http://example.com/vocab#" );
76+
77+ final JsonArrayBuilder dataBuilder = Json .createArrayBuilder ();
78+
79+ final int departmentCount = Math .max (5 , elementCount / 100 );
80+ final int personsPerDepartment = elementCount / departmentCount ;
81+
82+ for (int departmentIndex = 0 ; departmentIndex < departmentCount ; departmentIndex ++) {
83+
84+ final String departmentId = "http://example.com/department" + departmentIndex ;
85+
86+ dataBuilder .add (Json .createObjectBuilder ()
87+ .add ("@id" , departmentId )
88+ .add ("@type" , "Department" )
89+ .add ("name" , "Department " + departmentIndex ));
90+
91+ for (int personOffset = 0 ; personOffset < personsPerDepartment ; personOffset ++) {
92+
93+ final int personIndex = departmentIndex * personsPerDepartment + personOffset ;
94+ final JsonObjectBuilder personBuilder = Json .createObjectBuilder ()
95+ .add ("@id" , "http://example.com/person" + personIndex )
96+ .add ("@type" , "Person" )
97+ .add ("name" , "Person " + personIndex )
98+ .add ("memberOf" , departmentId );
99+
100+ if (personOffset == 0 && departmentIndex > 0 ) {
101+ personBuilder .add ("manages" , departmentId );
102+ }
103+
104+ if (personIndex > 0 && personIndex % 3 == 0 ) {
105+ final JsonArrayBuilder relatedBuilder = Json .createArrayBuilder ();
106+ relatedBuilder .add ("http://example.com/person" + (personIndex - 1 ));
107+
108+ if (personIndex < elementCount - 1 ) {
109+ relatedBuilder .add ("http://example.com/person" + (personIndex + 1 ));
110+ }
111+
112+ if (personIndex >= 5 && personIndex % 5 == 0 ) {
113+ relatedBuilder .add ("http://example.com/person" + (personIndex - 5 ));
114+ }
115+
116+ personBuilder .add ("relatedTo" , relatedBuilder );
117+ }
118+
119+ final int itemsPerPerson = 3 ;
120+ for (int itemOffset = 0 ; itemOffset < itemsPerPerson ; itemOffset ++) {
121+
122+ final int itemIndex = personIndex * itemsPerPerson + itemOffset ;
123+ final JsonObjectBuilder itemBuilder = Json .createObjectBuilder ()
124+ .add ("@id" , "http://example.com/item" + itemIndex )
125+ .add ("@type" , "Item" )
126+ .add ("name" , "Item " + itemIndex )
127+ .add ("parent" , "http://example.com/person" + personIndex );
128+
129+ if (itemOffset > 0 ) {
130+ itemBuilder .add ("child" , "http://example.com/item" + (itemIndex - 1 ));
131+ }
132+
133+ dataBuilder .add (itemBuilder );
134+ }
135+
136+ dataBuilder .add (personBuilder );
137+ }
138+ }
139+
140+ return Json .createObjectBuilder ()
141+ .add ("@context" , contextBuilder )
142+ .add ("@graph" , dataBuilder )
143+ .build ();
144+ }
145+
146+ private JsonObject buildTestFrame () {
147+
148+ return Json .createObjectBuilder ()
149+ .add ("@context" , Json .createObjectBuilder ()
150+ .add ("@vocab" , "http://example.com/vocab#" ))
151+ .add ("@type" , "Person" )
152+ .add ("@reverse" , Json .createObjectBuilder ()
153+ .add ("memberOf" , Json .createObjectBuilder ()
154+ .add ("@type" , "Department" ))
155+ .add ("parent" , Json .createObjectBuilder ()
156+ .add ("@type" , "Item" ))
157+ .add ("relatedTo" , Json .createObjectBuilder ()
158+ .add ("@type" , "Person" )))
159+ .build ();
160+ }
161+ }
0 commit comments