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

  Рет қаралды 73,276

Nick Chapsas

Nick Chapsas

Жыл бұрын

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

Пікірлер: 104
@rxuhwxx4tmly9xvcktcn2znpsx4
@rxuhwxx4tmly9xvcktcn2znpsx4 Жыл бұрын
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 Жыл бұрын
It is definitely coming
@silkfire
@silkfire Жыл бұрын
Cool password bro
@slowjocrow6451
@slowjocrow6451 7 ай бұрын
​@@nickchapsasYes pleeease! Would love an Open Telemetry video
@lordicemaniac
@lordicemaniac Жыл бұрын
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
@alfredoquintanalopez2265
@alfredoquintanalopez2265 Жыл бұрын
It is a great library. I was always waiting for something like this to measure time, easy, clean, and beautiful. Thanks Nick!!!!!
@FrancisGauthier2
@FrancisGauthier2 Жыл бұрын
Great video Nick. Ive been looking for a better way to measure methods for a long time. Thanks for sharing!
@KvapuJanjalia
@KvapuJanjalia Жыл бұрын
There's also Serilog Timings library; writes measurements to log.
@nickchapsas
@nickchapsas Жыл бұрын
It does but it is Serilog specific. You can plug any logging provider or metrics collector in this one which I prefer
@muhamedkarajic
@muhamedkarajic Жыл бұрын
Great video as always, thank you for the hard work.
@techmentormaria
@techmentormaria Жыл бұрын
Thank you, this helps so much when working on new projects!
@MrGTFOplz
@MrGTFOplz Жыл бұрын
Cheers Nick. This is super useful. We have stop watches everywhere and its kinda clunky. Definitely going to investigate this.
@GuildOfCalamity
@GuildOfCalamity Жыл бұрын
Love these types of videos Nick!
@ehudpinhas8873
@ehudpinhas8873 Жыл бұрын
Thanks for the free course and awesome work
@domportera
@domportera Жыл бұрын
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?
@lucaweidmann8339
@lucaweidmann8339 10 ай бұрын
Wow thanks, that's exactly what i needed right now!
@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
@learnsoftwarearchitecture
@learnsoftwarearchitecture Жыл бұрын
Thanks! It is a really nice library (and underrated, only several hundreds stars) :)
@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
@ayalamac
@ayalamac 7 ай бұрын
Hey Nick, nice video. Aside from time performance, can I log the allocated memory by the method?
@MrHandsy
@MrHandsy Жыл бұрын
I fucking love both you and this nugget provider for making and showcasing such a useful tool!
@RichardONeil
@RichardONeil Жыл бұрын
Fantastic! Thanks!
@sherifmohamed5294
@sherifmohamed5294 Жыл бұрын
Hi Nick, Thank you for the great video , How does the Package work or How Can I make method attribute work as decorator ?
@lubnamanzoor132
@lubnamanzoor132 Жыл бұрын
good explaination
@kasozivincent8685
@kasozivincent8685 7 ай бұрын
Hmmm cool stuff. Does AOP libraries use assembly weaving too to handle cross-cutting concerns?
@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.
@HenrryPires
@HenrryPires Жыл бұрын
Very good
@davidserb1678
@davidserb1678 8 ай бұрын
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?
@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.
@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.
@rmichaeldeutsch
@rmichaeldeutsch Жыл бұрын
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 Жыл бұрын
Yes! Basically all that code will be as if it was in the main method at compile time
@rahulkjohny4377
@rahulkjohny4377 8 ай бұрын
this is cool!
@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.
@hotasfireph
@hotasfireph 8 ай бұрын
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?
@nickwong9649
@nickwong9649 9 ай бұрын
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?
@mohamedchaabouni4477
@mohamedchaabouni4477 Жыл бұрын
Hi Nick !
@antonmartyniuk
@antonmartyniuk Жыл бұрын
Does grabbing and dealing with MethodIndo has any performance issues when using this library?
@brookster7772
@brookster7772 Жыл бұрын
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... :)
@andrejcacala8629
@andrejcacala8629 8 ай бұрын
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.
@metaltyphoon
@metaltyphoon Жыл бұрын
Would it just be simpler to add the Prometheus library and now you have all that info on /metrics ?
@Petoj87
@Petoj87 Жыл бұрын
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 Жыл бұрын
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!
@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 Жыл бұрын
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.
@TheDuerden
@TheDuerden Жыл бұрын
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.
@DevonLehmanJ
@DevonLehmanJ Жыл бұрын
Content starts at 2:15
@the_sweet_heaven
@the_sweet_heaven Жыл бұрын
How did you go to the code that is converted to ??
@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
@RichardNatal
@RichardNatal 16 күн бұрын
Is it compatible 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...
@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 Жыл бұрын
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.
@EliasElMango
@EliasElMango Жыл бұрын
Does this work with aot?
@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?
@codingwithgyver1637
@codingwithgyver1637 Жыл бұрын
oh yeahh
@claude4074
@claude4074 Жыл бұрын
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.
@whatgoglikeno6120
@whatgoglikeno6120 Жыл бұрын
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.
@rubberuk
@rubberuk Жыл бұрын
Please do an Azure course.
@nickchapsas
@nickchapsas Жыл бұрын
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!
@void_star_void
@void_star_void Жыл бұрын
Can't signup on the website as a new User, there is a problem with teachable
@omidsojoody
@omidsojoody Жыл бұрын
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 5 ай бұрын
agree, especially if it create exception trap
@ivcbusinesssystems6613
@ivcbusinesssystems6613 Жыл бұрын
How much does Fody cost?
@vladstatnyk8104
@vladstatnyk8104 Жыл бұрын
I think that datadog is doing the same things for you already out of the box
@gideonharmse4270
@gideonharmse4270 Жыл бұрын
I really want to buy your course, however £80.00 is too pricey when you live in SA
@thorkmunch
@thorkmunch Жыл бұрын
Damn just bought 2 courses and can't use the discount now
@berkanbilgin2287
@berkanbilgin2287 2 ай бұрын
like if your playback settings are 0.5x to understand him
@ps2goat297
@ps2goat297 4 ай бұрын
Fody isn't free, it requires a mandatory donation matching the number of devs using the code base or something like that.
@Petoj87
@Petoj87 Жыл бұрын
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
@TodPalin
@TodPalin Жыл бұрын
Could have done with this two weeks ago. 🙄
@TechySpeaking
@TechySpeaking Жыл бұрын
First
@davidwhite2011
@davidwhite2011 Жыл бұрын
Third!
@nayanchoudhary4353
@nayanchoudhary4353 Жыл бұрын
I just got rid of Fody in my projects. Code generation has to be better than that.
@nickchapsas
@nickchapsas Жыл бұрын
Fody is assembly weaving not code generation
@nayanchoudhary4353
@nayanchoudhary4353 Жыл бұрын
@@nickchapsas You're right. But Fody interferes with build. It's super annoying. So i don't want it
@KasperDahlTangen
@KasperDahlTangen Жыл бұрын
The C# team should add python-like decorators
@IldarIsm
@IldarIsm 18 күн бұрын
This is not the video i have searched for, Google
@richardarielcruzcespedes9455
@richardarielcruzcespedes9455 Жыл бұрын
First comment
@Papiertig0r
@Papiertig0r Жыл бұрын
This was 9 lines. disappointing.
@deepakdagar_change_is_in
@deepakdagar_change_is_in Жыл бұрын
Datadog now support OpenTelelerty. The way more powerful combination
Settling the Biggest Await Async Debate in .NET
14:47
Nick Chapsas
Рет қаралды 138 М.
Double the Performance of your Dictionary in C#
15:12
Nick Chapsas
Рет қаралды 64 М.
ISSEI funny story😂😂😂Strange World | Pink with inoCat
00:36
ISSEI / いっせい
Рет қаралды 31 МЛН
FOOTBALL WITH PLAY BUTTONS ▶️ #roadto100m
00:29
Celine Dept
Рет қаралды 38 МЛН
Каха с волосами
01:00
К-Media
Рет қаралды 6 МЛН
NO NO NO YES! (50 MLN SUBSCRIBERS CHALLENGE!) #shorts
00:26
PANDA BOI
Рет қаралды 93 МЛН
"Stop Wasting Memory on Strings in C#!" | Code Cop #016
9:20
Nick Chapsas
Рет қаралды 66 М.
The Fastest Way to Modify a List in C# | Coding Demo
10:30
Zoran Horvat
Рет қаралды 18 М.
You are doing .NET logging wrong. Let's fix it
25:29
Nick Chapsas
Рет қаралды 168 М.
Why Startups Hate .NET and C#
10:38
Nick Chapsas
Рет қаралды 239 М.
How IEnumerable can kill your performance in C#
11:02
Nick Chapsas
Рет қаралды 111 М.
What is Span in C# and why you should be using it
15:15
Nick Chapsas
Рет қаралды 245 М.
Swagger is Going Away in .NET 9!
10:48
Nick Chapsas
Рет қаралды 65 М.
The fastest way to iterate a List in C# is NOT what you think
13:42
Nick Chapsas
Рет қаралды 154 М.
ISSEI funny story😂😂😂Strange World | Pink with inoCat
00:36
ISSEI / いっせい
Рет қаралды 31 МЛН