The Easiest Way to Measure Your Method’s Performance in C#

แชร์
ฝัง
  • เผยแพร่เมื่อ 3 ธ.ค. 2024

ความคิดเห็น • 110

  • @rxuhwxx4tmly9xvcktcn2znpsx4
    @rxuhwxx4tmly9xvcktcn2znpsx4 ปีที่แล้ว +80

    In regard to telemetry, I'd love to see an OpenTelemetry video. Aside from logs, both the traces and metrics SDKs are stable.

    • @nickchapsas
      @nickchapsas  ปีที่แล้ว +61

      It is definitely coming

    • @silkfire
      @silkfire ปีที่แล้ว +3

      Cool password bro

    • @slowjocrow6451
      @slowjocrow6451 ปีที่แล้ว

      ​@@nickchapsasYes pleeease! Would love an Open Telemetry video

    • @unkn0wnrge189
      @unkn0wnrge189 หลายเดือนก่อน

      forget nunit, xunit, msunit, hunit, tunit, and abcedfgh whatever, the new thing that everyone must use today its the rage unit which has a partnership with my channel also buy a cup and hoodie and your assertions will be asserted.

  • @lordicemaniac
    @lordicemaniac ปีที่แล้ว +8

    it is very nice solution, one thing that i would change is that you have to call that magic class MethodTimeLogger exactly same way, I would prefer some way of registering class that implements some interface with the package

  • @KvapuJanjalia
    @KvapuJanjalia ปีที่แล้ว +14

    There's also Serilog Timings library; writes measurements to log.

    • @nickchapsas
      @nickchapsas  ปีที่แล้ว +12

      It does but it is Serilog specific. You can plug any logging provider or metrics collector in this one which I prefer

  • @DevonLehmanJ
    @DevonLehmanJ ปีที่แล้ว +6

    Content starts at 2:15

  • @alfredoquintanalopez2265
    @alfredoquintanalopez2265 ปีที่แล้ว +13

    It is a great library. I was always waiting for something like this to measure time, easy, clean, and beautiful. Thanks Nick!!!!!

  • @ayalamac
    @ayalamac ปีที่แล้ว +2

    Hey Nick, nice video. Aside from time performance, can I log the allocated memory by the method?

  • @ehudpinhas8873
    @ehudpinhas8873 ปีที่แล้ว

    Thanks for the free course and awesome work

  • @domportera
    @domportera ปีที่แล้ว +4

    Amazing, thank you Nick and Simon, this is the easy mode benchmarking I needed to actually start profiling in depth. is this compiled out of release builds?

  • @MrGTFOplz
    @MrGTFOplz ปีที่แล้ว

    Cheers Nick. This is super useful. We have stop watches everywhere and its kinda clunky. Definitely going to investigate this.

  • @lucaweidmann8339
    @lucaweidmann8339 ปีที่แล้ว

    Wow thanks, that's exactly what i needed right now!

  • @GuildOfCalamity
    @GuildOfCalamity ปีที่แล้ว

    Love these types of videos Nick!

  • @muhamedkarajic
    @muhamedkarajic ปีที่แล้ว +1

    Great video as always, thank you for the hard work.

  • @techmentormaria
    @techmentormaria ปีที่แล้ว

    Thank you, this helps so much when working on new projects!

  • @FrancisGauthier2
    @FrancisGauthier2 ปีที่แล้ว

    Great video Nick. Ive been looking for a better way to measure methods for a long time. Thanks for sharing!

  • @sherifmohamed5294
    @sherifmohamed5294 ปีที่แล้ว +1

    Hi Nick, Thank you for the great video ,
    How does the Package work or How Can I make method attribute work as decorator ?

  • @brookster7772
    @brookster7772 ปีที่แล้ว +5

    Another strategy would be to create middleware that is added to the very beginning of the pipeline on the way in… record the time, perhaps even store it in the request object…. Call Next()… then record the time to get your elapsed time for the entire request. In addition, one of the strategies we did is fire off a Kafka message (event hub / service bus works) over time u have some excellent telemetry that can be monitored.! Then efficiently send an entire batch of request telemetry to a Kafka topic every second
    And finally, instead of making a network call for every request, we would buffer up a batch of messages…

    • @yunsha9986
      @yunsha9986 ปีที่แล้ว

      I do the same with NServicebus. Really effective way to monitor queue times.

    • @ergis8004
      @ergis8004 ปีที่แล้ว

      You said it first... :)

  • @metaltyphoon
    @metaltyphoon ปีที่แล้ว +2

    Would it just be simpler to add the Prometheus library and now you have all that info on /metrics ?

  • @dimitris470
    @dimitris470 ปีที่แล้ว

    Very interesting. Symfony uses such data to show in their debug toolbar/panel/graph how much time each part of the framework takes. You can even group stuff together like all repositories and see how much time your db interaction takes, or your template compilation or whatever

  • @kasozivincent8685
    @kasozivincent8685 ปีที่แล้ว

    Hmmm cool stuff. Does AOP libraries use assembly weaving too to handle cross-cutting concerns?

  • @TheDuerden
    @TheDuerden ปีที่แล้ว +1

    Nice, but currently, you can't update Git from Visual Studio while this is installed - had to do a restart and remove it to commit. I am sure it will be fixed in time and will be most useful to use.

  • @learnsoftwarearchitecture
    @learnsoftwarearchitecture ปีที่แล้ว

    Thanks! It is a really nice library (and underrated, only several hundreds stars) :)

  • @MrHandsy
    @MrHandsy ปีที่แล้ว

    I fucking love both you and this nugget provider for making and showcasing such a useful tool!

  • @Petoj87
    @Petoj87 ปีที่แล้ว +2

    I have been watching your content a long time, i have never used Raider it seems nice but what do the pencils/fence in the lower right corner do?

    • @LeMustache
      @LeMustache ปีที่แล้ว +2

      They let you choose which issues rider highlights (only up to errors, only up to warnings, etc.) and control other visual hints like enabling/disabling inline information.

    • @Petoj87
      @Petoj87 ปีที่แล้ว

      @@LeMustache thanks!

  • @danielprows6416
    @danielprows6416 ปีที่แล้ว

    I don't know how this could be mixed with OpenTelemetry spans, because the way your log method just gets the elapsed time means that even if you make your logger build the spans manually, and you set the span's start/end times, you would still lose the parent child relationship with your spans.
    You could definitely implement metrics, but the spans seem dubious.
    if the package let you run your own method to start the stopwatch in addition to the logging, then you could do spans.
    The other weavers i've looked at like MethodBoundaryAspect and MethodDecorator have issues weaving correctly into the IL of the async/await state machine.
    MethodBoundaryAspect you can do a task.continueWith, but that still screws up the parent/child relationship.

  • @rmichaeldeutsch
    @rmichaeldeutsch ปีที่แล้ว +6

    Hey @Nick Chapsas, cool utility! Love how you're always sharing these neat little libraries I never would have heard of otherwise!
    One important question that comes to mind with the way this is implemented: what happens to the stack trace from methods that have this attribute? Will they still have the original method's name and class?

    • @nickchapsas
      @nickchapsas  ปีที่แล้ว +3

      Yes! Basically all that code will be as if it was in the main method at compile time

  • @andrejcacala8629
    @andrejcacala8629 ปีที่แล้ว

    Hi @nickchapsas, are you still planning to make video how to join this nugget with some telemetry? Also I would like to know if is possible to get hierarchical view of call when method call another methods.

  • @rubberuk
    @rubberuk ปีที่แล้ว +3

    Please do an Azure course.

    • @nickchapsas
      @nickchapsas  ปีที่แล้ว +8

      Only if Azure sponsors it

    • @domportera
      @domportera ปีที่แล้ว

      @@nickchapsas they'd be stupid not to tbh

    • @kfique
      @kfique ปีที่แล้ว

      @@nickchapsas you should ask them to sponsor it, I'll love to see an Azure course!

  • @nickwong9649
    @nickwong9649 ปีที่แล้ว

    I don't quite understand the last part - how do you use the non-allocating way of time measurement with Fody still, since the use of StopWatch is completely controlled by Fody itself?

  • @garcipat
    @garcipat ปีที่แล้ว

    I wonder how would you write those values in a time series database if your applicacion has not too many users at the same time of course. To observe the behaviour over time of the application. To observe endpoints.

  • @davidserb1678
    @davidserb1678 ปีที่แล้ว

    Very cool post!
    I have a question, please. You showed how to send a variable in the message, but is it possible to send an object property? For example this is my signature method:
    ```public async Task GetAllAppointments([FromQuery] GetAllAppointmentsQuery paginationQuery)```
    I want to send a property from the object like this: ```[Time("Retrieved {paginationQuery.PageSize} appointments.")]```
    This gives an error, and from what I read in the official docs of the library, it doesn't support referencing properties. Is there a workaround?

  • @eunantog
    @eunantog ปีที่แล้ว

    does this work with a method that yields results into an IEnumerable output?

    • @nickchapsas
      @nickchapsas  ปีที่แล้ว

      I have actually never tried that. I'd have to give it a go and see

  • @the_sweet_heaven
    @the_sweet_heaven ปีที่แล้ว

    How did you go to the code that is converted to ??

  • @lubnamanzoor132
    @lubnamanzoor132 ปีที่แล้ว

    good explaination

  • @omidsojoody
    @omidsojoody ปีที่แล้ว +1

    I'm not suggest any IL Weavering type libraries at all, because it it has some unwnated side effects like mismatched stacktrace compare to actual production code and so on....

    • @Lactorioga
      @Lactorioga ปีที่แล้ว

      agree, especially if it create exception trap

  • @logank.70
    @logank.70 ปีที่แล้ว

    How does this compare to environments using interceptors? It felt pretty similar just without marking up the code with details about cross-cutting concerns. Just setting it up when you are defining your dependency map and that's it. I've been happy with it so far.

  • @RichardNatal
    @RichardNatal 7 หลายเดือนก่อน

    Is it compatible with AOT?

  • @antonmartyniuk
    @antonmartyniuk ปีที่แล้ว

    Does grabbing and dealing with MethodIndo has any performance issues when using this library?

  • @arielunanue4354
    @arielunanue4354 ปีที่แล้ว

    Great video. Yesterday I was searching for middleware to measure the performance of the endpoints but now I see it can be done quite easy with this package too

  • @RichardONeil
    @RichardONeil ปีที่แล้ว

    Fantastic! Thanks!

  • @fmonteiropt
    @fmonteiropt ปีที่แล้ว

    I've made my own class to get this, but this is another level ... thanks a lot.
    Another that I would like to have, is a Translation method that can also works with variables in between the text, any ideas?

    • @reikooters
      @reikooters ปีที่แล้ว +2

      What you need is an i18n library (internationalization). How they work is you create what is basically a dictionary of keys (identifiers for phrases, e.g. "login.error.invalidPassword"), and then you specify a value which is specific to each language, optionally passing parameters to fill in the places where variables appear as you mentioned. I haven't used one for C# before, personally, I do this on the front end instead where all the language text is stored in a json file. From a quick search on github for a C# library though, if it was me I would maybe try TypealizR - from the readme, it looks like it works in the way I would expect, and was last updated yesterday, so means it's still maintained. But again I have not used it myself as I don't do this on the backend.

  • @hotasfireph
    @hotasfireph ปีที่แล้ว

    Is there a posibility to have an instance of the MethodTimeLog and not a static one? I wanted to log all method in one log in every request. But since since the Log function is static, I can't do it. Is there any better way than this?

  • @EliasElMango
    @EliasElMango ปีที่แล้ว

    Does this work with aot?

  • @29Aios
    @29Aios ปีที่แล้ว

    Hmm, but how it worked ?
    How the code was changed based on an Attribute before the compilation ?
    I've checked IL, and there is a Stopwatch already injected. I think this is a big topic to explain, but advanced...

  • @R0ckyRocket
    @R0ckyRocket ปีที่แล้ว

    can you export them to a dashboard in aws? because using app.metrics its very easy since in expose an api endpoint for metrics

    • @nickchapsas
      @nickchapsas  ปีที่แล้ว

      Sure it is. You can plug in App Metrics, OpenTelemetry or whatever you want

  • @carmineos
    @carmineos ปีที่แล้ว

    is it possible to automatically log all the input parameters without having to specify their names in the TimeAttribute ctor? Or even take complex object and serialize them to json

    • @nickchapsas
      @nickchapsas  ปีที่แล้ว

      Yeah absolutely. You can do that by accessing them in the MethodInfo object. They are just an array that you can loop

    • @carmineos
      @carmineos ปีที่แล้ว

      @@nickchapsas i thought it was possible for params metadata, but can you even get the runtime value of them?

  • @alexmac513
    @alexmac513 ปีที่แล้ว

    What looks like an !. operator made me double take. Is that null forgiving, but thats for method calls? Or are you using a pre release version of the compiler?

    • @nickchapsas
      @nickchapsas  ปีที่แล้ว +1

      It’s just suppressing the null warning. It’s been there since C# 8 I’m pretty sure

    • @alexmac513
      @alexmac513 ปีที่แล้ว

      ​@@nickchapsas Ah yes, they added all the warnings on reference intent. While I am on C# 10 I disabled that functionality as the code originates from dotnet rc1 and I don't have the time to fix so many warnings. Probably make it more reliable when I do though.

  • @claude4074
    @claude4074 ปีที่แล้ว +4

    Why can't this be done with a Source Code Generator? Having to include Fody for IL weaving seems like both a heavy handed and an old fashioned approach.
    It is expected that all developers using Fody become a Patron on OpenCollective.

  • @HenrryPires
    @HenrryPires ปีที่แล้ว

    Very good

  • @whatgoglikeno6120
    @whatgoglikeno6120 ปีที่แล้ว +1

    Nick how do you find these small libraries? Maybe make a video on that topic? If you put in a solid amount of time, do you think it's worth for "non creator developers" to do the same or should we just wait for the video/article?
    I am pretty involved and up to date. I follow people on youtube, I check conference talks, I am checking newsletters/aggregators, I randomly look for articles and videos like "10 useful nuget packages".
    But I feel I could still do with way more "hey this exists, this is probably useful some time in the future" type of knowledge.

  • @rahulkjohny4377
    @rahulkjohny4377 ปีที่แล้ว

    this is cool!

  • @ivcbusinesssystems6613
    @ivcbusinesssystems6613 ปีที่แล้ว

    How much does Fody cost?

  • @mohamedchaabouni4477
    @mohamedchaabouni4477 ปีที่แล้ว

    Hi Nick !

  • @aqkhana2002
    @aqkhana2002 3 หลายเดือนก่อน

    i have notice in the end of video you said better to use telemetry, then why not let us know how to if that is the best practice

  • @JohnOliverAtHome
    @JohnOliverAtHome ปีที่แล้ว

    I love Aspects. I've used PostSharp for many years, which I believe, uses Fody under the covers - although PostSharp is not free (commercially anyway). This is a great way of removing those cross cutting concerns from your logic code. One aspect I've used is for tracing stored procedure parameters with a single method attribute.

  • @void_star_void
    @void_star_void ปีที่แล้ว

    Can't signup on the website as a new User, there is a problem with teachable

  • @gideonharmse4270
    @gideonharmse4270 ปีที่แล้ว

    I really want to buy your course, however £80.00 is too pricey when you live in SA

  • @vladstatnyk8104
    @vladstatnyk8104 ปีที่แล้ว

    I think that datadog is doing the same things for you already out of the box

  • @BhavyaShah26
    @BhavyaShah26 3 หลายเดือนก่อน

    The link in the description is no longer valid!

  • @casperhansen826
    @casperhansen826 2 หลายเดือนก่อน

    I never measure performance unless there the code is slow, and not even then measure the code, I just look at the code and change it for better performance

  • @Petoj87
    @Petoj87 ปีที่แล้ว +4

    While Fody is a cool project they expect you to pay money for it, seems a bit counter intuitive for a open source project... so i don't know if i would use the project unless i must...

    • @Krokoklemmee
      @Krokoklemmee ปีที่แล้ว

      unreal engine is also open-source, but they still expect you to pay for it. it's just a business-model.

    • @Kollegah9997
      @Kollegah9997 ปีที่แล้ว

      Found the "use open source but never ever support the developers behind it" guy

    • @Kollegah9997
      @Kollegah9997 ปีที่แล้ว

      Found the "use open source but never ever support the developers behind it" guy

    • @okmarshall
      @okmarshall ปีที่แล้ว

      Open source != free.

    • @PanzerFaustFurious
      @PanzerFaustFurious ปีที่แล้ว

      Open source is not necessary free

  • @thorkmunch
    @thorkmunch ปีที่แล้ว

    Damn just bought 2 courses and can't use the discount now

  • @codingwithgyver1637
    @codingwithgyver1637 ปีที่แล้ว

    oh yeahh

  • @berkanbilgin2287
    @berkanbilgin2287 8 หลายเดือนก่อน

    like if your playback settings are 0.5x to understand him

  • @ps2goat297
    @ps2goat297 11 หลายเดือนก่อน

    Fody isn't free, it requires a mandatory donation matching the number of devs using the code base or something like that.

  • @TodPalin
    @TodPalin ปีที่แล้ว

    Could have done with this two weeks ago. 🙄

  • @nayanchoudhary4353
    @nayanchoudhary4353 ปีที่แล้ว

    I just got rid of Fody in my projects.
    Code generation has to be better than that.

    • @nickchapsas
      @nickchapsas  ปีที่แล้ว +1

      Fody is assembly weaving not code generation

    • @nayanchoudhary4353
      @nayanchoudhary4353 ปีที่แล้ว +1

      @@nickchapsas You're right. But Fody interferes with build. It's super annoying. So i don't want it

  • @TechySpeaking
    @TechySpeaking ปีที่แล้ว +1

    First

  • @KasperDahlTangen
    @KasperDahlTangen ปีที่แล้ว

    The C# team should add python-like decorators

  • @davidwhite2011
    @davidwhite2011 ปีที่แล้ว +1

    Third!

  • @IldarIsm
    @IldarIsm 7 หลายเดือนก่อน

    This is not the video i have searched for, Google

  • @Papiertig0r
    @Papiertig0r ปีที่แล้ว

    This was 9 lines. disappointing.

  • @richardarielcruzcespedes9455
    @richardarielcruzcespedes9455 ปีที่แล้ว

    First comment

  • @rmcgraw7943
    @rmcgraw7943 3 หลายเดือนก่อน

    public class StopWatch : System.Diagnostics.Stopwatch
    {
    public StopWatch() : base(){}
    public TimeSpan CalculateDuration(Action a)
    {
    base.Reset();base.Start();a();base.Start();return base.Elapsed;
    }
    public (TimeSpan Duration, TResult Result) CalculateDuration(Func function)
    {
    TResult result=default;
    base.Reset();base.Start();result=function();base.Start();return (Duration: base.Elapsed, Result:result);
    }
    public (TimeSpan Duration, TResult Result) CalculateDuration(Func asyncFunction)
    {
    TResult result=default;
    base.Reset();base.Start();base.Start();Call().RunSynchronously();return (Duration: base.Elapsed, Result:result);
    async Task Call()
    {
    result=await asyncFunction();
    }
    }
    }

  • @deepakdagar_change_is_in
    @deepakdagar_change_is_in ปีที่แล้ว

    Datadog now support OpenTelelerty. The way more powerful combination