Search in sources :

Example 1 with JsonObjectBuilder

use of co.aikar.timings.util.JSONUtil.JsonObjectBuilder in project SpongeCommon by SpongePowered.

the class TimingsExport method reportTimings.

/**
 * Builds an XML report of the timings to be uploaded for parsing.
 */
static void reportTimings() {
    if (TimingsExport.requestingReport.isEmpty()) {
        return;
    }
    TimingsReportListener listeners = new TimingsReportListener(TimingsExport.requestingReport);
    TimingsExport.requestingReport.clear();
    long now = System.currentTimeMillis();
    final long lastReportDiff = now - TimingsExport.lastReport;
    if (lastReportDiff < 60000) {
        listeners.send(Component.text("Please wait at least 1 minute in between Timings reports. (" + (int) ((60000 - lastReportDiff) / 1000) + " seconds)", NamedTextColor.RED));
        listeners.done();
        return;
    }
    final long lastStartDiff = now - TimingsManager.timingStart;
    if (lastStartDiff < 180000) {
        listeners.send(Component.text("Please wait at least 3 minutes before generating a Timings report. Unlike Timings v1, v2 benefits from longer timings and is not as useful with short timings. (" + (int) ((180000 - lastStartDiff) / 1000) + " seconds)", NamedTextColor.RED));
        listeners.done();
        return;
    }
    listeners.send(Component.text("Preparing Timings Report...", NamedTextColor.GREEN));
    TimingsExport.lastReport = now;
    Platform platform = SpongeCommon.game().platform();
    JsonObjectBuilder builder = JSONUtil.objectBuilder().add("version", platform.container(IMPLEMENTATION).metadata().version()).add("maxplayers", SpongeCommon.game().server().maxPlayers()).add("start", TimingsManager.timingStart / 1000).add("end", System.currentTimeMillis() / 1000).add("sampletime", (System.currentTimeMillis() - TimingsManager.timingStart) / 1000);
    if (!TimingsManager.privacy) {
        builder.add("server", TimingsExport.getServerName()).add("motd", PlainTextComponentSerializer.plainText().serialize(Sponge.server().motd())).add("online-mode", Sponge.server().isOnlineModeEnabled()).add("icon", SpongeCommon.server().getStatus().getFavicon());
    }
    final Runtime runtime = Runtime.getRuntime();
    RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
    builder.add("system", JSONUtil.objectBuilder().add("timingcost", TimingsExport.getCost()).add("name", System.getProperty("os.name")).add("version", System.getProperty("os.version")).add("jvmversion", System.getProperty("java.version")).add("arch", System.getProperty("os.arch")).add("maxmem", runtime.maxMemory()).add("cpu", runtime.availableProcessors()).add("runtime", ManagementFactory.getRuntimeMXBean().getUptime()).add("flags", TimingsExport.RUNTIME_FLAG_JOINER.join(runtimeBean.getInputArguments())).add("gc", JSONUtil.mapArrayToObject(ManagementFactory.getGarbageCollectorMXBeans(), (input) -> {
        return JSONUtil.singleObjectPair(input.getName(), JSONUtil.arrayOf(input.getCollectionCount(), input.getCollectionTime()));
    })));
    Set<BlockEntityType> blockEntityTypeSet = Sets.newHashSet();
    Set<EntityType<?>> entityTypeSet = Sets.newHashSet();
    int size = TimingsManager.HISTORY.size();
    TimingHistory[] history = new TimingHistory[size + 1];
    int i = 0;
    for (TimingHistory timingHistory : TimingsManager.HISTORY) {
        blockEntityTypeSet.addAll(timingHistory.tileEntityTypeSet);
        entityTypeSet.addAll(timingHistory.entityTypeSet);
        history[i++] = timingHistory;
    }
    // Current snapshot
    history[i] = new TimingHistory();
    blockEntityTypeSet.addAll(history[i].tileEntityTypeSet);
    entityTypeSet.addAll(history[i].entityTypeSet);
    JsonObjectBuilder handlersBuilder = JSONUtil.objectBuilder();
    for (TimingIdentifier.TimingGroup group : TimingIdentifier.GROUP_MAP.values()) {
        for (TimingHandler id : group.handlers) {
            if (!id.timed && !id.isSpecial()) {
                continue;
            }
            handlersBuilder.add(id.id, JSONUtil.arrayOf(group.id, id.name));
        }
    }
    builder.add("idmap", JSONUtil.objectBuilder().add("groups", JSONUtil.mapArrayToObject(TimingIdentifier.GROUP_MAP.values(), (group) -> JSONUtil.singleObjectPair(group.id, group.name))).add("handlers", handlersBuilder).add("worlds", JSONUtil.mapArrayToObject(TimingHistory.worldMap.entrySet(), (entry) -> JSONUtil.singleObjectPair(entry.getValue(), entry.getKey()))).add("blockentity", JSONUtil.mapArrayToObject(blockEntityTypeSet, (blockEntityType) -> {
        final ResourceKey resourceKey = Sponge.game().registry(RegistryTypes.BLOCK_ENTITY_TYPE).valueKey(blockEntityType);
        return JSONUtil.singleObjectPair(TimingsPls.getBlockEntityId(blockEntityType), resourceKey);
    })).add("entity", JSONUtil.mapArrayToObject(entityTypeSet, (entityType) -> {
        final ResourceKey resourceKey = (ResourceKey) (Object) Registry.ENTITY_TYPE.getKey((net.minecraft.world.entity.EntityType<?>) entityType);
        return JSONUtil.singleObjectPair(TimingsPls.getEntityId(entityType), resourceKey);
    })));
    // Information about loaded plugins
    builder.add("plugins", JSONUtil.mapArrayToObject(SpongeCommon.game().pluginManager().plugins(), (plugin) -> {
        @Nullable final URL homepageUrl = plugin.metadata().links().homepage().orElse(null);
        final String homepage = homepageUrl != null ? homepageUrl.toString() : "<not specified>";
        return JSONUtil.objectBuilder().add(plugin.metadata().id(), JSONUtil.objectBuilder().add("version", plugin.metadata().version()).add("description", plugin.metadata().description().orElse("")).add("website", homepage).add("authors", TimingsExport.AUTHOR_LIST_JOINER.join(plugin.metadata().contributors()))).build();
    }));
    // Information on the users Config
    builder.add("config", JSONUtil.objectBuilder().add("sponge", TimingsExport.serializeConfigNode(SpongeConfigs.getCommon().getNode())));
    new TimingsExport(listeners, builder.build(), history).start();
}
Also used : HttpURLConnection(java.net.HttpURLConnection) JsonObject(com.google.gson.JsonObject) ByteArrayOutputStream(java.io.ByteArrayOutputStream) URL(java.net.URL) ConfigurationNode(org.spongepowered.configurate.ConfigurationNode) ClickEvent(net.kyori.adventure.text.event.ClickEvent) BlockEntityType(org.spongepowered.api.block.entity.BlockEntityType) RconConnection(org.spongepowered.api.network.RconConnection) IMPLEMENTATION(org.spongepowered.api.Platform.Component.IMPLEMENTATION) Platform(org.spongepowered.api.Platform) JsonElement(com.google.gson.JsonElement) Registry(net.minecraft.core.Registry) InetAddress(java.net.InetAddress) Lists(com.google.common.collect.Lists) Component(net.kyori.adventure.text.Component) ResourceKey(org.spongepowered.api.ResourceKey) ManagementFactory(java.lang.management.ManagementFactory) Nullable(org.checkerframework.checker.nullness.qual.Nullable) JsonObjectBuilder(co.aikar.timings.util.JSONUtil.JsonObjectBuilder) OutputStream(java.io.OutputStream) RuntimeMXBean(java.lang.management.RuntimeMXBean) JSONUtil(co.aikar.timings.util.JSONUtil) Launch(org.spongepowered.common.launch.Launch) Sponge(org.spongepowered.api.Sponge) Set(java.util.Set) IOException(java.io.IOException) SpongeCommon(org.spongepowered.common.SpongeCommon) RegistryTypes(org.spongepowered.api.registry.RegistryTypes) Sets(com.google.common.collect.Sets) NamedTextColor(net.kyori.adventure.text.format.NamedTextColor) JsonArray(com.google.gson.JsonArray) List(java.util.List) Audience(net.kyori.adventure.audience.Audience) PlainTextComponentSerializer(net.kyori.adventure.text.serializer.plain.PlainTextComponentSerializer) SpongeConfigs(org.spongepowered.common.applaunch.config.core.SpongeConfigs) Entry(java.util.Map.Entry) EntityType(org.spongepowered.api.entity.EntityType) GZIPOutputStream(java.util.zip.GZIPOutputStream) Joiner(com.google.common.base.Joiner) InputStream(java.io.InputStream) BlockEntityType(org.spongepowered.api.block.entity.BlockEntityType) Platform(org.spongepowered.api.Platform) RuntimeMXBean(java.lang.management.RuntimeMXBean) URL(java.net.URL) ResourceKey(org.spongepowered.api.ResourceKey) BlockEntityType(org.spongepowered.api.block.entity.BlockEntityType) EntityType(org.spongepowered.api.entity.EntityType) JsonObjectBuilder(co.aikar.timings.util.JSONUtil.JsonObjectBuilder)

Aggregations

JSONUtil (co.aikar.timings.util.JSONUtil)1 JsonObjectBuilder (co.aikar.timings.util.JSONUtil.JsonObjectBuilder)1 Joiner (com.google.common.base.Joiner)1 Lists (com.google.common.collect.Lists)1 Sets (com.google.common.collect.Sets)1 JsonArray (com.google.gson.JsonArray)1 JsonElement (com.google.gson.JsonElement)1 JsonObject (com.google.gson.JsonObject)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 ManagementFactory (java.lang.management.ManagementFactory)1 RuntimeMXBean (java.lang.management.RuntimeMXBean)1 HttpURLConnection (java.net.HttpURLConnection)1 InetAddress (java.net.InetAddress)1 URL (java.net.URL)1 List (java.util.List)1 Entry (java.util.Map.Entry)1 Set (java.util.Set)1