Skip to content

Commit

Permalink
Improve consistency with unified code format/imports (#3156)
Browse files Browse the repository at this point in the history
Configures the spring-javaformat plugin to enforce and apply code formatting as part of the build. Applies the formatting to existing code. Also adds to editorconfig configuration for import order and updates all imports. There is no generic support for Java import order configuration in editorconfig, so an IntelliJ specific configuration is used.
  • Loading branch information
shakuzen committed May 6, 2022
1 parent d8b06e7 commit 4f3cf5a
Show file tree
Hide file tree
Showing 679 changed files with 13,655 additions and 12,263 deletions.
3 changes: 3 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ insert_final_newline = true
indent_style = space
indent_size = 4
continuation_indent_size = 8
ij_java_imports_layout = *,|,javax.**,java.**,|,$*
ij_java_class_count_to_use_import_on_demand = 5
ij_java_names_count_to_use_import_on_demand = 3
2 changes: 2 additions & 0 deletions .springjavaformatconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
java-baseline=8
indentation-style=spaces
7 changes: 7 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ The wrapper can be used with a command, for example, `./gradlew check` to build

This repository should be imported as a Gradle project into your IDE of choice.

## Code formatting

The [spring-javaformat plugin](https://github.com/spring-io/spring-javaformat) is configured to check and apply consistent formatting in the codebase through the build.
The `checkFormat` task checks the formatting as part of the `check` task.
Apply formatting with the `format` task.
You should rely on the formatting the `format` task applies instead of your IDE's configured formatting.

## Testing changes locally

Specific modules or a test class can be run from your IDE for convenience.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,12 @@
@OutputTimeUnit(TimeUnit.NANOSECONDS)
@Threads(16)
public class CompareCountersWithOtherLibraries {

@State(Scope.Benchmark)
public static class DropwizardState {

com.codahale.metrics.MetricRegistry registry;

com.codahale.metrics.Counter counter;

@Setup(Level.Trial)
Expand All @@ -52,12 +55,16 @@ public void setup() {
public void tearDown(Blackhole hole) {
hole.consume(counter.getCount());
}

}

@State(Scope.Benchmark)
public static class MicrometerState {

io.micrometer.core.instrument.MeterRegistry registry;

io.micrometer.core.instrument.Counter counter;

io.micrometer.core.instrument.Counter counterWithTags;

@Setup(Level.Trial)
Expand All @@ -75,13 +82,16 @@ public void tearDown(Blackhole hole) {
}
}
}

}

@State(Scope.Benchmark)
public static class Dropwizard5State {

io.dropwizard.metrics5.MetricRegistry registry;

io.dropwizard.metrics5.Counter counter;

io.dropwizard.metrics5.Counter counterWithTags;

@Setup(Level.Trial)
Expand All @@ -100,39 +110,44 @@ public void tearDown(Blackhole hole) {
hole.consume(c.getCount());
}
}

}

@State(Scope.Benchmark)
public static class PrometheusState {

io.prometheus.client.Counter counter;

io.prometheus.client.Counter counterWithTags;

@Setup(Level.Trial)
public void setup() {
counter = io.prometheus.client.Counter.build().name("counter").help("A counter").create();
counterWithTags = io.prometheus.client.Counter.build().name("counter").help("Counter with two tags declared").labelNames("key1", "key2").register();
counterWithTags = io.prometheus.client.Counter.build().name("counter")
.help("Counter with two tags declared").labelNames("key1", "key2").register();
}

}

// @Benchmark
// @Benchmark
public void dropwizard5Counter(Dropwizard5State state) {
state.counter.inc();
}

// @Benchmark
// @Benchmark
public void dropwizard5CounterFixedTags(Dropwizard5State state) {
state.counterWithTags.inc();
}

// @Benchmark
// @Benchmark
public void dropwizard5CounterTags(Dropwizard5State state) {
Map<String, String> tags = new HashMap<>();
tags.put("key1", "value1");
tags.put("key2", "value2");
state.registry.counter(new io.dropwizard.metrics5.MetricName("tagged", tags)).inc();
}

// @Benchmark
// @Benchmark
public void micrometerCounter(MicrometerState state) {
state.counter.increment();
}
Expand All @@ -142,12 +157,12 @@ public void micrometerCounterTags(MicrometerState state) {
state.registry.counter("dynamicTags", "key1", "value1", "key2", "value2").increment();
}

// @Benchmark
// @Benchmark
public void micrometerCounterFixedTags(MicrometerState state) {
state.counterWithTags.increment();
}

// @Benchmark
// @Benchmark
public void prometheusCounter(PrometheusState state) {
state.counter.inc();
}
Expand All @@ -158,10 +173,9 @@ public void prometheusCounterWithTags(PrometheusState state) {
}

public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(CompareCountersWithOtherLibraries.class.getSimpleName())
.addProfiler(GCProfiler.class)
.build();
Options opt = new OptionsBuilder().include(CompareCountersWithOtherLibraries.class.getSimpleName())
.addProfiler(GCProfiler.class).build();
new Runner(opt).run();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
//CHECKSTYLE:OFF
import com.google.common.collect.Iterators;
import com.google.common.primitives.Doubles;
////CHECKSTYLE:ON
//CHECKSTYLE:ON
import io.micrometer.core.instrument.Clock;
import io.micrometer.core.instrument.DistributionSummary;
import io.micrometer.core.instrument.distribution.DistributionStatisticConfig;
Expand Down Expand Up @@ -63,30 +63,30 @@ public static class Data {
@Setup(Level.Iteration)
public void setup() {
final Random r = new Random(1234567891L);
dataIterator = Iterators.cycle(
Stream.generate(() -> Math.round(Math.exp(2.0 + r.nextGaussian()))).limit(1048576)
.collect(Collectors.toList()));
dataIterator = Iterators.cycle(Stream.generate(() -> Math.round(Math.exp(2.0 + r.nextGaussian())))
.limit(1048576).collect(Collectors.toList()));
}

}

@State(Scope.Benchmark)
public static class DropwizardState {

MetricRegistry registry;

Histogram histogram;

Histogram histogramSlidingTimeWindow;

Histogram histogramUniform;

@Setup(Level.Iteration)
public void setup() {
registry = new MetricRegistry();
histogram = registry.histogram("histogram");
histogramSlidingTimeWindow =
registry.register("slidingTimeWindowHistogram",
new Histogram(new SlidingTimeWindowReservoir(10, TimeUnit.SECONDS)));
histogramUniform =
registry.register("uniformHistogram",
new Histogram(new UniformReservoir()));
histogramSlidingTimeWindow = registry.register("slidingTimeWindowHistogram",
new Histogram(new SlidingTimeWindowReservoir(10, TimeUnit.SECONDS)));
histogramUniform = registry.register("uniformHistogram", new Histogram(new UniformReservoir()));
}

@TearDown(Level.Iteration)
Expand All @@ -95,33 +95,34 @@ public void tearDown(Blackhole hole) {
hole.consume(histogramSlidingTimeWindow.getSnapshot().getMedian());
hole.consume(histogramUniform.getSnapshot().getMedian());
}

}

@State(Scope.Benchmark)
public static class MicrometerState {

io.micrometer.core.instrument.MeterRegistry registry;

io.micrometer.core.instrument.DistributionSummary summary;

@Setup(Level.Iteration)
public void setup() {
registry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT, new CollectorRegistry(),
Clock.SYSTEM);
summary = DistributionSummary.builder("summary")
.publishPercentileHistogram()
.register(registry);
registry = new PrometheusMeterRegistry(PrometheusConfig.DEFAULT, new CollectorRegistry(), Clock.SYSTEM);
summary = DistributionSummary.builder("summary").publishPercentileHistogram().register(registry);
}

@TearDown(Level.Iteration)
public void tearDown(Blackhole hole) {
hole.consume(summary.takeSnapshot().count());
}

}

@State(Scope.Benchmark)
public static class MicrometerPlainSummaryState {

io.micrometer.core.instrument.MeterRegistry registry;

io.micrometer.core.instrument.DistributionSummary summary;

@Setup(Level.Iteration)
Expand All @@ -134,6 +135,7 @@ public void setup() {
public void tearDown(Blackhole hole) {
hole.consume(summary.takeSnapshot().count());
}

}

@State(Scope.Benchmark)
Expand All @@ -143,55 +145,54 @@ public static class PrometheusState {

@Setup(Level.Trial)
public void setup() {
double[] micrometerBuckets =
Doubles.toArray(PercentileHistogramBuckets.buckets(
DistributionStatisticConfig.builder().minimumExpectedValue(0.0).maximumExpectedValue(Double.POSITIVE_INFINITY)
.percentilesHistogram(true).build()));
histogram = io.prometheus.client.Histogram.build("histogram", "A histogram")
.buckets(micrometerBuckets).create();
double[] micrometerBuckets = Doubles.toArray(
PercentileHistogramBuckets.buckets(DistributionStatisticConfig.builder().minimumExpectedValue(0.0)
.maximumExpectedValue(Double.POSITIVE_INFINITY).percentilesHistogram(true).build()));
histogram = io.prometheus.client.Histogram.build("histogram", "A histogram").buckets(micrometerBuckets)
.create();
}

@TearDown(Level.Iteration)
public void tearDown(Blackhole hole) {
hole.consume(histogram.collect());
}

}

@Benchmark
public void micrometerPlainHistogram(MicrometerPlainSummaryState state, Data data) {
state.summary.record(1);
}

// @Benchmark
// @Benchmark
public void micrometerHistogram(MicrometerState state, Data data) {
state.summary.record(data.dataIterator.next());
}

// @Benchmark
// @Benchmark
public void dropwizardHistogram(DropwizardState state, Data data) {
state.histogram.update(data.dataIterator.next());
}

// This benchmark is likely broken, results vary wildly between runs.
// @Benchmark
// @Benchmark
public void dropwizardHistogramSlidingTimeWindow(DropwizardState state, Data data) {
state.histogramSlidingTimeWindow.update(data.dataIterator.next());
}

// @Benchmark
// @Benchmark
public void dropwizardHistogramUniform(DropwizardState state, Data data) {
state.histogramUniform.update(data.dataIterator.next());
}

// @Benchmark
// @Benchmark
public void prometheusHistogram(PrometheusState state, Data data) {
state.histogram.observe(data.dataIterator.next());
}

public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(CompareHistogramsWithOtherLibraries.class.getSimpleName())
.build();
Options opt = new OptionsBuilder().include(CompareHistogramsWithOtherLibraries.class.getSimpleName()).build();
new Runner(opt).run();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,27 @@
@State(Scope.Benchmark)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class CounterBenchmark {

public static void main(String[] args) throws RunnerException {
// @formatter:off
Options opt = new OptionsBuilder()
.include(CounterBenchmark.class.getSimpleName())
.warmupIterations(5)
.measurementIterations(10)
.mode(Mode.SampleTime)
.forks(1)
.build();
// @formatter:on

new Runner(opt).run();
}

private int x = 923;

private int y = 123;

private MeterRegistry registry;

private Counter counter;

@Setup
Expand All @@ -70,4 +75,5 @@ public int countSumWithRegistryLookup() {
public int sum() {
return x + y;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@
@State(Scope.Benchmark)
@OutputTimeUnit(TimeUnit.MICROSECONDS)
public class MeterRegistrationBenchmark {

public static void main(String[] args) throws RunnerException {
// @formatter:off
Options opt = new OptionsBuilder()
.include(MeterRegistrationBenchmark.class.getSimpleName())
.warmupIterations(2)
Expand All @@ -37,11 +39,13 @@ public static void main(String[] args) throws RunnerException {
.timeUnit(TimeUnit.SECONDS)
.forks(1)
.build();
// @formatter:on

new Runner(opt).run();
}

private int x = 923;

private int y = 123;

@Benchmark
Expand All @@ -57,4 +61,5 @@ public int insert10_000() {
public int sum() {
return x + y;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.NANOSECONDS)
public class TagsBenchmark {

@Threads(16)
@Benchmark
public void of() {
Expand All @@ -43,9 +44,8 @@ public void dotAnd() {
}

public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(TagsBenchmark.class.getSimpleName())
.build();
Options opt = new OptionsBuilder().include(TagsBenchmark.class.getSimpleName()).build();
new Runner(opt).run();
}

}
Loading

0 comments on commit 4f3cf5a

Please sign in to comment.