Skip to content

Commit

Permalink
Hacked up tracy capture during client tests
Browse files Browse the repository at this point in the history
  • Loading branch information
modmuss50 committed Dec 24, 2024
1 parent c6322ff commit 78d43a0
Show file tree
Hide file tree
Showing 5 changed files with 136 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ jobs:
with:
distribution: 'microsoft'
java-version: '21'
- run: brew install tracy
- run: mkdir run && echo "eula=true" >> run/eula.txt
- name: Run Client Gametests
uses: modmuss50/xvfb-action@v1
Expand All @@ -53,6 +54,10 @@ jobs:
with:
name: Test Screenshots
path: run/screenshots
- uses: actions/upload-artifact@v4
with:
name: Test Trace
path: run/profile.tracy

server_test:
runs-on: ubuntu-22.04
Expand Down
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -515,7 +515,8 @@ tasks.register('runProductionClientGametest', JavaExec) {
args(
"--assetIndex", loom.minecraftProvider.versionInfo.assetIndex().fabricId(loom.minecraftProvider.minecraftVersion()),
"--assetsDir", new File(loom.files.userCache, "assets").absolutePath,
"--gameDir", workingDir.absolutePath
"--gameDir", workingDir.absolutePath,
"--tracy"
)

if (Platform.CURRENT.operatingSystem.isMacOS()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* 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 net.fabricmc.fabric.impl.client.gametest.tracy;

import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;

import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class TracyCapture {
private static final Logger LOGGER = LoggerFactory.getLogger(TracyCapture.class);
private final Path output;

@Nullable
private Process process = null;

public TracyCapture(Path output) {
this.output = output;
}

public void startCapture() {
try {
Files.createDirectories(output.getParent());
LOGGER.info("Starting tracy-capture");
process = createProcess().start();
} catch (IOException e) {
throw new UncheckedIOException("Failed to start tracy-capture", e);
}

// Send ctrl+c on shutdown
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
if (process != null) {
LOGGER.info("Stopping tracy-capture");

try {
// Send ctrl+c
long pid = process.pid(); // Requires Java 9+
ProcessBuilder killProcess = new ProcessBuilder("kill", "-SIGINT", String.valueOf(pid));
killProcess.inheritIO().start();

process.waitFor();
LOGGER.info("tracy-capture finished with exit code {}", process.exitValue());
LOGGER.info(new String(process.getInputStream().readAllBytes()));
} catch (InterruptedException e) {
LOGGER.error("tracy-capture was interrupted", e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}));
}

private ProcessBuilder createProcess() {
return new ProcessBuilder()
.command(
"tracy-capture",
"-o", output.toAbsolutePath().toString(),
"-a", "127.0.0.1",
"-f"
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* Copyright (c) 2016, 2017, 2018, 2019 FabricMC
*
* 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 net.fabricmc.fabric.impl.client.gametest.tracy;

import java.nio.file.Path;
import java.util.Arrays;

import net.fabricmc.loader.api.FabricLoader;
import net.fabricmc.loader.api.entrypoint.PreLaunchEntrypoint;

public class TracyEntrypoint implements PreLaunchEntrypoint {
private static final boolean ENABLED = System.getProperty("fabric.client.gametest") != null;

private static TracyCapture tracyCapture;

@Override
public void onPreLaunch() {
if (!ENABLED) {
return;
}

if (!Arrays.asList(FabricLoader.getInstance().getLaunchArguments(true)).contains("--tracy")) {
return;
}

Path output = FabricLoader.getInstance().getGameDir().resolve("profile.tracy");
tracyCapture = new TracyCapture(output);
tracyCapture.startCapture();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,5 +26,10 @@
"accessWidener": "fabric-client-gametest-api-v1.accesswidener",
"custom": {
"fabric-api:module-lifecycle": "experimental"
},
"entrypoints": {
"preLaunch": [
"net.fabricmc.fabric.impl.client.gametest.tracy.TracyEntrypoint"
]
}
}

0 comments on commit 78d43a0

Please sign in to comment.