#Simple TPS Counter

1 messages Β· Page 1 of 1 (latest)

peak falcon
#

Here's a simple TPS counter (Tick per Second) for anyone who just wants to copy-paste it into their code.

TPS here (The one you want to use) is a decimal number. You can round it to the nearest integer if you like.

import { system } from "@minecraft/server"

const interval = 10 //delay between check
let TPS = 20 //Use this on the rest of your codes

let lastDate = Date.now()
system.runInterval(()=>{
    const currDate = Date.now()
    TPS = interval*50/(currDate-lastDate)*20
    lastDate = currDate
},interval)

Other variations:

  • [Visual1mpact's]( #1281437513675964446 message)
glossy bobcat
#

why are you multiplying the tick interval by 50?

peak falcon
feral owl
#

Let's take these values:
currDate = 20
lastDate = 10
interval = 10

and calculate using you formula:

10 * 50 / (20 - 10) *20 = 500 / 200 = ???
Will the result be 20 TPS?

void yacht
feral owl
void yacht
#

also your math is incorrect πŸ˜…

feral owl
void yacht
feral owl
#

Ok, thank you

fickle fulcrum
#
import { system } from "@minecraft/server";

/**
 * Tracks the last real-world time for TPS calculation.
 * @type {number}
 */
let lastTime = Date.now();

/**
 * Counts the number of ticks since the last TPS update.
 * @type {number}
 */
let tickCount = 0;

/**
 * Calculates and logs the TPS (Ticks Per Second) to the console.
 * This function is called every 20 ticks.
 */
function updateTPS() {
    let currentTime = Date.now();
    let currentTick = system.currentTick;
    
    // Calculate real-world time difference in seconds
    let timeDifferenceInSeconds = (currentTime - lastTime) / 1000;
    
    // Calculate TPS (ticks per second)
    let tps = tickCount / timeDifferenceInSeconds;
    
    // Reset the tick count and update the last tick time
    tickCount = 0;
    lastTime = currentTime;
    
    // Log the TPS value to the console
    console.log(`Current TPS: ${tps.toFixed(2)}`);
}

/**
 * Handles each tick by incrementing the tick count and updating TPS.
 * This function is registered to run every 1 tick.
 */
function tickHandler() {
    tickCount++; // Increment tick count for each tick
    
    // Call the TPS update every 20 ticks
    if (tickCount % 20 === 0) {
        updateTPS(); // Update TPS calculation and logging every 20 ticks
    }
}

// Register the tickHandler function to be called every 1 tick
system.runInterval(tickHandler, 1);
peak falcon
void yacht
#

TPS can change up?

peak falcon
#

You can test by using minecarts, or a big pile of sand (falling block)

peak falcon
fickle fulcrum
# peak falcon I'm not sure if that will work. Pretty sure `system.currentTick` is affected by ...

That's unfortunately misinformed information that I have seen many people here pass around. Minecraft's tick system remains consistent regardless of hardware or server performance, while Date.now() relies ideally on "real-world" time. Using Date.now() could introduce inaccuracies, especially on laggy servers where real-time and game-time drift apart. If the server is under heavy load, Date.now() may not reflect what’s happening in the game world accurately. Using system.currentTick ensures your measurements are made within the context of the game engine, meaning your TPS calculation will be more reflective of in-game performance rather than real-world time discrepancies.

shut rapids
#

yeah

peak falcon
#

Aren't TPS Counter supposed to measure Tick passed based on real-world time?

peak falcon
fickle fulcrum
peak falcon
fickle fulcrum
peak falcon
#

aight. Let me know when you can. I'm curious to know what's the difference lol

fickle fulcrum
#

For example, I could write a code (already have) that does a count down to clear entities in the world. This countdown from 5 seconds to 1 seconds could potentially duplicate using Date.now() when the server is under heavy load. Now, this could be managed to clean it up but that doesn't fix the problem. It covers it with a bandaid. Using currentTick can make the difference of 5, 4, 3, 2, 1 versus 5, 4, 4, 3, 2, 2, 1, 1.

peak falcon
#

I'll just test myself then. You see I'm not very smart

fickle fulcrum
#

Testing is always good. Especially when there is uncertainty.

#

Ill try to remember to touch base here when I have more free time and fix the code I shared above. I wrote it on Mobile which probably wasn't the best idea on my part.

peak falcon
#

Ok, we might have a misunderstanding this whole time.

My TPS counter here is supposed to measure the game tick by relying on the real-world time difference. Which is enough to signify the game's performance.

I didn't mean to use it to schedule things! You still need to use system.runInterval for the general API usage!

Now I know what you mean by "duplicate". You thought I was using Date.now() as a means of timing my code, which I don't! 🀣

fickle fulcrum
peak falcon
fickle fulcrum
#

@peak falcon reviewing the logic for TPS I stand corrected. πŸ‘

peak falcon
#

okey, Imma lay dead now. its 1 am here

fickle fulcrum
prisma cedar
#

@fickle fulcrum

let lastDate = Date.now()
let TPS = 20;
system.runInterval(() => {
    itemMechanics()
    const currDate = Date.now();
    TPS = tickspeed * 50 / (currDate - lastDate) * 20
    lastDate = currDate
    console.error(TPS)
}, tickspeed
)
#

i used this and it says 0

fickle fulcrum
prisma cedar
fickle fulcrum
#
let lastDate = Date.now()
let tickspeed = 20;
system.runInterval(() => {
    itemMechanics()
    let currDate = Date.now();
    let TPS = tickspeed * 50 / (currDate - lastDate) * 20
    lastDate = currDate
    console.error(TPS)
}, tickspeed
)
prisma cedar
#

thanks

twilit stone
#

so which one to use?

#

Gega's TPS counter or Visual1mpact's TPS counter?

twilit stone
peak falcon
#

If you want a simple and small one, mine looks good. Visual1mpact got the comments and parameters for typings tho