Skip to content

Commit

Permalink
bench
Browse files Browse the repository at this point in the history
  • Loading branch information
yegor256 committed Jan 23, 2025
1 parent d48c39e commit 6286182
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,9 @@ private static Map<String, String> programObjects(final XML xmir) {
.collect(
Collectors.toMap(
names::get,
pos -> xmir.xpath("/program/objects/o/@line").get(pos),
pos -> xmir.xpath(
String.format("/program/objects/o[%d]/@line", pos + 1)
).stream().findFirst().orElse("0"),
(existing, replacement) -> replacement
)
);
Expand Down
4 changes: 2 additions & 2 deletions src/test/java/benchmarks/ProgramBench.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@
@Fork(1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 1, time = 1, timeUnit = TimeUnit.MILLISECONDS)
@Measurement(iterations = 2, time = 1, timeUnit = TimeUnit.MILLISECONDS)
@Warmup(iterations = 1)
@Measurement(iterations = 3, time = 1, timeUnit = TimeUnit.MILLISECONDS)
@State(Scope.Benchmark)
public class ProgramBench {

Expand Down
85 changes: 85 additions & 0 deletions src/test/java/benchmarks/ProgramsBench.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* The MIT License (MIT)
*
* Copyright (c) 2016-2025 Objectionary.com
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package benchmarks;

import fixtures.LargeXmir;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.TimeUnit;
import org.cactoos.scalar.IoChecked;
import org.eolang.lints.Program;
import org.eolang.lints.Programs;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.Fork;
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.State;
import org.openjdk.jmh.annotations.Warmup;

/**
* Benchmark for {@link Program}.
*
* @since 0.0.34
* @checkstyle DesignForExtensionCheck (10 lines)
* @checkstyle NonStaticMethodCheck (100 lines)
*/
@Fork(1)
@BenchmarkMode(Mode.AverageTime)
@OutputTimeUnit(TimeUnit.MILLISECONDS)
@Warmup(iterations = 1)
@Measurement(iterations = 3, time = 1, timeUnit = TimeUnit.MILLISECONDS)
@State(Scope.Benchmark)
public class ProgramsBench {

/**
* Large XMIR document.
*/
private final Path home;

public ProgramsBench() {
try {
this.home = Files.createTempDirectory("tmp");
for (int idx = 0; idx < 10; ++idx) {
final String name = String.format("program-%d.xmir", idx);
Files.write(
this.home.resolve(String.format("%s.xmir", name)),
new IoChecked<>(new LargeXmir(name))
.value().toString().getBytes(StandardCharsets.UTF_8)
);
}
} catch (final IOException ex) {
throw new IllegalArgumentException(ex);
}
}

@Benchmark
public final void scansLargeProgram() throws IOException {
new Programs(this.home).defects();
}
}
18 changes: 17 additions & 1 deletion src/test/java/fixtures/LargeXmir.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
import org.cactoos.bytes.BytesOf;
import org.cactoos.bytes.UncheckedBytes;
import org.cactoos.io.ResourceOf;
import org.xembly.Directives;
import org.xembly.Xembler;

/**
* Large XMIR document.
Expand All @@ -41,6 +43,16 @@
*/
public final class LargeXmir implements Scalar<XML> {

private final String name;

public LargeXmir() {
this("unknown");
}

public LargeXmir(final String nme) {
this.name = nme;
}

@Override
public XML value() throws Exception {
final Path home = Files.createTempDirectory("tmp");
Expand Down Expand Up @@ -76,6 +88,10 @@ public XML value() throws Exception {
);
}
);
return ref.get();
final XML xml = ref.get();
new Xembler(
new Directives().xpath("/program").attr("name", this.name)
).apply(xml.inner());
return xml;
}
}

0 comments on commit 6286182

Please sign in to comment.