The Clinic.js Workshop

Workshop from:
JS Nation
JSNation 2022

Learn the ways of the clinic suite of tools, which help you detect performance issues in your Node.js applications. This workshop walks you through a number of examples, and the knowledge required to do benchmarking and debug I/O and Event Loop issues.


but I Gonzaga and member of a few organizations in the open source and node.js Corps member and specify for member, and I also click jscore member. And from nearform as well, and I am based at in Brazil. So A fun fact about me is that I love C++. I have a paint of C++ here and I also have a tattoo a c++.2 here in my fist and it has a bug obviously. Okay. So let's move on. A few requirements to these. Okay. Let me just move it here. one second presenter Okay, it might be because of e3, don't worry. Can't do it right now. Okay. If you requirements to these Workshop is at least node 16. Clean Jazz a few. Not all the tools from from The Suite of clean jazz is supporting a note 18 because it changed at the V8 and the V8 change at the sync operations and just was not the belated to support it officially, but it might work. You need to have Docker and also doesn't matter your operation operational system. It should work. Firstly download these exercise. I will send in the chat right here. just set clone these people locally and we move on in the next it's like Okay. so let me introduce Clinic then you can go ahead and you these Workshop will work. Basically I would get send you a kind of exercise. You will try to do it alone. And then we will do it together and I will explain a bit of the clinic the suite that we were using. Okay, so basically Clinic yes is a suite of tools it contains. It will help you diagnose your application. Basically, you'll be able to to check the CPU. The memory you will be able to find button acts memory Lakes CPU dropping and also IO operation that is causing this major issues in your application. Okay, so it consists in four. For tools but in this Workshop, we will see just three. The tools are doctor. Clink doctor clinic flame clean bubbleprof and Clinic heat profiler. The heat provider is not covered in these Workshop because it's relatively new and it needs a new workshop just for that because it's a long complex, okay. so let's move on first thing that I need to do install node Clinic. Okay, you can just run any PM install. G clinic and you'll be able to run Clinic dash dash help. I will wait for everyone to install it. Okay. Okay. So, you know you now have Clinic install very good. Now the workshop with this Workshop will go through a guide through the click flow. How normally we do it in performance analysis by the way and performance in here at near form. So I used to do I use it to do it in client applications for instance. A lot of client application that I can't say the name unfortunately. Okay. so usually finding performance tissues can be a complex task. Usually it involves lucky. Yeah and sure. Profiling benchmarking and nowing knowing exactly the right tools. It means that it's very hard to identify a boronak by just using console.log, okay. Yeah, the vulnerabilities is totally safe. That doesn't. Don't worry about it. Okay. So solve a performance issue. Usually we have a long path. You find performance issues. Then you need to to identify it the second step like oh, okay. This is a about anac in my prediction application. So but this is a production application. You can't replicate it locally like the next step is to replicate in your product like environment. Not the live one. Once you have it, you need to diagnose to find the boronic and then replicate it in your local environment. Then you have you can use the right to to find the bottleneck. And usually it takes time. You need to to start at the screen for a long time go out for a dog and maybe in the middle of the the journey you think oh, these might be the issue and then you try to solve it. There are several ways to to solve a bar on like that. There are several approach to solve our neck. You have the scientific method that they may basically you have hypothesis and you test a potency and now so you have Ways to to map your entire application it will depend of the context that you are working on. Okay? So but in the end in the end of the day, I hope that you have a performance application. Okay, actually in the end of the month the week. I don't know it takes time. The first thing that you need to do is establish a baseline when when you are for instance, you are working on API. You need to establish a baseline before optimizing before optimizing anything you need to have. Data, you need to have dashboards. You need to have at least something to compare. for instance How can you measure how much improvement you got without a baseline you can't okay? So here in this Workshop, we will use a autocannon. These are two writing node.js. It's pretty good. It's pretty similar to a patch Benchmark or any kind of benchmark. basically, it will Throw a lot of requests to your HTTP service. And then you get some stats like how much requests per second or in 20 seconds doesn't matter and the latest the P99 the p15 and that's it. Okay. So our first server. The first thing that you need to do is once you have clone the repository you can go to the folder Hilo word. Hello world, okay. In this folder. You see that there's a HTTP base kttp server there and how can you measure how fast it can go? Okay, you can firstly run the application and open localhost 3000 your brows and refresh it a bunch of times and like and make assumption. Oh, this is fast or this is not fast. Okay, this is this is not scientific. It will not work. Absolutely. Okay. You need to to get more available where Zoots we're using idp2 as I mentioned that we use outer candle here. So the first thing that you need to do is install npm install autocannon. Please do that and answer me in the in the chat when you're done, okay? after installing run your where run the HTTP server and in another term now run autocannon localhost 3000 and said to me the result this result here how much requests in 10 seconds and how much throat would okay? It really depends off your machine, by the way. Okay, so One thing to mention here. That is very important. When you are performing a benchmark, make sure to be a dedicated machine. Which means that? If you are in a zoom call it will affect our benchmark. If you are just moving. Your mouth it will affect your benchmark. Okay, so usually When you perform a benchmark. I run dedicated machine in a cloud provider. Yeah dedicated CPU because if you like if you just get a easy ec2 in AWS, it contains a shared CPU. It means that your benchmark will not be totally realable. Okay, but running locally, it's totally like totally accepted. But if you are like if you want to to measure Very close to the production environment. I recommend a strong recommend to run your dedicated machine. Okay, let's move on. So autocanno is telling us that our server is really fast. It's really depends on your machine as I say, but this is expected because the service not doing much. Okay. It's just a simple way to be server. Now we will run the kinky doctor. So open to terminals. The first one you run Clinic Doctor dash dash node, hello world index.js in the first terminal and the second one. You run the outer Cannon again. And then went down to kind of finish you go to the clinic doctor. and do a control C and then it will open and tab in the In the in your browser, okay. When you don't let me know so basically you. you get A doctor pretty similar to these I mean it will depend of your machine. You can see the event Loop delay bigger or Low, it will Depends off your machine. but In a separated P server. You see that it was detected no issue. Okay. It means that there is no recommendation. So no issue was found. so everything looks good, so Click doctor, basically help diagnose performance issues in your application and guys you towards more specially tools to look deeper into your specific issues. So symptoms such as low CPU usage blocking garbage collection frequent event loop block. Or like a cock cut number of active handles might be might indicate a potential problem. Okay. So simple easy to be server. Simple doctor no issues. Let's keep going. Okay. You got CPU usage. Yeah, it might be like as I say most of the The benchmarks is the fact that by the zoom cower what is happening our machine for instance. If you are with your game your machine running background it can affect okay, so but in theory, you should get like if you run it again, we have more dedicated environment like if you close other tabs or you make sure that that no just will not be affected. The CPU usage. That should not be detected. Okay. Yeah, the air SS is basically resident set size. It is covered in the other Workshop the hip profiler Workshop, but also covered in the clinic JS dot org. He profiler okay. This is totally memory related. But I think that we will see something related to it in this Workshop. Okay? You are using node 8 14 try to use node 16, please. And also like make sure to that outer candle stops and then you run the control seat just one time, okay. I can run it here if you want. Let's go to the I increase the font size. Going to real Hello World. and here if I run node index actually Clinic doctor node index Is no is Halo word index, okay. And then Auto Canon. Okay, the autocanner is done. So I go to the other terminal and I press control C. And that the data will be analyzed and a new tab will open just like that and then no issue is detected. Okay. So if you in the future if you are doing it in your production in your like in your app and you are finding any bug, please raise an issue in the clinic JS repository, okay? So there's one thing that will help you in further analysis. That is the In Clinic Doctor. You can use one on dash dash on dashboard and then your comment or also Dash how to Canon directly. It means that for instance instead of having two terminals. I can just do these. Let me move it here. Right here. Okay, I can. touch Dash Auto Canon in the in the in the root path and then I I pass my application. So when autocanno finish its work, it will close the the node.js application. Automatically so look at that. Running it and it will close. and when done it will open a new tab right here automatically so you can check it if the two terminals doesn't work for you for some reason. You can do that. So yeah. Sure, could you command oh sure. So let me increase the font size. Okay, we have Clinic Doctor dash dash Auto piano, right? Autocannon is just like it will wrap the auto Channel basically instead of running these and in another terminal you run the autocannon like this. Right, you can run in a single command. So it means that right here if you run a clean doctor with dash dash Auto Canon. It will run out of piano when these server starts. Okay, when the node hero word index.js start a server a socket. It will run auto piano. But in which in which path is specifically so you spent you pass the parameters of autocannon here. So I want that autocannon run the full parameters in the road. Slash the root wrote like is the same as Local Host. 3000 is Lash. it's like Or for instance if I want if I for some reason I want to run a different route test, so it will perform Auto Channel basically here how to Canon. localhost 3000 test okay. Okay. So awesome. Now you you had inside of King doctor and when we are usually when you are doing a performance analysis clink doctor is the first is the entry point for everything because it will give give the visibility that you need in order to find any kind of butter neck for instance. If you are debugging a CPU boronac. The note the think doctor will say that the CPU is a problem. Then the entry point is still clink doctor, then it will be rejected to clean flame for instance that we will see further. If you have a problem with memory clean the doctor you say, oh go to the clinic he profiler and then you can check that. Okay. Let me see the other image. Okay? Okay. next next work You need to to set up setting up database because we use a mongodb to run locally to perform to simulate some requests to simulate database and not a database but to simulate a real API request. Okay, so first pull the image your machine I know that for Macos guys Docker is like difficult but if you are having trouble with doctor You can check the script that we have created to to run. Along with it be locally, but I don't recommend try to use Docker, please. You can skip this step if the docker container work it okay this the NPC is one npx one. Do we need to test it that works somehow or just like random commander and it's actually if you run both commands in dot bring any error, you should be able to run to go to the browser localhost 3000. And we will see a result some pretty similar to these three. Thousand. Oh, it's not these. Let me see exactly the Okay, no, no, don't don't worry, but if you just run Docker PS. And the container is up. That's fine. That's enough. Okay. Let's move on then. So now you have among DB running well for imported npm metadata, so you can run to the new server. Basically, the new server is pretty similar to this if you go to the If you go to the npm care server. You see these this code, okay. and if you run If you go to the to the server and run it node index. And go to localhost 3000 at least see. You should be able to get these kind of data. Can you check if you can get it? So it means that. The Mongo servers your Mongo the B is not up kakapur. so You are basically you need to run the docker run again. Perform a Docker PS and C. Okay now. Try running autocannon against it and see how many requests per second it can deliver try to run it. I would do it right now. So if you are like confuse it. Just follow me. Okay. I run my server. And another terminal I run. autocannon Is there a difference if you wanted the MPX or you install it and no actually, it's just a preference. I usually don't like to install Global scripts in my machine. I mean even npx running it. It's like a personal preference. Okay, great 51 requests in 10 seconds a hundred requests in the second in my machine. 81 requests. Oh, the Elder machine is very cured. By the way. So let's move on. So you you probably have not seen not said that the autocanner stats. The server is quite slow. It's very low. Let's try to investigate it. So try to run clinical doctor against this server and say and see what the reports like we run. your node index, but just instead of that you run click doctor and then out you perform the autocannon. To know the index. Okay, try to do it. So basically you get something pretty similar to these CPU usage and a potential IU issue. Usually when you go to recommendations here, you see that click bubbleprof is the recommendation to run it to to understand because clickable Prof is like It's called Bubble crop because there are a lot of bubbles on it. Nothing especially really. but if you run click bubbleprof, you might see something interesting so Let's go back here. As you can see the recommendation is to use bubbleprof. So basically what you need to do is just run instead of running. the king doctor like these You know. You just remove doctor and but bubble Prof. And run. Okay, basically you get the application running but I have fixed eating the 18 version of no Jazz, so don't worry about it. That's totally fine. Do this colors means something or No Yes. It means. When you hover dependencies, you'll see that some some things. I I mean, it might not show right here. But if you show like when I hover definition something you be highlighted here node core as well. and normally the operations is grouped by the yellow is the operation taking more time the the purple is the fastest one and the middle as you can see here. Basically, they are sync operations. is all hundred Handle by bubbleprof basically as you can see here, I run that script and I got for a thousand fifteen hundred cows of a synchronous horse over 10 seconds in my You my application. It means you know, it's a lot of asynchronous cow, but it doesn't mean a problem necessarily. You can check here the historical data of the sync operations and here you'll see a bunch of bubbles here actually are server is very small. So you see less. Bubbles, but yeah, like a big application you see a lot of bubbles. That's pretty cool actually. And the idea of the bubble we basically to track a sinky a sinky operations. So when you hover the first one it is the is our end point is our first fight server pretty similar to express one, but better. when you go to the left side, you see that there is npm can server and if you click here it will be expand and as you as you can see, this is the main connection to among the bit so we don't really care about it because the connection is created once So it will not affect our Benchmark at all. okay, but for instance if you click here, you'll see that stack trace for the The app for the operation like it will be created in these call stack, okay. going back So the left side doesn't matter to us right now. The right side is any PM care server, and if you click here you'll see that. There is one frame. It was called firstly in my Object index.js online 11 if I go to my server. In the line 11. Let let me just make sure that yes my line 11 is this is basically my call to the to the mongodb database. Okay, so it means okay. And taking let me see here. Let me just decrease as you can see here in the in the in the exact race. It's taking nine seconds. Just this I operation. Okay, usually during the the providing during The Benchmark nine seconds. I mean 98% of our time words spend in the in this connection, okay? So and the second one is the other curesor. Request if you click here and check the stack trace, it will show you that index line three treating. And it means that this or another call to the mongodb database is or another calorie. Okay, so, okay. Now we have found that. Most of our time were spent in this connection what we can do to improve that, okay? an idea like what is the issue or how can we improve that? So bigger bubble more time spender. Okay, basically is this so the bubble prop showed show that there is a ton of mongodb activity as you can see the cursor or something like that and this is dividing into mongodb bubbles and also they create connection and you can check in the UI the bubble Prof is the Both is there is hard to read honestly is very hard to read but once you get you get more family lighter that with that I think that it's cute to to help in your Suite of tools. Okay, so let's move on so bubbleprof is telling us that a lot of time night eight percent is being spent in mongodb. So we need to investigate how the time is spent like usually when you are working a Handler which is doing it Mongolian be connection database connection. It's expected that most of the time is expanding in the database. That's totally fine. But we need to check how much is a spender like The connection they carry is taking how much time? So if you go to the container. Same as we try it with. To Andrew you go to the container. And you run the use and PM actually go to the container with this command. I will run here. The container ID is basically you get in the docker PS, okay. And then you go you go to this command use npm to switch to the database to write database and then you run this carry. DB modules find basically the same carry that we are doing in your database nor application. Is the same one? Okay. So if you run it several times, you're not see that there is almost one second. I mean if 1500 actually 500 milliseconds of the lake. You cannot see that. if you run this carry with DOT explain Yeah, it can take few seconds. It will depends on your machine. Don't worry very much. I don't know if I say in your name. correctly, but apologize but if you run the same carry with DOT explain you see At a result pretty similar to this. This Workshop is not focused in database, but you need to know how database Works in order to optimize it. So as you can see when you run dot explain you see that the we are we are getting cold skin as hacker. I don't know your name. Sorry, but say that's totally right. Coast cam means a food scanning your database. It means that. In this requests in this scary. We are reading the entire database until you get the response and you're the in our database is not so big so it will be even worse if you are handling a big database. Okay, so Coast Gun is not good for us. an idea, how can we improve that like Just throw through ID in the chat, how can we improve that? Okay, we have found that Coast can't yeah is a problem because it can't entirely the entire table which you got to us. Great at index great great. fortunately We are ready had switched to post degrees a good one. Very good. We have another database. You can check it by let me see here. if you go to if you run DB dot get collection names. You'll see that there is other collection. The other collection is called modules indexed. So if you just change. the modules here to modules in the backset It is very it's way more fast and don't use a cow skin anymore. Look at that. So what you can do now is go to our exercise. In the big collection Line 6 you change the modules to modules indexed. And perform the autocannon again. Which result are you getting? just to For hefference, I run it without the the module induction indexing. how to Canon if the full values and then getting almost 80 requests in 10 seconds if I change to modules index it. Restart my server and running again. Again, I am getting 1000 requests. a big jump, right but Do you think that the current? Requests in 10 seconds is good. for instance Let's assume that. Or HTTP server is handling more requests. To do that. You can just change you can just add one flag to Auto Channel. I can handle I want to handle. a hundred concurrent clients at senpai if I run it Obviously, it should get more requests per second because it's more clients requesting. Okay, but it doesn't mean That your service is fast at all. for instance right here if when we run the auto-can with the full values with technical current requests concrete clients. We are getting a Max of latents of 100 30 31 milliseconds. Kind of acceptable. Okay, but when we increase the the concurrent clients to 100 or Max. Is 800 milliseconds and our average is almost 700 milliseconds and this is not acceptable. Okay? I wrote a blog post. Yeah, you probably got some a horse because the machine timeout and so on. Okay, I wrote a blog post. Like this one. This one that is with this amazing image. that when your your Client is phrases. You enter in your website. Okay, and you want to buy a product. I want to buy a new keyboard, by the way, I love keyboards. And I go to this to this website and I select my product and if they answer if I click the button and the button. Perform a request do something and I get there the response I get a feedback in. Between 0 to 100 milliseconds. I hope you oh this is instant. Oh, this is very fast. That's fine. When it goes from 10 seconds to 300 seconds. From a hundred to three hundred milliseconds. I will few eyes more perceptive delay and when it goes from 300 to one second, I will feel okay machine is working, but I can wait, but when it it expand that like more than one second. Likely, I have a context switch the task will be abandoned. For instance when I have a context with what I usually do I go to my Twitter and I should I look to other thing. Oh, for instance. I find this the same keyboard in order in the competitor website and it's not great. We don't want it for your clients. Okay. And these data is not taking from anywhere. I there's a project called human benchmark. Which is pretty good by the way, you can check and these. Milliseconds it's it's true. Okay, it's not it's not throwing. Rules to you, okay. Let's move on. basically If you look to your to your latest, you'll see that the average is higher, right? kiriakos Yes. Yeah, so that's not acceptable. Yeah. That's totally not acceptable. So okay, we have improved our app totally. Absolutely improve at our app, but we have we still have a bug we still have a bottleneck. We still need to improve with that what we can do. Okay, we fixed a bug we fixed at the bottleneck. We got more requests per second, but it's still a problem. We still have about our neck what we can do we start from from the point zero the point zero is clean conductor. So try to run clinical doctor again and see if you can pick up any new issues. Okay? I will let you to run it and try to analyze a bit of the click doctor, okay? Okay, basically you got something very pretty similar to this. Right. You got a quotation even Loop issue. Do you know what is the event Loop issue what this means after all can somebody just explain or open your mic and try to explain? basically, the event Loop delay is basically what As you know or might know doesn't really matter but node.js. Is a non-blocking. Platform it doesn't mean that is a single there is a single thread in. No Jazz. There is a bunch of threads in Libya V. But the main Loop is a single thread. It means that it ticks every time it will run your process. For instance. I want to do HTTP call actually mongodb call. I go to my Loop and they look looks okay. I want to create this connection to the database. It will bring it to create the connection database and keep going with the loop. And when the connection is established established. They even look we receive a notification. Okay, this task is done. I can take it again. And that's fine I operation so the the loop is not blocking. It's still going on. But let's assume that you are doing a synchronous task, which means signal stack is that you are waiting for something. You are doing a synchronous operation before moving the next one. It means that. For instance. If you are iterating over something it is a synchronous operation, even if you are doing like crumbs that are that's really matter, okay? It means that you are running your your event Loop and it's blocking in the middle in your task. And the time that you see even through delay is the time that your task was blocking. They didn't look. It means that for the next cheek of David Loop to get new tasks. It will need it. It needed to wait at least. fart 40 milliseconds to the gas the next one and usually They even look delay. Milliseconds should be very low. Okay, try to make it very low. So as you can see Click doctor show what that it was detected application event. Loop issue. And what is the recommendation? Use a clean flame to do that. Okay, you can read more, you know, it will have a better explanation of event Loop delay. You can check it right here, you know. What what is the the blocking operation synchronous operation asynchronous operation? You can check the half rest next step and so on by the way, we have a long tutorial over the The pink JS website, okay. So, okay, they recommendation is to use clean flame. Let's use that. You can just the same as you did for. The same as it did for bubbleprof you can run for flame graph. Okay. Basically the thing that you need to do here is removing doctor and adding flame just simple like that. Okay do it and It regenerate a flame graph. Flame graph is Amazing, by the way. Generated and let me know in the chat. Okay, we are almost done. Okay, don't worry. When you don't try to to read the flame graph. Not read, but just play with a few things and I will explain better. Okay. Flame graphs. This is a nice two to see how much? Time was spent in the CPU. Which function is taking is spending more time your CPU? as as you as you seen in the last slide click doctor show what that a quotation IO operation is blocking the event Loop normally CPU heavy tasks. And they recommendation was to radical Flame. clink flame generate this flame graphs how to read that it means the x-axis is basically. Bigger means more time spent the CPU if you Hoover for instance this one it will it is showing that 40 40% of the time that you are running this this profiling. CPU was working in this function. It's a lot. Really? It's really a lot. And the ACT there Y axis it basically the cow stock. So for instance, we have the index.js line treating and we have the cruiser and so on we can remove the dependence here if we want, but it we need to to keep it there if we want to see V8 house tax we can just click here. Okay? but basically we are calling. from this call When it's done, we basically we are basically calling complete magic. It's a function as you can see here. Your bitmagic is the Is the bottleneck? Cubit magic is doing a lot of CPU work. How can we do to solve that? any idea So computer mat compute match is used a lot. So if you if you go to near form computer magic, you see that it's creating a hash iterating over several times. And you're creating the hash with adjacent stringify. And by the way, Jesus certified is a synchronous operation as well. So we are iterating with a lot of for loops and we are We are. Doing that. I mean if you look to the code this usually happens. Okay. It happens a lot. I mean I I deliver Pharmacy consultancy for several companies and a lot of companies Are using function that don't really need okay for instance the magic. is not the thing that we we need in these in this application, we can probably we can just remove because this endpoint as you can see here this endpoint is returning the five newest and the five oldest Rose in your In your your database so it means that there is no mention to Magic The Magic was not expected to be there. I mean it was old future but nobody used it and the best optimization is always to remove something. Okay. For instance in 80 to be the best request the request the faster. The request is a request not made Okay, so Cupid magic is a thing that we don't really care. We don't need so what we can do is just remove that. Pretty simple. I would love to remove a lot of things. Okay. I very I normally like to remove things in Fast Five. I love to remove a lot of things and in order to make it fast, okay. so Try to remove and run autocanner again, and show me the results. Okay? Okay. Yeah, that's totally accepted accepted. Because basically even Loop it utilization doesn't mean even Loop delay. That may explain it a bit. These are things that we are fixing No Doctor by the way, eventization means just that you are performing work. You are using the the event Loop. so for instance You run your ATP server and you're already server. You run also out you can autocanno. It's performing a bunch of requests to your server. So is expected to see a bigger like even utilization around of 100. Percent, but it will Depends off your machine. Okay. So if your machine can handle more requests than others, they even Loop sization will not be so big. Okay. So in your case it it is showing I eventually fertilization. But it I mean if you look to the event Loop delay, it will not it is not more than four milliseconds. So that's totally acceptable. Okay, yeah. Thanks. So for most of you you see that everything looks good right as in my my image as in my Dashboard here. Everything looks good. Does it mean that it can show that there is a problem, but there it's not actually a problem and How to understand those cases like which are false positives because here you planned us. Okay, but if we are on our own we just do it and we think okay still a problem. What should I do? And yeah, totally what you can do in for instance in case of the key records instead of running out to Canon in the same common as click doctor. You can run out of piano. Yeah, chief of terminal and basically. You run your server with clink doctor and you run the out again another terminal and when autocanno adds you wait for a couple of seconds like three seconds before sending a control C to your application. So then they event utilization should be less should be low and then you you're not see any issue, but to identify false positives is hard. But it will depends how much experience you have on that. When you are performing a benchmark is expected to see CPU usage on under high usage and even tributilization. What can't. Below be behind is the event Loop delay regardless of the load that you are sending to your server. They even to play should be low as possible. Okay, so it didn't find false positives is basically in the case of a curriculus is not a false positive. It's a positive. It's like even to be delay is doing a lot of work. They even Loop utilization is doing a lot of work and that expected. That's fine. Because you are doing a CPA performance analysis. Yeah, a benchmark analysis. You can check in. My machine that they eventualization is around 94 94% and that's pretty similar. Okay. but one thing important to mention here is that even click doctor showing that there's like the tech that no issue. It doesn't mean that your your server is is done like you don't have anything to optimize. Click doctor is not. Super bullet for everything you need to to understand by yourself. Like you have establish it a baseline in the beginning of this Workshop. At least in my machine, I was handling around of. 400,000 requests And now I handling. Let me see how much here. I don't remember. Let me run again. Okay, no index. and autocannon I handling almost five almost 49,000 requests in 10 seconds. It's Fairway from our Baseline I mean It's very difficult to be close to the Baseline because the Baseline is just a simple way to p and now and here we are using a framework. We are using database connection. So you want reach to at least my machine you want to reach 500,000 requests in 10 seconds. Okay? But we need to be better. for me this amount of request in 10 seconds is not good. I can improve even the Clean Doctors showing to me that is not there is no potential issue apparently. Try to run the same application. With bubbleprof. Okay. I will run it here as well. click doctor so bubbleprof I have a question. Go ahead on our Baseline. index.js we don't use we just use the HTTP coming from node.js and we don't use the first five to set the Baseline because you said it's not framework. We don't use anything. Why don't we just start a fast server? and Baseline so that we know that there is some small overhead, but there is some so to count the requests who can serve using the bare minimum from the Yeah, that's a great question. That's totally fair question. Basically, let me just remove it here. No Trace here notice here. Basically, we have added a baseline because even in these Workshop we are not like showing a business application. We are showing layers of improvements festify is fast, but still is a layer on top of the Once we need to use festify we need to understand how much overhead it adds on top of the TV. So In your case. We are using festify in the second application just because it's a business requirement. We are simulating a business requirement. We can for instance for a baseline. We can instead of running the HTTP the nodejacent to be just run fast five. We can't of course, but the the proposals of these this Workshop is to show layers of improvement even Fast Five being very fast. Is it still a layer of improvement? yeah, okay, because I don't think despite it is fast and I know because I'm using first five for years and it adds a bit of overhead rather than The native node.js HTTP server. So in in order to in our comparison we cannot read the requests that We got when we run the first indexes. so I in my opinion would be if a more fair comparison if the first if the Baseline was also using pacify. But okay, I understand the point of view. I'm just saying that we cannot. improve Further than certain point. Yes exactly. As far as we're using more Frameworks libraries, whatever that we we cannot avoid using them in our business application exactly. That's a good one. I just fixing clinics here. Sometimes it has a bug but it's like other tendency a dependence of Click it's possible to attach Clinic to a production workload running Cloud. Yeah, it's possible, but it's not recommended. When you attack Clinic to something it will handle way less request. So it was designed to run a locally. Yeah developer machine. What about a webpack and typescript? Okay. to run Clinic with web back or typescript application you need to to build to build everything you don't you don't really don't want to run our Clinic before the bundling So make sure to run clinic in your last application. As your last application here. Yeah, you should be able to see recommendations. Yeah with Source Maps. We don't support directly Source Maps. It might work for a few tools of The Suite. I guess that clinic flame works well with it. But I don't think that. Like Heap profiler will work. Well, honestly. Let me run it again. Did you guys run the bubble prop? And as you can see in the warning here nadab bubbleprof does not support a source Maps. so it answered your question, I guess. Okay, and having an a horse, so what they can do is basically switch my no jazz version. It doesn't really matter because I have these already here. But did you guys run the bubbleprof with the latest? No jazz version that with the latest. application Dawn okay. Got the nehor. Okay, no problem at all. Whichever is like premature clothes. Yes. Yeah, this year is using bubbleprof for some time is the same as I'm getting. This is like flaky or and we are just working on that too, so that so but basically let's go to my to this image that I have here. Okay Ramesh go. Go ahead. Thank you for your participation, by the way. But basically if you run the bubble prop you you get something pretty similar to these and as we have seen. The left side of the bubbleprof doesn't really matter. But the right side is important piece of the of the software. Of this application if you click here if you run here you'll see that basically bubbleprof is a bunch of bubbles. and you have cow Stacks here here you'll see that npm care server basically is also a database connection as we have seen. it's taking time and then when it's done. It calls another. database cursor it means that this Synchronous like the second operation is waiting the first one to to complete to run. So if you run the if you look to the code. We are not using the newest. in in the second carry So what we can do here is basically. Be a synchronous. We don't need to wait one to run. The second prompts out is a good option. So what we can do here is basically Mongo JS use a or the old approach the Callback help approach. So what we can do is basically import file. from YouTube this building on no Jazz and then I will be able to run it. Let me see one thing here. Just one second. And just one second. Hey and just answering someone here in the front of each Nation. Okay, what we can do basically is using promise that are okay. Basically, we need to recreate the find method. So let's refactor that so motifed is basically if it's sorted or not. So we return a new promising. Actually, we can just remove that because we are using the problems that out, right? so a good approach to do that. It's basically let me show without the problems that are so you can refactor if you want but basically the performance is the same. Okay, so let's create the problems that are The the new promise basically I have the resolve. And I have the result and it has a function and I will call C dot find. And sort based in the modifier. obviously limit five and whenever I receive the response here data. and If I have a neighbor, I will reject their. error Otherwise, I will resolve the data. Simple like that. So what we can do here is basically We have the newest and we have the oldest let's comment it. Okay. Before that's remove it from here based here and let comment it because we do it better. So basically we have newest and oldest here that equals prompts dot all. And the array is basically. We can find with minus one for newest and we count fine with one for oldest. And that's pretty much it. Let me see if this work. note index should return should you return online 8 or like else at least? Because you resolve in any case right? Just okay. Sorry. But online, I'm sorry online 12. No. Yeah, actually, yeah. Let me just adjust it to you. Okay, go ahead. I mean would you like? 12 so you if there you reject and then you always resolve so it's not like else resolved. Yeah is the same when you reject something and basically would throw but it's the same as this. Okay good. Okay. Okay. So now we have a simple solution and it should be more fast right? So let's run it. Running it and Performing autocanner again. I was handling. almost 50,000 requests in 10 seconds. Let's see. How much now? Okay, there is an error. Let me see what's happening. localhost 3000 okay, I think that I'm having an error here is basically Limit, five the sword see fine. No problem here. more fired should it be like an object modified one or Oh, yes. You're right. Thank you. Let me just check if this working. Yes, it's working. And then let's see how much requests. Not too much. Not too much. Yeah. basically Let me see if I'm using everything correctly. Yeah, I am. but it really depends of the the Sorry, it will Depends off the zoom call because I mean I have delivered this workshops several times and there is no a big jump but is a small perceptive. Improvement so Just for comparison. I can like go to the old one these I guess right. copy that and see here. Remove the magic. remove this remove this and run it again. Get your handle less. Or almost the same but like a small perceptive delay, but always use problems that are when you need to look as you can see here 2K less request. The zoom cow really affects that okay. But yes. this is you can use you to that problems fight you optimize it if you like if you like to refactor. so basically This one here. I forgot to mention. Let me see. Okay, you should see eyes now performance increase as well. Yeah, as I as I thought this is not too much. In this case, but most of the cases a lot. And basically to finalize the clean flow you have Richard to the end of the workshop. So great job.
71 min
04 Jul, 2022

Watch more workshops on topic

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career