From 3790ebb88930d9b592957b919650361e8b21cba1 Mon Sep 17 00:00:00 2001 From: Joseph Cosentino Date: Tue, 28 Nov 2023 13:44:53 -0800 Subject: [PATCH] feat: add benchmarking --- benchmark/README.md | 9 ++ benchmark/pom.xml | 121 ++++++++++++++++++ benchmark/run-benchmarks.sh | 18 +++ .../RuleExpressionEvaluationBenchmark.java | 74 +++++++++++ 4 files changed, 222 insertions(+) create mode 100644 benchmark/README.md create mode 100644 benchmark/pom.xml create mode 100755 benchmark/run-benchmarks.sh create mode 100644 benchmark/src/main/java/com/aws/greengrass/clientdevices/auth/benchmark/RuleExpressionEvaluationBenchmark.java diff --git a/benchmark/README.md b/benchmark/README.md new file mode 100644 index 000000000..85c4a994e --- /dev/null +++ b/benchmark/README.md @@ -0,0 +1,9 @@ +# Client Device Auth Benchmarking + +Benchmarks for main code paths in CDA, such as policy evaluation. + +## Quick Start + +``` +./run-benchmarks.sh +``` diff --git a/benchmark/pom.xml b/benchmark/pom.xml new file mode 100644 index 000000000..cad1a4658 --- /dev/null +++ b/benchmark/pom.xml @@ -0,0 +1,121 @@ + + + + 4.0.0 + + com.aws.greengrass + client-devices-auth-benchmark + benchmark + 2.5.0-SNAPSHOT + + jar + + + + org.openjdk.jmh + jmh-core + ${jmh.version} + + + org.openjdk.jmh + jmh-generator-annprocess + ${jmh.version} + provided + + + com.aws.greengrass + client-devices-auth + 2.5.0-SNAPSHOT + + + + + UTF-8 + 1.23 + 1.8 + benchmarks + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.8.0 + + ${javac.target} + ${javac.target} + ${javac.target} + + + + org.apache.maven.plugins + maven-shade-plugin + 3.2.2 + + + package + + shade + + + ${uberjar.name} + + + org.openjdk.jmh.Main + + + + + + + *:* + + META-INF/*.SF + META-INF/*.DSA + META-INF/*.RSA + + + + + + + + + + + + + + true + + + false + + central + https://repo1.maven.org/maven2 + + + + + + + true + + + true + + central + https://repo1.maven.org/maven2 + + + diff --git a/benchmark/run-benchmarks.sh b/benchmark/run-benchmarks.sh new file mode 100755 index 000000000..f0dba3686 --- /dev/null +++ b/benchmark/run-benchmarks.sh @@ -0,0 +1,18 @@ +#!/usr/bin/env bash +# +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. +# SPDX-License-Identifier: Apache-2.0 +# + +set -e + +# install CDA locally +cd ./.. +mvn clean install -DskipTests +cd - + +# build benchmark project +mvn clean package + +# run all benchmarks +java -jar target/benchmarks.jar diff --git a/benchmark/src/main/java/com/aws/greengrass/clientdevices/auth/benchmark/RuleExpressionEvaluationBenchmark.java b/benchmark/src/main/java/com/aws/greengrass/clientdevices/auth/benchmark/RuleExpressionEvaluationBenchmark.java new file mode 100644 index 000000000..932acd427 --- /dev/null +++ b/benchmark/src/main/java/com/aws/greengrass/clientdevices/auth/benchmark/RuleExpressionEvaluationBenchmark.java @@ -0,0 +1,74 @@ +/* + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. + * SPDX-License-Identifier: Apache-2.0 + */ + +package com.aws.greengrass.clientdevices.auth.benchmark; + +import com.aws.greengrass.clientdevices.auth.configuration.ExpressionVisitor; +import com.aws.greengrass.clientdevices.auth.configuration.parser.ASTStart; +import com.aws.greengrass.clientdevices.auth.configuration.parser.RuleExpression; +import com.aws.greengrass.clientdevices.auth.session.Session; +import com.aws.greengrass.clientdevices.auth.session.attribute.AttributeProvider; +import com.aws.greengrass.clientdevices.auth.session.attribute.DeviceAttribute; +import com.aws.greengrass.clientdevices.auth.session.attribute.WildcardSuffixAttribute; +import org.openjdk.jmh.annotations.Benchmark; +import org.openjdk.jmh.annotations.BenchmarkMode; +import org.openjdk.jmh.annotations.Mode; + +import java.io.StringReader; + + +public class RuleExpressionEvaluationBenchmark { + + @Benchmark + @BenchmarkMode(Mode.Throughput) + public void evaluateThingExpressionWithThingName() throws Exception { + ASTStart tree = new RuleExpression(new StringReader("thingName: MyThingName")).Start(); + Session session = new FakeSession("MyThingName"); + new ExpressionVisitor().visit(tree, session); + } + + @Benchmark + @BenchmarkMode(Mode.Throughput) + public void evaluateWildcardPrefixThingExpressionWithThingName() throws Exception { + ASTStart tree = new RuleExpression(new StringReader("thingName: *ThingName")).Start(); + Session session = new FakeSession("MyThingName"); + new ExpressionVisitor().visit(tree, session); + } + + @Benchmark + @BenchmarkMode(Mode.Throughput) + public void evaluateWildcardSuffixThingExpressionWithThingName() throws Exception { + ASTStart tree = new RuleExpression(new StringReader("thingName: MyThing*")).Start(); + Session session = new FakeSession("MyThingName"); + new ExpressionVisitor().visit(tree, session); + } + + @Benchmark + @BenchmarkMode(Mode.Throughput) + public void evaluateWildcardPrefixAndSuffixThingExpressionWithThingName() throws Exception { + ASTStart tree = new RuleExpression(new StringReader("thingName: *ThingName*")).Start(); + Session session = new FakeSession("MyThingName"); + new ExpressionVisitor().visit(tree, session); + } + + static class FakeSession implements Session { + + private final DeviceAttribute attribute; + + FakeSession(String thingName) { + this.attribute = new WildcardSuffixAttribute(thingName); + } + + @Override + public AttributeProvider getAttributeProvider(String attributeProviderNameSpace) { + throw new UnsupportedOperationException(); + } + + @Override + public DeviceAttribute getSessionAttribute(String attributeNamespace, String attributeName) { + return attribute; + } + } +}