-
Notifications
You must be signed in to change notification settings - Fork 1.3k
[AI-8th] perf: add reflection cache #1415
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
kuleat
wants to merge
3
commits into
sofastack:master
Choose a base branch
from
kuleat:feature/reflection-cache
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
43 changes: 43 additions & 0 deletions
43
...sofa/boot/actuator/autoconfigure/reflection/ReflectionCacheEndpointAutoConfiguration.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.alipay.sofa.boot.actuator.autoconfigure.reflection; | ||
|
|
||
| import com.alipay.sofa.boot.actuator.reflection.ReflectionCacheEndpoint; | ||
| import com.alipay.sofa.boot.autoconfigure.reflection.ReflectionCacheAutoConfiguration; | ||
| import com.alipay.sofa.boot.reflection.ReflectionCache; | ||
| import org.springframework.boot.actuate.autoconfigure.endpoint.condition.ConditionalOnAvailableEndpoint; | ||
| import org.springframework.boot.autoconfigure.AutoConfiguration; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
| import org.springframework.context.annotation.Bean; | ||
|
|
||
| /** | ||
| * Configures {@link ReflectionCacheEndpoint}. | ||
| * | ||
| * @author xiaosiyuan | ||
| */ | ||
| @AutoConfiguration(after = ReflectionCacheAutoConfiguration.class) | ||
| @ConditionalOnBean(ReflectionCache.class) | ||
| @ConditionalOnAvailableEndpoint(endpoint = ReflectionCacheEndpoint.class) | ||
| public class ReflectionCacheEndpointAutoConfiguration { | ||
|
|
||
| @Bean | ||
| @ConditionalOnMissingBean | ||
| public ReflectionCacheEndpoint reflectionCacheEndpoint(ReflectionCache reflectionCache) { | ||
| return new ReflectionCacheEndpoint(reflectionCache); | ||
| } | ||
| } |
1 change: 1 addition & 0 deletions
1
...esources/META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports
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
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
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
61 changes: 61 additions & 0 deletions
61
...boot/actuator/autoconfigure/reflection/ReflectionCacheEndpointAutoConfigurationTests.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.alipay.sofa.boot.actuator.autoconfigure.reflection; | ||
|
|
||
| import com.alipay.sofa.boot.actuator.reflection.ReflectionCacheEndpoint; | ||
| import com.alipay.sofa.boot.autoconfigure.reflection.ReflectionCacheAutoConfiguration; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.boot.autoconfigure.AutoConfigurations; | ||
| import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| /** | ||
| * Tests for {@link ReflectionCacheEndpointAutoConfiguration}. | ||
| * | ||
| * @author xiaosiyuan | ||
| */ | ||
| public class ReflectionCacheEndpointAutoConfigurationTests { | ||
|
|
||
| private final ApplicationContextRunner contextRunner = new ApplicationContextRunner() | ||
| .withConfiguration(AutoConfigurations | ||
| .of(ReflectionCacheAutoConfiguration.class, | ||
| ReflectionCacheEndpointAutoConfiguration.class)); | ||
|
|
||
| @Test | ||
| void runShouldHaveEndpointBean() { | ||
| this.contextRunner.withPropertyValues( | ||
| "management.endpoints.web.exposure.include=reflection-cache") | ||
| .run((context) -> assertThat(context) | ||
| .hasSingleBean(ReflectionCacheEndpoint.class)); | ||
| } | ||
|
|
||
| @Test | ||
| void runWhenNotExposedShouldNotHaveEndpointBean() { | ||
| this.contextRunner.run( | ||
| (context) -> assertThat(context).doesNotHaveBean(ReflectionCacheEndpoint.class)); | ||
| } | ||
|
|
||
| @Test | ||
| void runWhenReflectionCacheBeanMissingShouldNotHaveEndpointBean() { | ||
| new ApplicationContextRunner().withConfiguration( | ||
| AutoConfigurations.of(ReflectionCacheEndpointAutoConfiguration.class)) | ||
| .withPropertyValues("management.endpoints.web.exposure.include=reflection-cache") | ||
| .run((context) -> assertThat(context) | ||
| .doesNotHaveBean(ReflectionCacheEndpoint.class)); | ||
| } | ||
| } |
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
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
48 changes: 48 additions & 0 deletions
48
...uator/src/main/java/com/alipay/sofa/boot/actuator/reflection/ReflectionCacheEndpoint.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.alipay.sofa.boot.actuator.reflection; | ||
|
|
||
| import com.alipay.sofa.boot.reflection.ReflectionCache; | ||
| import org.springframework.boot.actuate.endpoint.annotation.Endpoint; | ||
| import org.springframework.boot.actuate.endpoint.annotation.ReadOperation; | ||
| import org.springframework.boot.actuate.endpoint.annotation.WriteOperation; | ||
|
|
||
| /** | ||
| * Endpoint for viewing and clearing the reflection cache. | ||
| * | ||
| * @author xiaosiyuan | ||
| */ | ||
| @Endpoint(id = "reflection-cache") | ||
| public class ReflectionCacheEndpoint { | ||
|
|
||
| private final ReflectionCache reflectionCache; | ||
|
|
||
| public ReflectionCacheEndpoint(ReflectionCache reflectionCache) { | ||
| this.reflectionCache = reflectionCache; | ||
| } | ||
|
|
||
| @ReadOperation | ||
| public ReflectionCache.CacheStats stats() { | ||
| return reflectionCache.getStats(); | ||
| } | ||
|
|
||
| @WriteOperation | ||
| public ReflectionCache.CacheStats clear() { | ||
| reflectionCache.clear(); | ||
| return reflectionCache.getStats(); | ||
| } | ||
| } |
62 changes: 62 additions & 0 deletions
62
.../src/test/java/com/alipay/sofa/boot/actuator/reflection/ReflectionCacheEndpointTests.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.alipay.sofa.boot.actuator.reflection; | ||
|
|
||
| import com.alipay.sofa.boot.reflection.ReflectionCache; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| /** | ||
| * Tests for {@link ReflectionCacheEndpoint}. | ||
| * | ||
| * @author xiaosiyuan | ||
| */ | ||
| public class ReflectionCacheEndpointTests { | ||
|
|
||
| @Test | ||
| void statsShouldExposeReflectionCacheState() throws Exception { | ||
| ReflectionCache reflectionCache = new ReflectionCache(); | ||
| ReflectionCacheEndpoint endpoint = new ReflectionCacheEndpoint(reflectionCache); | ||
|
|
||
| reflectionCache.forName(String.class.getName()); | ||
| reflectionCache.forName(String.class.getName()); | ||
|
|
||
| ReflectionCache.CacheStats stats = endpoint.stats(); | ||
| assertThat(stats.isEnabled()).isTrue(); | ||
| assertThat(stats.getClassHitCount()).isEqualTo(1); | ||
| assertThat(stats.getClassMissCount()).isEqualTo(1); | ||
| assertThat(stats.getClassCacheSize()).isEqualTo(1); | ||
| } | ||
|
|
||
| @Test | ||
| void clearShouldResetReflectionCacheState() throws Exception { | ||
| ReflectionCache reflectionCache = new ReflectionCache(); | ||
| ReflectionCacheEndpoint endpoint = new ReflectionCacheEndpoint(reflectionCache); | ||
|
|
||
| reflectionCache.forName(String.class.getName()); | ||
| reflectionCache.findMethod(String.class, "trim"); | ||
| reflectionCache.getDeclaredConstructor(String.class, String.class); | ||
|
|
||
| ReflectionCache.CacheStats stats = endpoint.clear(); | ||
| assertThat(stats.getTotalHitCount()).isZero(); | ||
| assertThat(stats.getTotalMissCount()).isZero(); | ||
| assertThat(stats.getClassCacheSize()).isZero(); | ||
| assertThat(stats.getMethodCacheSize()).isZero(); | ||
| assertThat(stats.getConstructorCacheSize()).isZero(); | ||
| } | ||
| } |
39 changes: 39 additions & 0 deletions
39
.../java/com/alipay/sofa/boot/autoconfigure/reflection/ReflectionCacheAutoConfiguration.java
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one or more | ||
| * contributor license agreements. See the NOTICE file distributed with | ||
| * this work for additional information regarding copyright ownership. | ||
| * The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| * (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package com.alipay.sofa.boot.autoconfigure.reflection; | ||
|
|
||
| import com.alipay.sofa.boot.reflection.ReflectionCache; | ||
| import org.springframework.boot.autoconfigure.AutoConfiguration; | ||
| import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
| import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
| import org.springframework.context.annotation.Bean; | ||
|
|
||
| /** | ||
| * Configures {@link ReflectionCache}. | ||
| * | ||
| * @author xiaosiyuan | ||
| */ | ||
| @AutoConfiguration | ||
| @EnableConfigurationProperties(ReflectionCacheProperties.class) | ||
| public class ReflectionCacheAutoConfiguration { | ||
|
|
||
| @Bean | ||
| @ConditionalOnMissingBean | ||
| public ReflectionCache reflectionCache(ReflectionCacheProperties reflectionCacheProperties) { | ||
| return new ReflectionCache(reflectionCacheProperties.isEnabled()); | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.