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

2023 ж. 25 Қаң.
72 575 Рет қаралды

Enroll to "Cloud Fundamentals: AWS Services for C# Developers" for FREE: bit.ly/3XKUBOH
Become a Patreon and get source code access: / nickchapsas
Hello everybody I'm Nick and in this video I will show you by far the easiest day to start measuring the performance of your methods in C#. It is as simple as adding a single line of code and it can really help you start collecting some metrics without having to edit existing code.
Give MethodTimer a star on GitHub: github.com/Fody/MethodTimer
Don't forget to comment, like and subscribe :)
Social Media:
Follow me on GitHub: bit.ly/ChapsasGitHub
Follow me on Twitter: bit.ly/ChapsasTwitter
Connect on LinkedIn: bit.ly/ChapsasLinkedIn
Keep coding merch: keepcoding.shop
#csharp #dotnet

Пікірлер
  • In regard to telemetry, I'd love to see an OpenTelemetry video. Aside from logs, both the traces and metrics SDKs are stable.

    @rxuhwxx4tmly9xvcktcn2znpsx4@rxuhwxx4tmly9xvcktcn2znpsx4 Жыл бұрын
    • It is definitely coming

      @nickchapsas@nickchapsas Жыл бұрын
    • Cool password bro

      @silkfire@silkfire Жыл бұрын
    • ​@@nickchapsasYes pleeease! Would love an Open Telemetry video

      @slowjocrow6451@slowjocrow64516 ай бұрын
  • It is a great library. I was always waiting for something like this to measure time, easy, clean, and beautiful. Thanks Nick!!!!!

    @alfredoquintanalopez2265@alfredoquintanalopez2265 Жыл бұрын
  • Great video Nick. Ive been looking for a better way to measure methods for a long time. Thanks for sharing!

    @FrancisGauthier2@FrancisGauthier2 Жыл бұрын
  • Great video as always, thank you for the hard work.

    @muhamedkarajic@muhamedkarajic Жыл бұрын
  • Thank you, this helps so much when working on new projects!

    @techmentormaria@techmentormaria Жыл бұрын
  • Love these types of videos Nick!

    @GuildOfCalamity@GuildOfCalamity Жыл бұрын
  • 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?

    @domportera@domportera Жыл бұрын
  • 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

    @lordicemaniac@lordicemaniac Жыл бұрын
  • Cheers Nick. This is super useful. We have stop watches everywhere and its kinda clunky. Definitely going to investigate this.

    @MrGTFOplz@MrGTFOplz Жыл бұрын
  • There's also Serilog Timings library; writes measurements to log.

    @KvapuJanjalia@KvapuJanjalia Жыл бұрын
    • It does but it is Serilog specific. You can plug any logging provider or metrics collector in this one which I prefer

      @nickchapsas@nickchapsas Жыл бұрын
  • Thanks for the free course and awesome work

    @ehudpinhas8873@ehudpinhas8873 Жыл бұрын
  • I fucking love both you and this nugget provider for making and showcasing such a useful tool!

    @MrHandsy@MrHandsy Жыл бұрын
  • Wow thanks, that's exactly what i needed right now!

    @lucaweidmann8339@lucaweidmann833910 ай бұрын
  • Thanks! It is a really nice library (and underrated, only several hundreds stars) :)

    @learnsoftwarearchitecture@learnsoftwarearchitecture Жыл бұрын
  • Hey Nick, nice video. Aside from time performance, can I log the allocated memory by the method?

    @ayalamac@ayalamac7 ай бұрын
  • 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

    @dimitris470@dimitris470 Жыл бұрын
  • Hi Nick, Thank you for the great video , How does the Package work or How Can I make method attribute work as decorator ?

    @sherifmohamed5294@sherifmohamed5294 Жыл бұрын
  • this is cool!

    @rahulkjohny4377@rahulkjohny43777 ай бұрын
  • 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

    @arielunanue4354@arielunanue4354 Жыл бұрын
  • Fantastic! Thanks!

    @RichardONeil@RichardONeil Жыл бұрын
  • 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.

    @andrejcacala8629@andrejcacala86298 ай бұрын
  • 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?

    @davidserb1678@davidserb16788 ай бұрын
  • 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?

    @hotasfireph@hotasfireph8 ай бұрын
  • 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.

    @logank.70@logank.70 Жыл бұрын
  • good explaination

    @lubnamanzoor132@lubnamanzoor132 Жыл бұрын
  • Hmmm cool stuff. Does AOP libraries use assembly weaving too to handle cross-cutting concerns?

    @kasozivincent8685@kasozivincent86857 ай бұрын
  • 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.

    @danielprows6416@danielprows6416 Жыл бұрын
  • 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?

    @rmichaeldeutsch@rmichaeldeutsch Жыл бұрын
    • Yes! Basically all that code will be as if it was in the main method at compile time

      @nickchapsas@nickchapsas Жыл бұрын
  • Would it just be simpler to add the Prometheus library and now you have all that info on /metrics ?

    @metaltyphoon@metaltyphoon Жыл бұрын
  • 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…

    @brookster7772@brookster7772 Жыл бұрын
    • I do the same with NServicebus. Really effective way to monitor queue times.

      @yunsha9986@yunsha9986 Жыл бұрын
    • You said it first... :)

      @ergis8004@ergis8004 Жыл бұрын
  • 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.

    @garcipat@garcipat Жыл бұрын
  • 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.

    @JohnOliverAtHome@JohnOliverAtHome Жыл бұрын
  • 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?

    @nickwong9649@nickwong96499 ай бұрын
  • Does grabbing and dealing with MethodIndo has any performance issues when using this library?

    @antonmartyniuk@antonmartyniuk Жыл бұрын
  • Very good

    @HenrryPires@HenrryPires Жыл бұрын
  • 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?

    @fmonteiropt@fmonteiropt Жыл бұрын
    • 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.

      @reikooters@reikooters Жыл бұрын
  • Hi Nick !

    @mohamedchaabouni4477@mohamedchaabouni4477 Жыл бұрын
  • 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.

    @TheDuerden@TheDuerden Жыл бұрын
  • 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?

    @Petoj87@Petoj87 Жыл бұрын
    • 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.

      @LeMustache@LeMustache Жыл бұрын
    • @@LeMustache thanks!

      @Petoj87@Petoj87 Жыл бұрын
  • 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...

    @29Aios@29Aios Жыл бұрын
  • 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.

    @claude4074@claude4074 Жыл бұрын
  • How did you go to the code that is converted to ??

    @the_sweet_heaven@the_sweet_heaven Жыл бұрын
  • 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.

    @whatgoglikeno6120@whatgoglikeno6120 Жыл бұрын
  • does this work with a method that yields results into an IEnumerable output?

    @eunantog@eunantog Жыл бұрын
    • I have actually never tried that. I'd have to give it a go and see

      @nickchapsas@nickchapsas Жыл бұрын
  • Content starts at 2:15

    @DevonLehmanJ@DevonLehmanJ Жыл бұрын
  • Does this work with aot?

    @EliasElMango@EliasElMango Жыл бұрын
  • Is it compatible with AOT?

    @RichardNatal@RichardNatal4 күн бұрын
  • 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

    @carmineos@carmineos Жыл бұрын
    • Yeah absolutely. You can do that by accessing them in the MethodInfo object. They are just an array that you can loop

      @nickchapsas@nickchapsas Жыл бұрын
    • @@nickchapsas i thought it was possible for params metadata, but can you even get the runtime value of them?

      @carmineos@carmineos Жыл бұрын
  • 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?

    @alexmac513@alexmac513 Жыл бұрын
    • It’s just suppressing the null warning. It’s been there since C# 8 I’m pretty sure

      @nickchapsas@nickchapsas Жыл бұрын
    • ​@@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.

      @alexmac513@alexmac513 Жыл бұрын
  • can you export them to a dashboard in aws? because using app.metrics its very easy since in expose an api endpoint for metrics

    @R0ckyRocket@R0ckyRocket Жыл бұрын
    • Sure it is. You can plug in App Metrics, OpenTelemetry or whatever you want

      @nickchapsas@nickchapsas Жыл бұрын
  • oh yeahh

    @codingwithgyver1637@codingwithgyver1637 Жыл бұрын
  • Can't signup on the website as a new User, there is a problem with teachable

    @void_star_void@void_star_void Жыл бұрын
  • How much does Fody cost?

    @ivcbusinesssystems6613@ivcbusinesssystems6613 Жыл бұрын
  • 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....

    @omidsojoody@omidsojoody Жыл бұрын
    • agree, especially if it create exception trap

      @Lactorioga@Lactorioga5 ай бұрын
  • Please do an Azure course.

    @rubberuk@rubberuk Жыл бұрын
    • Only if Azure sponsors it

      @nickchapsas@nickchapsas Жыл бұрын
    • @@nickchapsas they'd be stupid not to tbh

      @domportera@domportera Жыл бұрын
    • @@nickchapsas you should ask them to sponsor it, I'll love to see an Azure course!

      @kfique@kfique Жыл бұрын
  • I really want to buy your course, however £80.00 is too pricey when you live in SA

    @gideonharmse4270@gideonharmse4270 Жыл бұрын
  • I think that datadog is doing the same things for you already out of the box

    @vladstatnyk8104@vladstatnyk8104 Жыл бұрын
  • Damn just bought 2 courses and can't use the discount now

    @thorkmunch@thorkmunch Жыл бұрын
  • 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...

    @Petoj87@Petoj87 Жыл бұрын
    • unreal engine is also open-source, but they still expect you to pay for it. it's just a business-model.

      @Krokoklemmee@Krokoklemmee Жыл бұрын
    • 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

      @Kollegah9997@Kollegah9997 Жыл бұрын
    • Open source != free.

      @okmarshall@okmarshall Жыл бұрын
    • Open source is not necessary free

      @PanzerFaustFurious@PanzerFaustFurious Жыл бұрын
  • Fody isn't free, it requires a mandatory donation matching the number of devs using the code base or something like that.

    @ps2goat297@ps2goat2974 ай бұрын
  • like if your playback settings are 0.5x to understand him

    @berkanbilgin2287@berkanbilgin2287Ай бұрын
  • First

    @TechySpeaking@TechySpeaking Жыл бұрын
  • Could have done with this two weeks ago. 🙄

    @TodPalin@TodPalin Жыл бұрын
  • I just got rid of Fody in my projects. Code generation has to be better than that.

    @nayanchoudhary4353@nayanchoudhary4353 Жыл бұрын
    • Fody is assembly weaving not code generation

      @nickchapsas@nickchapsas Жыл бұрын
    • @@nickchapsas You're right. But Fody interferes with build. It's super annoying. So i don't want it

      @nayanchoudhary4353@nayanchoudhary4353 Жыл бұрын
  • Third!

    @davidwhite2011@davidwhite2011 Жыл бұрын
  • The C# team should add python-like decorators

    @KasperDahlTangen@KasperDahlTangen Жыл бұрын
  • This is not the video i have searched for, Google

    @IldarIsm@IldarIsm7 күн бұрын
  • First comment

    @richardarielcruzcespedes9455@richardarielcruzcespedes9455 Жыл бұрын
  • This was 9 lines. disappointing.

    @Papiertig0r@Papiertig0r Жыл бұрын
  • Datadog now support OpenTelelerty. The way more powerful combination

    @deepakdagar_change_is_in@deepakdagar_change_is_in Жыл бұрын
KZhead