Skip to content

Commit

Permalink
Check if tags array is already sorted set before sorting.
Browse files Browse the repository at this point in the history
  • Loading branch information
mstyura committed May 23, 2024
1 parent dfbdde3 commit 6b96b4d
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package io.micrometer.benchmark.core;

import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.Tags;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
Expand All @@ -31,6 +32,60 @@
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class TagsBenchmark {

static final Tag[] orderedTagsSet10 = new Tag[] { Tag.of("key0", "value"), Tag.of("key1", "value"),
Tag.of("key2", "value"), Tag.of("key3", "value"), Tag.of("key4", "value"), Tag.of("key5", "value"),
Tag.of("key6", "value"), Tag.of("key7", "value"), Tag.of("key8", "value"), Tag.of("key9", "value") };

static final Tag[] orderedTagsSet4 = new Tag[] { Tag.of("key0", "value"), Tag.of("key1", "value"),
Tag.of("key2", "value"), Tag.of("key3", "value"), };

static final Tag[] orderedTagsSet2 = new Tag[] { Tag.of("key0", "value"), Tag.of("key1", "value"), };

static final Tag[] unorderedTagsSet10 = new Tag[] { Tag.of("key1", "value"), Tag.of("key2", "value"),
Tag.of("key3", "value"), Tag.of("key4", "value"), Tag.of("key5", "value"), Tag.of("key6", "value"),
Tag.of("key7", "value"), Tag.of("key8", "value"), Tag.of("key9", "value"), Tag.of("key0", "value") };

static final Tag[] unorderedTagsSet4 = new Tag[] { Tag.of("key1", "value"), Tag.of("key2", "value"),
Tag.of("key3", "value"), Tag.of("key0", "value"), };

static final Tag[] unorderedTagsSet2 = new Tag[] { Tag.of("key1", "value"), Tag.of("key0", "value") };

@Threads(16)
@Benchmark
public Tags tagsOfOrderedTagsSet10() {
return Tags.of(orderedTagsSet10);
}

@Threads(16)
@Benchmark
public Tags tagsOfOrderedTagsSet4() {
return Tags.of(orderedTagsSet4);
}

@Threads(16)
@Benchmark
public Tags tagsOfOrderedTagsSet2() {
return Tags.of(orderedTagsSet2);
}

@Threads(16)
@Benchmark
public Tags tagsOfUnorderedTagsSet10() {
return Tags.of(unorderedTagsSet10);
}

@Threads(16)
@Benchmark
public Tags tagsOfUnorderedTagsSet4() {
return Tags.of(unorderedTagsSet4);
}

@Threads(16)
@Benchmark
public Tags tagsOfUnorderedTagsSet2() {
return Tags.of(unorderedTagsSet2);
}

@Threads(16)
@Benchmark
public void of() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018 VMware, Inc.
* Copyright 2024 VMware, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -32,12 +32,12 @@
@State(Scope.Thread)
public class TagsMergeBenchmark {

Tags left = Tags.of("key", "value", "key2", "value2", "key6", "value6", "key7", "value7", "key8", "value8", "keyA",
"valueA", "keyC", "valueC", "keyE", "valueE", "keyF", "valueF", "keyG", "valueG", "keyG", "valueG", "keyG",
"valueG", "keyH", "valueH");
static final Tags left = Tags.of("key", "value", "key2", "value2", "key6", "value6", "key7", "value7", "key8",
"value8", "keyA", "valueA", "keyC", "valueC", "keyE", "valueE", "keyF", "valueF", "keyG", "valueG", "keyG",
"valueG", "keyG", "valueG", "keyH", "valueH");

Tags right = Tags.of("key", "value", "key1", "value1", "key2", "value2", "key3", "value3", "key4", "value4", "key5",
"value5", "keyA", "valueA", "keyB", "valueB", "keyD", "valueD");
static final Tags right = Tags.of("key", "value", "key1", "value1", "key2", "value2", "key3", "value3", "key4",
"value4", "key5", "value5", "keyA", "valueA", "keyB", "valueB", "keyD", "valueD");

@Threads(16)
@Benchmark
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,11 @@ private static boolean isSortedSet(Tag[] sortedSet, int length) {
}

private static Tags make(Tag[] tags) {
Arrays.sort(tags);
int len = dedup(tags);
int len = tags.length;
if (!isSortedSet(tags, len)) {
Arrays.sort(tags);
len = dedup(tags);
}
return new Tags(tags, len);
}

Expand Down

0 comments on commit 6b96b4d

Please sign in to comment.