Repository navigation
Conversation
loadRef cached only the text of an external file and parsed the whole content again for every $ref into it. Cache the parsed tree (and the messages produced while parsing) per ResolverCache instance, and deserialize from a deepCopy() of the requested subtree so that resolved models never share mutable nodes with the cached tree. Fixes swagger-api#2275
Author
|
Benchmark used for the numbers above (temporary TestNG test placed in package io.swagger.v3.parser.test;
import io.swagger.v3.parser.OpenAPIV3Parser;
import io.swagger.v3.parser.core.models.ParseOptions;
import io.swagger.v3.parser.core.models.SwaggerParseResult;
import org.testng.annotations.Test;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
public class TmpBenchmarkTest {
private static final int DICTIONARY_FIELDS = 1100;
private Path generate(int specs, int refsPerSpec) throws Exception {
Path dir = Files.createTempDirectory("bench");
StringBuilder dict = new StringBuilder("openapi: 3.0.1\ninfo: {title: d, version: '1'}\npaths: {}\ncomponents:\n schemas:\n");
for (int i = 0; i < DICTIONARY_FIELDS; i++) {
dict.append(" Field").append(i).append(":\n")
.append(" type: string\n")
.append(" description: Dictionary field number ").append(i).append(" with a reasonably long description text\n")
.append(" maxLength: ").append(10 + i % 50).append("\n")
.append(" example: value").append(i).append("\n");
}
Files.write(dir.resolve("dictionary.yaml"), dict.toString().getBytes(StandardCharsets.UTF_8));
for (int s = 0; s < specs; s++) {
StringBuilder spec = new StringBuilder("openapi: 3.0.1\ninfo: {title: s").append(s).append(", version: '1'}\npaths:\n /p:\n get:\n responses:\n '200':\n description: ok\n content:\n application/json:\n schema:\n $ref: '#/components/schemas/Model'\ncomponents:\n schemas:\n Model:\n type: object\n properties:\n");
for (int r = 0; r < refsPerSpec; r++) {
int field = (s * 31 + r * 7) % DICTIONARY_FIELDS;
spec.append(" f").append(r).append(":\n $ref: 'dictionary.yaml#/components/schemas/Field").append(field).append("'\n");
}
Files.write(dir.resolve("spec" + s + ".yaml"), spec.toString().getBytes(StandardCharsets.UTF_8));
}
return dir;
}
private long run(Path dir, int specs) {
ParseOptions options = new ParseOptions();
options.setResolve(true);
long start = System.nanoTime();
int props = 0;
for (int s = 0; s < specs; s++) {
SwaggerParseResult result = new OpenAPIV3Parser().readLocation(dir.resolve("spec" + s + ".yaml").toString(), null, options);
props += result.getOpenAPI().getComponents().getSchemas().get("Model").getProperties().size();
}
System.out.println("BENCH checksum props=" + props);
return (System.nanoTime() - start) / 1_000_000;
}
@Test
public void benchmark() throws Exception {
int[][] cases = {{1, 500}, {60, 15}};
for (int[] c : cases) {
Path dir = generate(c[0], c[1]);
run(dir, c[0]);
long t1 = run(dir, c[0]);
long t2 = run(dir, c[0]);
System.out.println("BENCH specs=" + c[0] + " refsPerSpec=" + c[1] + " totalRefs=" + c[0] * c[1] + " ms=" + t1 + "," + t2);
}
}
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #2275
Problem
ResolverCache.loadRefcaches only the text of an external file. For every$refinto that file the whole content is parsed again withDeserializationUtils.deserializeIntoTree, and then a single fragment is taken from the resulting tree. With a large shared file (a "dictionary" of fields, common definitions, etc.) referenced many times, parsing time grows asrefs x file size.Change
ResolverCacheinstance, so each file is parsed once.deepCopy()first. Without the copy two refs into the same file can end up sharing the sameObjectNode/ArrayNode(for example inSchema#example/default), so changing one resolved model would silently change another.Could not find ....Measurements
Synthetic specs: a dictionary file with 1100 small schemas, specs that
$refinto it viaOpenAPIV3Parser.readLocationwithresolve=true. Same machine and JVM, one warm-up run, then two timed runs (ms):This matches the report in #2275 (a build going from 4m49s to 58s). The benchmark source is in the comments below.
Tests
New
ResolverCacheExternalFileTest(7 tests, with and withoutvalidateExternalRefs):deepCopy()is removed);mvn testforswagger-parser-v3: 703 tests (696 on master plus the 7 new ones). The only failures,OpenAPIV3ParserTest.test31SafeURLResolvingandtest31SafeURLResolvingWithTurnedOffSafeResolving, fail the same way on unmodified master in my environment.