Skip to content

Commit

Permalink
Automatic bench-marker and improved usage information
Browse files Browse the repository at this point in the history
  • Loading branch information
RhysB committed Aug 6, 2024
1 parent b4b80b0 commit 3943782
Show file tree
Hide file tree
Showing 5 changed files with 164 additions and 7 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,7 @@ modules.xml
hs_err_pid*

# End of https://www.gitignore.io/api/java,intellij,intellij+all,intellij+iml

target/

*.lck
49 changes: 45 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,48 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>Server</artifactId>
<version>1.0-SNAPSHOT</version>
</project>
<groupId>com.johnymuffin</groupId>
<artifactId>mcregion-to-anvil-converter</artifactId>
<version>1.0.0</version>

<build>
<plugins>
<!-- Maven Compiler Plugin -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

<!-- Maven Shade Plugin to create a fat JAR -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.2.4</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>net.minecraft.world.level.storage.AnvilConverter</mainClass> <!-- Replace with your main class -->
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencies>
<!-- Add your project dependencies here -->
</dependencies>
</project>
64 changes: 64 additions & 0 deletions src/main/java/AutomaticBenchmarker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import net.minecraft.world.level.storage.AnvilConverter;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.logging.*;

public class AutomaticBenchmarker {

private static final Logger logger = Logger.getLogger(AutomaticBenchmarker.class.getName());

public static void main(String[] args) {
setupLogger();

// Notice: This program performs automatic benchmarking of the AnvilConverter application.
logger.info("Starting automatic benchmarking of the AnvilConverter application.");

// Fake arguments for the AnvilConverter application
String baseFolderPath = "E:\\RetroMC-1\\";
String worldName = "retromc";
int[] threadCounts = {32, 16, 8, 4, 2, 1, 0}; // 0 runs the converter sequentially on the main thread

for (int numThreads : threadCounts) {
logger.info("Running benchmark with " + numThreads + " threads.");
AnvilConverter.main(new String[]{baseFolderPath, worldName, String.valueOf(numThreads)});

// Cleanup JVM
System.gc();

// Sleep for 1 minute to allow the JVM to cleanup
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
logger.log(Level.WARNING, "Thread interrupted while sleeping", e);
}
}
}

private static void setupLogger() {
try {
// Create a file handler that writes log record to a file called benchmark.log
FileHandler fileHandler = new FileHandler("benchmark.log", true);
fileHandler.setFormatter(new SimpleFormatter());

// Add the file handler to the logger
logger.addHandler(fileHandler);

// Set the logger level to INFO
logger.setLevel(Level.INFO);

// Also log to the console
ConsoleHandler consoleHandler = new ConsoleHandler();
consoleHandler.setFormatter(new SimpleFormatter());
logger.addHandler(consoleHandler);

// Redirect System.out and System.err to logger
PrintStream logStream = new PrintStream(new DualOutputStream(System.out, new FileOutputStream("benchmark.log", true)));
System.setOut(logStream);
System.setErr(logStream);

} catch (IOException e) {
logger.log(Level.SEVERE, "Failed to setup logger", e);
}
}
}
43 changes: 43 additions & 0 deletions src/main/java/DualOutputStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.IOException;

public class DualOutputStream extends OutputStream {
private final OutputStream outputStream1;
private final OutputStream outputStream2;

public DualOutputStream(OutputStream outputStream1, OutputStream outputStream2) {
this.outputStream1 = outputStream1;
this.outputStream2 = outputStream2;
}

@Override
public void write(int b) throws IOException {
outputStream1.write(b);
outputStream2.write(b);
}

@Override
public void write(byte[] b) throws IOException {
outputStream1.write(b);
outputStream2.write(b);
}

@Override
public void write(byte[] b, int off, int len) throws IOException {
outputStream1.write(b, off, len);
outputStream2.write(b, off, len);
}

@Override
public void flush() throws IOException {
outputStream1.flush();
outputStream2.flush();
}

@Override
public void close() throws IOException {
outputStream1.close();
outputStream2.close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,20 @@ public void progressStage(String string) {

private static void printUsageAndExit() {
System.out.println("Map converter for Minecraft, from format \"McRegion\" to \"Anvil\". (c) Mojang AB 2012");
System.out.println("");
System.out.println("Forked by RhysB to include multi-threaded conversion support.");
System.out.println();
System.out.println("Usage:");
System.out.println("\tjava -jar AnvilConverter.jar <base folder> <world name>");
System.out.println("\tjava -jar AnvilConverter.jar <base folder> <world name> [thread count]");
System.out.println("Where:");
System.out.println("\t<base folder>\tThe full path to the folder containing Minecraft world folders");
System.out.println("\t<world name>\tThe folder name of the Minecraft world to be converted");
System.out.println("\t[thread count]\t(Optional) Number of threads to use for conversion. Defaults to 1 if not specified.");
System.out.println("Example:");
System.out.println("\tjava -jar AnvilConverter.jar /home/jeb_/minecraft world");
System.out.println("\tjava -jar AnvilConverter.jar /home/jeb_/minecraft world - Convert world using 1 thread");
System.out.println("\tjava -jar AnvilConverter.jar /home/jeb_/minecraft world 0 - Convert world sequentially on the main thread");
System.out.println("\tjava -jar AnvilConverter.jar /home/jeb_/minecraft world 4 - Convert world using 4 threads");
System.exit(1);
}


}

0 comments on commit 3943782

Please sign in to comment.