|
| 1 | +/* |
| 2 | + * Copyright (c) 2024. |
| 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 | + |
| 17 | +package org.modelix.model.server |
| 18 | + |
| 19 | +import io.ktor.serialization.kotlinx.json.json |
| 20 | +import io.ktor.server.application.install |
| 21 | +import io.ktor.server.plugins.contentnegotiation.ContentNegotiation |
| 22 | +import io.ktor.server.resources.Resources |
| 23 | +import io.ktor.server.routing.IgnoreTrailingSlash |
| 24 | +import io.ktor.server.testing.ApplicationTestBuilder |
| 25 | +import io.ktor.server.testing.testApplication |
| 26 | +import io.ktor.server.websocket.WebSockets |
| 27 | +import kotlinx.coroutines.coroutineScope |
| 28 | +import org.modelix.authorization.installAuthentication |
| 29 | +import org.modelix.model.api.IChildLink |
| 30 | +import org.modelix.model.api.IConceptReference |
| 31 | +import org.modelix.model.api.INode |
| 32 | +import org.modelix.model.api.getRootNode |
| 33 | +import org.modelix.model.client2.ModelClientV2 |
| 34 | +import org.modelix.model.client2.runWriteOnBranch |
| 35 | +import org.modelix.model.lazy.RepositoryId |
| 36 | +import org.modelix.model.server.handlers.KeyValueLikeModelServer |
| 37 | +import org.modelix.model.server.handlers.ModelReplicationServer |
| 38 | +import org.modelix.model.server.handlers.RepositoriesManager |
| 39 | +import org.modelix.model.server.store.InMemoryStoreClient |
| 40 | +import org.modelix.model.server.store.LocalModelClient |
| 41 | +import kotlin.random.Random |
| 42 | +import kotlin.test.Test |
| 43 | +import kotlin.test.assertTrue |
| 44 | + |
| 45 | +class PullPerformanceTest { |
| 46 | + private fun runTest(block: suspend ApplicationTestBuilder.(storeClientWithStatistics: StoreClientWithStatistics) -> Unit) = testApplication { |
| 47 | + val storeClientWithStatistics = StoreClientWithStatistics(InMemoryStoreClient()) |
| 48 | + val repositoriesManager = RepositoriesManager(LocalModelClient(storeClientWithStatistics)) |
| 49 | + application { |
| 50 | + installAuthentication(unitTestMode = true) |
| 51 | + install(ContentNegotiation) { |
| 52 | + json() |
| 53 | + } |
| 54 | + install(WebSockets) |
| 55 | + install(Resources) |
| 56 | + install(IgnoreTrailingSlash) |
| 57 | + ModelReplicationServer(repositoriesManager).init(this) |
| 58 | + KeyValueLikeModelServer(repositoriesManager).init(this) |
| 59 | + } |
| 60 | + |
| 61 | + coroutineScope { |
| 62 | + block(storeClientWithStatistics) |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + /** |
| 67 | + * Tests the performance of the `GET /v2/repositories/{repository}/branches/{branch}` endpoint. |
| 68 | + * Many small request to an IgniteStoreClient lead to a poor performance. This test ensure that bulk requests are |
| 69 | + * used for loading a model. |
| 70 | + */ |
| 71 | + @Test |
| 72 | + fun `bulk requests are used`() = runTest { storeClientWithStatistics -> |
| 73 | + val rand = Random(1056343) |
| 74 | + val url = "http://localhost/v2" |
| 75 | + val preparingModelClient = ModelClientV2.builder().url(url).client(client).build().also { it.init() } |
| 76 | + val repositoryId = RepositoryId("repo1") |
| 77 | + preparingModelClient.initRepository(repositoryId) |
| 78 | + preparingModelClient.runWriteOnBranch(repositoryId.getBranchReference()) { branch -> |
| 79 | + val rootNode = branch.getRootNode() |
| 80 | + repeat(10_000) { |
| 81 | + val randomNode = rootNode.getRandomNode(rand) |
| 82 | + randomNode.addNewChild(IChildLink.fromName("roleA"), -1, null as IConceptReference?) |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + val requestingModelClient = ModelClientV2.builder().url(url).client(client).build().also { it.init() } |
| 87 | + val totalRequestsBeforePull = storeClientWithStatistics.getTotalRequests() |
| 88 | + requestingModelClient.pull(repositoryId.getBranchReference(), null) |
| 89 | + val totalRequestsAfterPull = storeClientWithStatistics.getTotalRequests() |
| 90 | + val actualRequestCount = totalRequestsAfterPull - totalRequestsBeforePull |
| 91 | + |
| 92 | + // The request count when not using a bulk query is a couple ten thousand. |
| 93 | + // Using a bulk query reduces the number of separate requests to the underling store to much fewer. |
| 94 | + assertTrue(actualRequestCount < 20, "Too many request: $actualRequestCount") |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +private fun INode.getRandomNode(rand: Random): INode { |
| 99 | + val node = this |
| 100 | + val children = node.allChildren.toList() |
| 101 | + val index = rand.nextInt(children.size + 1) |
| 102 | + return if (index < children.size) { |
| 103 | + children.get(index).getRandomNode(rand) |
| 104 | + } else { |
| 105 | + node |
| 106 | + } |
| 107 | +} |
0 commit comments