Jump to content

Impact of Using Events on Server Performance


Recommended Posts

Hello everyone!

I'm developing a server and recently started managing all server outputs through events. For example, instead of directly calling `outputChatBox`, I've defined an event `onRequestChat` and then call `outputChatBox` within that event. My question is whether this approach can significantly impact server performance, especially when there are many players. Is it possible that extensive use of events could cause the server to slow down or lag?

Thank you!

Link to comment
  • Moderators
1 hour ago, Mersad said:

Is it possible that extensive use of events could cause the server to slow down or lag?

It is possible, but it depends...

There are a few ways to prevent possible lag:

  • Not trigger events with the source set to root.
  • Adding rate limits on clientside, to prevent (non cheating) players to abuse spamming events.
  • Being aware that elementdata also triggers events.

And as TMTMTL said, some more context would be nice.

 

 

Link to comment
Posted (edited)
1 hour ago, TMTMTL said:

Hi.

Why would you need to do that exactly? 
A little more context is required.

I want to send texts with specific keywords to maintain a consistent color palette for outputs. For example:
"Hello&redWorld"
In our handler function, we replace the &red with #ff0000 to change the color of the rest of the chat to red. I have done this for many colors and want to use constant values to make the code more stable and dynamic.

If I were to use classic color placement (outputChatBox), it would be difficult, and editing the code would be harder. Is there a better way to achieve this? Additionally, if not, if I implement this with events, could it slow down or lag my server? What steps should I take to prevent this issue?

 

I can put the color change code in each individual source and be sure that my code will not cause lag or delay, but this is not a proper approach. Additionally, if I later need to edit the hex code of a color, I would have to make this change in each individual source, which is hard and cumbersome.


@TMTMTL & @IIYAMA

Edited by Mersad
Link to comment
4 minutes ago, Mersad said:

I want to send texts with specific keywords to maintain a consistent color palette for outputs. For example:
"Hello&redWorld"
In our handler function, we replace the &red with #ff0000 to change the color of the rest of the chat to red. I have done this for many colors and want to use constant values to make the code more stable and dynamic.

If I were to use classic color placement (outputChatBox), it would be difficult, and editing the code would be harder. Is there a better way to achieve this? Additionally, if not, if I implement this with events, could it slow down or lag my server? What steps should I take to prevent this issue?

@TMTMTL & @IIYAMA

If you handle the outputChatBox messages in 1 resource or script, you can simply override the outputChatBox function, redefining to do your color changes and magic.

-- serverside
outputChatBoxOriginal = outputChatBox
outputChatBox = function(text, element, r, g, b, colorCoded)
  -- here you can change text variable
  return outputChatBoxOriginal(text, element, r, g, b, colorCoded)
end

 

  • Like 1
Link to comment
5 minutes ago, FernandoMTA said:

If you handle the outputChatBox messages in 1 resource or script, you can simply override the outputChatBox function, redefining to do your color changes and magic.

-- serverside
outputChatBoxOriginal = outputChatBox
outputChatBox = function(text, element, r, g, b, colorCoded)
  -- here you can change text variable
  return outputChatBoxOriginal(text, element, r, g, b, colorCoded)
end

 

No, I want to use the system I wrote instead of the regular outputChatBox in each individual source. I have already written the color change system, but my problem is that I don't know how to use it in all sources without causing delays. The event system was the only solution I could think of, but I'm not sure if this system will cause lag or delay.
 

Link to comment
  • Moderators
7 minutes ago, Mersad said:

If I were to use classic color placement (outputChatBox), it would be difficult, and editing the code would be harder. Is there a better way to achieve this? Additionally, if not, if I implement this with events, could it slow down or lag my server? What steps should I take to prevent this issue?

I would also go for the suggestion @FernandoMTA  made, but with the syntax that works for you.

If possible move some of the events that are triggered on the same side (client/server) to export functions.

 

Serverside

function customOutputChatBox  () -- export function
	-- Add rate limit here for each player
	-- Show message
end

How to create/call export functions?

 

 

Clientside (if available)

If you use remote access.

function customOutputChatBox  () -- export function
	-- Add rate limit here, protecting the server from spam
	triggerServerEvent(...)
end

 

 

Link to comment
5 minutes ago, IIYAMA said:

I would also go for the suggestion @FernandoMTA  made, but with the syntax that works for you.

If possible move some of the events that are triggered on the same side (client/server) to export functions.

 

Serverside

function customOutputChatBox  () -- export function
	-- Add rate limit here for each player
	-- Show message
end

How to create/call export functions?

 

 

Clientside (if available)

If you use remote access.

function customOutputChatBox  () -- export function
	-- Add rate limit here, protecting the server from spam
	triggerServerEvent(...)
end

 

 

Well, I was considering using such an approach, but the wiki states the following about this function:
"Note: Calls may incur a performance overhead - they are not equivalent in performance to calling functions in the same resource."
I felt that this might cause delays on the server.

Does this delay have any significant impact?
 

Link to comment
  • Moderators

 

45 minutes ago, Mersad said:

"Note: Calls may incur a performance overhead - they are not equivalent in performance to calling functions in the same resource."
I felt that this might cause delays on the server.

The code is ran single threaded so it doesn't really matter, it is not as if the CPU is doing nothing during that delay. Trigger events will move the execution to the next (server) frame, it is just delayed.

 

Why that extra overhead? (for events and exports)

  • Another environment has to be accessed.
  • The data arguments/parameters have to be cloned. If tables are passed I can imagine that will be even slower.

 

Here are some performance browser details + examples. Results will probably be different on your system.

I expected the events to be much worse, but that doesn't really seems to be the case. Might be because exports do also return values back to the original caller. (for better or worse)

 

function exportMe(a, b, c)

end

local thisResource = getThisResource()
setTimer(function()
    for i = 1, 10000, 1 do
        call(thisResource, "exportMe", "a", "b", "c")
    end
end, 50, 0)

Export:
58% > 60% CPU

 

function exportMe(a, b, c)

end

addEvent("blabla", true)
addEventHandler("blabla", resourceRoot, exportMe)

setTimer(function()
    for i = 1, 10000, 1 do
        triggerEvent("blabla", resourceRoot, "a", "b", "c")
    end
end, 50, 0)

Event:
62% > 64% CPU

Context: This resourceRoot has no extra elements.

 

function exportMe(a, b, c)

end

addEvent("blabla", true)
addEventHandler("blabla", resourceRoot, exportMe)

-- 400 extra children
for i = 1, 400, 1 do
    createElement("test")
end

setTimer(function()
    for i = 1, 10000, 1 do
        triggerEvent("blabla", resourceRoot, "a", "b", "c")
    end
end, 50, 0)

Event:
67 > 68% CPU

Context: Source element with 400 extra children

 

 

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...