From e4a9113713841f42805e1f34b531189843f10e57 Mon Sep 17 00:00:00 2001 From: Calum Murray Date: Tue, 26 Sep 2023 15:14:30 -0400 Subject: [PATCH] Added any filter benchmarks which mirror those in eventing core (#3361) * Added any filter benchmarks to mirror those in eventing core Signed-off-by: Calum Murray * Fixed Any filter benchmarks to use static classes only Signed-off-by: Calum Murray --------- Signed-off-by: Calum Murray --- .../impl/filter/AnyFilterBenchmark.java | 131 ++++++++++++++++++ .../impl/filter/ExactFilterBenchmark.java | 93 ++++++------- .../impl/filter/FilterBenchmark.java | 43 +++--- .../dispatcher/impl/filter/SampleEvent.java | 45 ++++++ 4 files changed, 242 insertions(+), 70 deletions(-) create mode 100644 data-plane/benchmarks/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/AnyFilterBenchmark.java create mode 100644 data-plane/benchmarks/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/SampleEvent.java diff --git a/data-plane/benchmarks/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/AnyFilterBenchmark.java b/data-plane/benchmarks/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/AnyFilterBenchmark.java new file mode 100644 index 0000000000..87d9c0fcd5 --- /dev/null +++ b/data-plane/benchmarks/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/AnyFilterBenchmark.java @@ -0,0 +1,131 @@ +/* + * Copyright © 2018 Knative Authors (knative-dev@googlegroups.com) + * + * 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 + * + * 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 dev.knative.eventing.kafka.broker.dispatcher.impl.filter; + +import dev.knative.eventing.kafka.broker.dispatcher.Filter; +import dev.knative.eventing.kafka.broker.dispatcher.impl.filter.subscriptionsapi.*; +import io.cloudevents.CloudEvent; +import java.util.Map; +import java.util.Set; + +public class AnyFilterBenchmark { + public static CloudEvent event() { + final var returnFirst = Math.random() > 0.5; + if (returnFirst) { + return SampleEvent.event(); + } else { + return SampleEvent.otherEvent(); + } + } + + public static ExactFilter makeExactFilter() { + return new ExactFilter(Map.of("id", "com.github.pull.create")); + } + + public static PrefixFilter makePrefixFilter() { + return new PrefixFilter(Map.of("type", "com.g")); + } + + public static SuffixFilter makeSuffixFilter() { + return new SuffixFilter(Map.of("source", "lhost")); + } + + public static PrefixFilter makePrefixFilterNoMatch() { + return new PrefixFilter(Map.of("type", "other.event")); + } + + public static SuffixFilter makeSufficFilterNoMatch() { + return new SuffixFilter(Map.of("source", "qwertyuiop")); + } + + public static class AnyFilterWithExactFilterBenchmark extends FilterBenchmark { + + @Override + protected Filter createFilter() { + return new AnyFilter(Set.of(makeExactFilter())); + } + + @Override + protected CloudEvent createEvent() { + return SampleEvent.event(); + } + } + + public static class AnyFilterMatchAllSubfilters extends FilterBenchmark { + + @Override + protected Filter createFilter() { + return new AnyFilter(Set.of(makeExactFilter(), makePrefixFilter(), makeSuffixFilter())); + } + + @Override + protected CloudEvent createEvent() { + return SampleEvent.event(); + } + } + + public static class AnyFilterFirstMatchAtEnd extends FilterBenchmark { + + @Override + protected Filter createFilter() { + return new AnyFilter(Set.of(makePrefixFilterNoMatch(), makeSufficFilterNoMatch(), makeExactFilter())); + } + + @Override + protected CloudEvent createEvent() { + return SampleEvent.event(); + } + } + + public static class AnyFilterFirstMatchAtStart extends FilterBenchmark { + + @Override + protected Filter createFilter() { + return new AnyFilter(Set.of(makeExactFilter(), makePrefixFilterNoMatch(), makeSufficFilterNoMatch())); + } + + @Override + protected CloudEvent createEvent() { + return SampleEvent.event(); + } + } + + public static class AnyFilter2EventsMatch2DifferentFilters extends FilterBenchmark { + + @Override + protected Filter createFilter() { + return new AnyFilter(Set.of(makePrefixFilter(), makePrefixFilterNoMatch())); + } + + @Override + protected CloudEvent createEvent() { + return event(); + } + } + + public static class AnyFilter2EventsMatch2DifferentFiltersOneFilterMatchesNeither extends FilterBenchmark { + @Override + protected Filter createFilter() { + return new AnyFilter(Set.of(makeSufficFilterNoMatch(), makePrefixFilter(), makePrefixFilterNoMatch())); + } + + @Override + protected CloudEvent createEvent() { + return event(); + } + } +} diff --git a/data-plane/benchmarks/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/ExactFilterBenchmark.java b/data-plane/benchmarks/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/ExactFilterBenchmark.java index da14ec7c57..d223723612 100644 --- a/data-plane/benchmarks/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/ExactFilterBenchmark.java +++ b/data-plane/benchmarks/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/ExactFilterBenchmark.java @@ -21,69 +21,66 @@ import io.cloudevents.CloudEvent; import io.cloudevents.core.builder.CloudEventBuilder; import io.cloudevents.core.v1.CloudEventV1; - import java.net.URI; import java.util.Map; public class ExactFilterBenchmark { - public static CloudEvent event() { - return CloudEventBuilder.v1() - .withId("abc") - .withSource(URI.create("http://localhost")) - .withType("test") - .withDataSchema(URI.create("/api/schema")) - .withDataContentType("testContentType") - .withSubject("testSubject") - .build(); - } + public static CloudEvent event() { + return CloudEventBuilder.v1() + .withId("abc") + .withSource(URI.create("http://localhost")) + .withType("test") + .withDataSchema(URI.create("/api/schema")) + .withDataContentType("testContentType") + .withSubject("testSubject") + .build(); + } - public static class ExactFilterBenchmarkExactMatchID extends FilterBenchmark { + public static class ExactFilterBenchmarkExactMatchID extends FilterBenchmark { - @Override - protected Filter createFilter() { - return new ExactFilter(Map.of(CloudEventV1.ID, "abc")); - } + @Override + protected Filter createFilter() { + return new ExactFilter(Map.of(CloudEventV1.ID, "abc")); + } - @Override - protected CloudEvent createEvent() { - return event(); + @Override + protected CloudEvent createEvent() { + return event(); + } } - } - public static class ExactFilterBenchmarkAllAttributes extends FilterBenchmark { + public static class ExactFilterBenchmarkAllAttributes extends FilterBenchmark { - @Override - protected Filter createFilter() { - return new ExactFilter(Map.of( - CloudEventV1.ID, "abc", - CloudEventV1.SOURCE, "http://localhost", - CloudEventV1.TYPE, "test", - CloudEventV1.DATASCHEMA,"/api/schema", - CloudEventV1.DATACONTENTTYPE, "testContentType", - CloudEventV1.SUBJECT, "testSubject" - )); - } + @Override + protected Filter createFilter() { + return new ExactFilter(Map.of( + CloudEventV1.ID, "abc", + CloudEventV1.SOURCE, "http://localhost", + CloudEventV1.TYPE, "test", + CloudEventV1.DATASCHEMA, "/api/schema", + CloudEventV1.DATACONTENTTYPE, "testContentType", + CloudEventV1.SUBJECT, "testSubject")); + } - @Override - protected CloudEvent createEvent() { - return event(); + @Override + protected CloudEvent createEvent() { + return event(); + } } - } - public static class ExactFilterBenchmarkNoMatch extends FilterBenchmark { + public static class ExactFilterBenchmarkNoMatch extends FilterBenchmark { - @Override - protected Filter createFilter() { - return new ExactFilter(Map.of( - CloudEventV1.ID, "qwertyuiopasdfghjklzxcvbnm", - CloudEventV1.SOURCE, "qwertyuiopasdfghjklzxcvbnm" - )); - } + @Override + protected Filter createFilter() { + return new ExactFilter(Map.of( + CloudEventV1.ID, "qwertyuiopasdfghjklzxcvbnm", + CloudEventV1.SOURCE, "qwertyuiopasdfghjklzxcvbnm")); + } - @Override - protected CloudEvent createEvent() { - return event(); + @Override + protected CloudEvent createEvent() { + return event(); + } } - } } diff --git a/data-plane/benchmarks/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/FilterBenchmark.java b/data-plane/benchmarks/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/FilterBenchmark.java index 33ac834b27..28ae301a4b 100644 --- a/data-plane/benchmarks/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/FilterBenchmark.java +++ b/data-plane/benchmarks/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/FilterBenchmark.java @@ -18,40 +18,39 @@ import dev.knative.eventing.kafka.broker.dispatcher.Filter; import io.cloudevents.CloudEvent; +import java.util.concurrent.TimeUnit; import org.openjdk.jmh.annotations.*; import org.openjdk.jmh.infra.Blackhole; -import java.util.concurrent.TimeUnit; - @BenchmarkMode(Mode.All) @Fork(1) @State(Scope.Thread) @OutputTimeUnit(TimeUnit.MICROSECONDS) public abstract class FilterBenchmark { - Filter filter; - CloudEvent cloudEvent; + Filter filter; + CloudEvent cloudEvent; - @Setup - public void setupFilter() { - this.filter = createFilter(); - } + @Setup + public void setupFilter() { + this.filter = createFilter(); + } - @Setup - public void setupCloudEvent() { - this.cloudEvent = createEvent(); - } + @Setup + public void setupCloudEvent() { + this.cloudEvent = createEvent(); + } - protected abstract Filter createFilter(); + protected abstract Filter createFilter(); - protected abstract CloudEvent createEvent(); + protected abstract CloudEvent createEvent(); - @Benchmark - public void benchmarkFilterCreation(Blackhole bh) { - bh.consume(this.createFilter()); - } + @Benchmark + public void benchmarkFilterCreation(Blackhole bh) { + bh.consume(this.createFilter()); + } - @Benchmark - public void benchmarkFilterEvaluation(Blackhole bh) { - bh.consume(this.filter.test(this.cloudEvent)); - } + @Benchmark + public void benchmarkFilterEvaluation(Blackhole bh) { + bh.consume(this.filter.test(this.cloudEvent)); + } } diff --git a/data-plane/benchmarks/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/SampleEvent.java b/data-plane/benchmarks/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/SampleEvent.java new file mode 100644 index 0000000000..6c230230a4 --- /dev/null +++ b/data-plane/benchmarks/src/main/java/dev/knative/eventing/kafka/broker/dispatcher/impl/filter/SampleEvent.java @@ -0,0 +1,45 @@ +/* + * Copyright © 2018 Knative Authors (knative-dev@googlegroups.com) + * + * 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 + * + * 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 dev.knative.eventing.kafka.broker.dispatcher.impl.filter; + +import io.cloudevents.CloudEvent; +import io.cloudevents.core.builder.CloudEventBuilder; +import java.net.URI; + +public class SampleEvent { + public static CloudEvent event() { + return CloudEventBuilder.v1() + .withId("abcdefghijklmnop") + .withSource(URI.create("http://localhost")) + .withType("com.github.pull.create") + .withDataSchema(URI.create("/api/schema")) + .withDataContentType("testContentType") + .withSubject("testSubject") + .build(); + } + + public static CloudEvent otherEvent() { + return CloudEventBuilder.v1() + .withId("abcdefghijklmnop") + .withSource(URI.create("http://localhost")) + .withType("other.event.type") + .withDataSchema(URI.create("/api/schema")) + .withDataContentType("testContentType") + .withSubject("testSubject") + .build(); + } +}