Describe the bug

10300 introduce TIME freeze and doesn't gave alternative

To reproduce

In script, call "TIME", do lot of things, recall "TIME" == no TIME elapsed.

I use the previous behavior to store the result of queries that take a long time to execute.

Comment From: madolson

We didn't consider this when making the change in #10300, since it was causing other unintended side effects.

It seems reasonable to introduce some type of function which is basically, "Current script execution time" that is available in lua.

Comment From: oranagra

IIRC we argued that long execution scripts are an anti-pattern and that script that uses time to exit a loop would be wrong too.

In your use case, do you measure it for debug / introspection? Or as a cache to avoid repeating long calculations?

Comment From: jgoudinou

@oranagra Both.

I migrated a piece of business code from backend to stored functions in order to save latency between backend and redis. One execution usually run in less than 5 ms, but some of them may take 1 or 2 seconds (depending on the amount of data). I put in cache the result of functions that use more data and are slower, proportional to their slowness.

For my defense, i don't exit loop using "time". The backend let the function running and if it has a waiting call since 20ms, it call function kill. There is no problem with the writes cause the cache is writen at the end of traitment.

Comment From: madolson

For my defense, i don't exit loop using "time".

Yeah, this is the anti-pattern that I think oran was referring to. I agree that I don't think time should be an exit criteria, but using it as extra information seems fine.

It sounds like you could re-implement most of the logic by basing the caching off of the size of the data instead of the time taken.

Comment From: oranagra

I think we should expose Lua's os.clock()

Comment From: jgoudinou

@oranagra : If I trust the documentation : "The os.clock function returns the number of seconds of CPU time for the program", Os.clock gives a number of seconds and it's not precise enought.

@madolson : Good idea but it's not the best way for me. On a existing dataset, i need to produces a relationship graph (nodes, edges, ... ) with lot of input parameters. I do that with recursive functions. The course may return more or fewer nodes depending of data explored, input parameters, .... Sometimes data are fetched then filtered or not require to explore more ... The size of the resultset is just a hint but the elapsed time to produce it is better.

Comment From: oranagra

os.clock() returns a fractional number. it is precise enough. the thing is that if the system is overloaded, unlike wall-clock or monotonic clock, it doesn't count time of other processes. but anyway, i don't mind exposing several types of clocks, i'm just arguing it can be via Lua, (not a redis command) @MeirShpilraien FYI.