Solved Stopwatch class usage?

Discussion in 'Rust Development' started by bazuka5801, Jun 22, 2016.

  1. Why i cant use stopwatch class?

    there are analogs ?
     
  2. Wulf

    Wulf Community Admin

    Which class would that be? If you need a timer, you can use Oxide's Timer API, which is documented in the Docs and our GitHub repo.
     
  3. I need to get a time start function then Current time - time start function = get time call function
     
  4. Hey Wulf,

    long time, im back at modding :)
    I agree with the original poster here, the Timer API you mentioned seems fit for creating timed triggered events.

    What he, and I, am looking for is the "performance counters" to measure execution time.
    The one he mentioned "stopwatch" is used by Rust itself which is in Systems.Diagnostics.

    Can it be added to the list of allowed classes inside a plugin please?

    Example of it is below, but it gives the following sort of error at run time.
    Code:
    [Oxide] 20:10 [Error] Failed to call hook 'cmdExample' on plugin 'ExamplePlugin v1.0.0' (UnauthorizedAccessException: System access is restricted, you are not allowed to use System.Diagnostics.Stopwatch)
    

    Sample of using a Stopwatch class in C#

    Code:
    System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();while(  someCondition!=true )
    {
        // Start performance timer
        stopwatch.Start();    // Do some really heavy lifting here
       HeavyCodeHere();    // Print out how long that task took...
        msg = "Searching Took: " + ((stopwatch.Elapsed.TotalMilliseconds / 1000.0)).ToString() + " seconds" ;
        Puts( msg );
        SendChatMessage(searchingPlayer,  msg ) ;    // Reset and restart the performance counter
        stopwatch.Stop();
        stopwatch.Reset();
        stopwatch.Start();
    }
    
     
  5. @bazuka5801

    Here is a quick and dirty solution to this problem, allows for inaccurate timing to be recorded.
    Use it just like in the example above.

    Code:
    // Basic inaccurate implementation of System.Diagnostics.Stopwatch
    class PoormansStopwatch
    {
         // Here so it matches the System.Diagnostics structure
         internal class tElapsed
         {
            public long tickstart = 0 ;
         
            public long TotalMilliseconds
            {
                get { return (DateTime.Now.Ticks - tickstart)/TimeSpan.TicksPerMillisecond ; }
            }
         }
      
         public tElapsed Elapsed = new tElapsed();
      
         public void Start() { Elapsed.tickstart = DateTime.Now.Ticks ; }
         public void Stop()  { }
         public void Reset() { Elapsed.tickstart = DateTime.Now.Ticks ; }
    }
    
     
  6. Thanks, but i write now my Oxide.Core.XXX.dll, and i add static class to use StopWatch
    Code:
    public static class StopwatchUtils
        {
            static Dictionary<string, Stopwatch> watches = new Dictionary<string, Stopwatch>();        /// <summary>
            /// Start Stopwatch
            /// </summary>
            /// <param name="name">KEY</param>
            public static void StopwatchStart(string name)
            {
                watches[name] = Stopwatch.StartNew();
            }        /// <summary>
            /// Get Elapsed Milliseconds
            /// </summary>
            /// <param name="name">KEY</param>
            /// <returns></returns>
            public static long StopwatchElapsedMilliseconds(string name) => watches[name].ElapsedMilliseconds;        /// <summary>
            /// Remove StopWatch
            /// </summary>
            /// <param name="name"></param>
            public static void StopwatchStop(string name)
            {
                watches.Remove(name);
            }
        }
    In plugins, i add reference to dll
     
  7. Wulf

    Wulf Community Admin

    Please use Oxide.Ext.XXXX.dll for your extension namespace. The Oxide.Core namespace is intended only for official extensions and may not be guaranteed to be available for third-party extensions.

    As long as it wouldn't cause any issues or provide any undesirable access that could risk the sandbox security such as I/O access, you can PR a change for it.
     
  8. Yeah I thought you might have either given up or made a nice solution - for others following, making a new Oxide DLL isnt always possible / easy for your end users if you are just writing a simple mod. I hope @Wulf can add the above as I think its useful.
     
  9. Wulf

    Wulf Community Admin

    Nope, just lost track of the thread. I likely passed it by the others and didn't get a definite reply at the time. If you know where it is, you could PR what you're thinking of.
     
  10. What about using `Time` which is available (UnityEngine.dll)
    Code:
    while (someCondition != true)
    {
        // Start time
        var started_at = Time.realtimeSinceStartup;    // Do some really heavy lifting here
       HeavyCodeHere();    // Print out how long that task took...
        var msg = $"Searching Took: {Time.realtimeSinceStartup - started_at:0.000} seconds.";
        Puts(msg);
        SendChatMessage(searchingPlayer,  msg ) ;
    }
    
     
  11. Building off of what Mughisi wrote:

    Here is a quick class that should act like System.Diagnostics.Stopwatch using Unity Time
    Code:
    class UStopWatch
        {
            float startTime;
            int startFrame;
            float stopTime;
            int stopFrame;        bool _isRunning;
            public bool isRunning { get { return _isRunning; } private set { _isRunning = value; } }        public float elapsedSeconds { get { return (isRunning ? Time.realtimeSinceStartup : stopTime) - startTime; } }
            public int elapsedMilliseconds { get { return Mathf.RoundToInt(elapsedSeconds / 1000f); } }
            public int elapsedFrames { get { return (isRunning ? Time.frameCount : stopFrame) - startFrame; } }        public UStopWatch(bool startNow = true)
            {
                if(startNow)
                    Start();
            }        public void Start()
            {
                Reset();
                _isRunning = true;
            }
            public void Stop()
            {
                stopTime = Time.realtimeSinceStartup;
                stopFrame = Time.frameCount;            _isRunning = false;
            }
            public void Reset()
            {
                startTime = Time.realtimeSinceStartup;
                startFrame = Time.frameCount;
                stopTime = Time.realtimeSinceStartup;
                stopFrame = Time.frameCount;
            }
        }
     
  12. Wulf

    Wulf Community Admin

    The biggest concern that @Mughisi brought up about exposing the Stopwatch, is that plugins would need to make sure they stop them, otherwise it could get a little hairy.
     
  13. OMG... I FUCK Oxide.Ext, I cant use magic code "// Reference Oxide.Ext.XXX", i love Oxide.Core. , because its possible
     
  14. Wulf

    Wulf Community Admin

    Well, once we get that fixed so the magic reference isn't needed, Oxide.Core will be only for official extensions. Just warning you in advance.
     
  15. Ok, i have my magic core, and how i must rename my extension to i can use // Reference for my dll
    [DOUBLEPOST=1483658778][/DOUBLEPOST]
    OOOO...., i bypass this fix with simple extension if i need it)
     
  16. Wulf

    Wulf Community Admin

    We're working to resolve the Oxide.Ext namespace not being referenced by default, but the Oxide.Core namespace is only intended for us to use, not third-party extensions. I'm sorry that you don't understand that.

    @Lucien, our only concern is that plugins may causing some issues if they start messing around with threading. Do you see this being an issue with System.Threading.Interlocked being exposed? With the many alternatives, we're not sure it's worth exposing it.
     
  17. how use // Reference To my dll extension?
     
  18. Wulf

    Wulf Community Admin

    If you are trying to reference the extension from plugins, it should just be a matter of the magic reference to your DLL minus the .dll, but this is supposed to be handled by default in Oxide, which is why I mentioned we need to fix it. If you're trying to make one extension communicate with the other, that is handled within the projects before compiling.
     
  19. i need default plugin to reference for extension
     
  20. Yes there are drawbacks to just directly exposing, we could wrap like you did for timer.Once/etc then reload/etc could kill them. Or what about using either the example I or @EinTime (great name for this thread) gave. They arent quite StopWatch but they are good enough maybe for most plugin uses?

    The advanced people that want to use stopwatch can follow an Ext route to get what ever functionality they want?