-
Notifications
You must be signed in to change notification settings - Fork 33
Optimize JNI performance and fix Decimal validation #349
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a97b4cd
Optimize JNI performance and fix Decimal validation
c26754c
Fix checkstyle violations in JMH benchmark method names
67c69da
Exclude JMH-generated code from SpotBugs analysis
d76674f
Address PR #349 review feedback
3012f0d
Fix cargo-zigbuild install failure and trailing whitespace
615a1e5
Address PR #349 review feedback
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
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,7 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <FindBugsFilter> | ||
| <!-- Exclude JMH-generated code: padding fields and dead stores are intentional for cache-line isolation --> | ||
| <Match> | ||
| <Package name="~com\.cedarpolicy\.jmh_generated.*"/> | ||
| </Match> | ||
| </FindBugsFilter> |
143 changes: 143 additions & 0 deletions
143
CedarJava/src/jmh/java/com/cedarpolicy/AuthorizationBenchmark.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,143 @@ | ||
| /* | ||
| * Copyright Cedar Contributors | ||
| * | ||
| * Licensed 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 | ||
| * | ||
| * https://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.cedarpolicy; | ||
|
|
||
| import com.cedarpolicy.model.AuthorizationRequest; | ||
| import com.cedarpolicy.model.AuthorizationResponse; | ||
| import com.cedarpolicy.model.ValidationRequest; | ||
| import com.cedarpolicy.model.ValidationResponse; | ||
| import com.cedarpolicy.model.entity.Entities; | ||
| import com.cedarpolicy.model.entity.Entity; | ||
| import com.cedarpolicy.model.exception.AuthException; | ||
| import com.cedarpolicy.model.policy.PolicySet; | ||
| import com.cedarpolicy.model.schema.Schema; | ||
| import com.cedarpolicy.model.schema.Schema.JsonOrCedar; | ||
| import com.cedarpolicy.value.EntityTypeName; | ||
|
|
||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.BenchmarkMode; | ||
| import org.openjdk.jmh.annotations.Fork; | ||
| import org.openjdk.jmh.annotations.Level; | ||
| import org.openjdk.jmh.annotations.Measurement; | ||
| import org.openjdk.jmh.annotations.Mode; | ||
| import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
| import org.openjdk.jmh.annotations.Scope; | ||
| import org.openjdk.jmh.annotations.Setup; | ||
| import org.openjdk.jmh.annotations.State; | ||
| import org.openjdk.jmh.annotations.Warmup; | ||
|
|
||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Paths; | ||
| import java.util.HashMap; | ||
| import java.util.Optional; | ||
| import java.util.Set; | ||
| import java.util.concurrent.TimeUnit; | ||
|
|
||
| /** | ||
| * JMH benchmarks for Cedar authorization engine JNI performance. | ||
| * | ||
| * <p>Run via: ./gradlew jmh | ||
| */ | ||
| @BenchmarkMode(Mode.AverageTime) | ||
| @OutputTimeUnit(TimeUnit.MICROSECONDS) | ||
| @Warmup(iterations = 3, time = 1) | ||
| @Measurement(iterations = 5, time = 1) | ||
| @Fork(1) | ||
| @State(Scope.Benchmark) | ||
| public class AuthorizationBenchmark { | ||
|
|
||
| private BasicAuthorizationEngine engine; | ||
|
|
||
| // Small scenario: minimal request | ||
| private AuthorizationRequest smallRequest; | ||
| private PolicySet smallPolicySet; | ||
| private Set<Entity> smallEntities; | ||
|
|
||
| // Medium scenario: photoflash schema with entities and multiple policies | ||
| private AuthorizationRequest mediumRequest; | ||
| private PolicySet mediumPolicySet; | ||
| private Set<Entity> mediumEntities; | ||
|
|
||
| // Validation scenario | ||
| private ValidationRequest validationRequest; | ||
|
|
||
| @Setup(Level.Trial) | ||
| public void setUp() throws Exception { | ||
| engine = new BasicAuthorizationEngine(); | ||
|
|
||
| setUpSmallScenario(); | ||
| setUpMediumScenario(); | ||
| setUpValidationScenario(); | ||
| } | ||
|
|
||
| private static String readResource(String resourcePath) throws Exception { | ||
| return new String( | ||
| Files.readAllBytes(Paths.get( | ||
| AuthorizationBenchmark.class.getResource(resourcePath).toURI())), | ||
| StandardCharsets.UTF_8); | ||
| } | ||
|
|
||
| private void setUpSmallScenario() throws Exception { | ||
| EntityTypeName userType = EntityTypeName.parse("User").get(); | ||
| EntityTypeName actionType = EntityTypeName.parse("Action").get(); | ||
| EntityTypeName resourceType = EntityTypeName.parse("Resource").get(); | ||
|
|
||
| smallRequest = new AuthorizationRequest( | ||
| userType.of("alice"), actionType.of("view"), resourceType.of("doc1"), new HashMap<>()); | ||
|
|
||
| smallPolicySet = PolicySet.parsePolicies(readResource("/small_policies.cedar")); | ||
| smallEntities = Entities.parse(readResource("/small_entities.json")).getEntities(); | ||
| } | ||
|
|
||
| private void setUpMediumScenario() throws Exception { | ||
| EntityTypeName userType = EntityTypeName.parse("User").get(); | ||
| EntityTypeName actionType = EntityTypeName.parse("Action").get(); | ||
| EntityTypeName photoType = EntityTypeName.parse("Photo").get(); | ||
|
|
||
| mediumRequest = new AuthorizationRequest( | ||
| userType.of("alice"), actionType.of("View_Photo"), photoType.of("pic01"), new HashMap<>()); | ||
|
|
||
| mediumPolicySet = PolicySet.parsePolicies(readResource("/medium_policies.cedar")); | ||
| mediumEntities = Entities.parse(readResource("/medium_entities.json")).getEntities(); | ||
| } | ||
|
|
||
| private void setUpValidationScenario() throws Exception { | ||
| String schemaText = readResource("/photoflash_schema.json"); | ||
| Schema schema = new Schema(JsonOrCedar.Json, Optional.of(schemaText), Optional.empty()); | ||
|
|
||
| PolicySet policySet = PolicySet.parsePolicies( | ||
| "permit(principal == User::\"alice\", action == Action::\"View_Photo\", resource);"); | ||
|
|
||
| validationRequest = new ValidationRequest(schema, policySet); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public AuthorizationResponse isAuthorizedSmall() throws AuthException { | ||
| return engine.isAuthorized(smallRequest, smallPolicySet, smallEntities); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public AuthorizationResponse isAuthorizedMedium() throws AuthException { | ||
| return engine.isAuthorized(mediumRequest, mediumPolicySet, mediumEntities); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public ValidationResponse validateSmall() throws AuthException { | ||
| return engine.validate(validationRequest); | ||
| } | ||
| } | ||
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,7 @@ | ||
| [ | ||
| {"uid": {"type": "User", "id": "alice"}, "attrs": {}, "parents": []}, | ||
| {"uid": {"type": "Action", "id": "View_Photo"}, "attrs": {}, "parents": []}, | ||
| {"uid": {"type": "Photo", "id": "pic01"}, "attrs": {}, "parents": [{"type": "Album", "id": "vacation"}]}, | ||
| {"uid": {"type": "Album", "id": "vacation"}, "attrs": {}, "parents": [{"type": "Account", "id": "account1"}]}, | ||
| {"uid": {"type": "Account", "id": "account1"}, "attrs": {}, "parents": []} | ||
| ] |
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,9 @@ | ||
| permit(principal == User::"alice", action == Action::"View_Photo", resource); | ||
|
|
||
| permit(principal, action == Action::"View_Photo", resource in Album::"vacation"); | ||
|
|
||
| forbid(principal, action, resource) when { resource.private }; | ||
|
|
||
| permit(principal, action == Action::"Edit_Photo", resource) when { principal == resource.owner }; | ||
|
|
||
| permit(principal, action == Action::"Delete_Photo", resource) when { principal == resource.owner }; |
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,5 @@ | ||
| [ | ||
| {"uid": {"type": "User", "id": "alice"}, "attrs": {}, "parents": []}, | ||
| {"uid": {"type": "Action", "id": "view"}, "attrs": {}, "parents": []}, | ||
| {"uid": {"type": "Resource", "id": "doc1"}, "attrs": {}, "parents": []} | ||
| ] |
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 @@ | ||
| permit(principal, action, resource); |
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
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: I think if we anticipate adding more scenarios, we might want to rename this (perhaps
basicPhotoFlashScenario), but I think this is good as is