Alright, let's talk a little bit about logging. In a perfect world, our system is working perfectly, but actually usually in reality, there are a lot of problems, applications crash, there are random bugs, data is missing, so logging always helps us to debug, and logging is something that we all need in our applications. But logging can cause performance degradation.
So I was, or I'm sorry, it cannot, it will always cause performance degradation. So I was comparing one server with a POST request that is, you know, with no logs, and another server which have only one log line, and that's it. And look what we have here. If with no log, the average request per second is around 9600, then when we have only one log line, the throughput is down by around 15%. Look at here. The average request per second is around 8400. In the last results, we were able to serve like around 100,000 requests in the total benchmark. Now we're able to serve like 93,000. That is like 10% degradation.
So when you're thinking about logging, that's also something that you need to take in mind to take a library which is performance efficient. Different log libraries that are, you know, popular right now are, let's say, Winstone, PinoJS, and other libraries that I took for my benchmark comparisons are Banyan and Log4j. So when we're looking at the overall, we're gonna see that PinoJS is the library that currently gives you the less overhead over your code, meaning that that is like for now, for my comparisons, the most efficient log library performance-wise, and it would give you the best results for not harming your application performance. So we can see that in the average request per second, and we can also see it in the total request as you see that Pino, the server with Pino was able to serve the amount of requests which is closer to a server with zero logs. So if we're conclusioning that, so if you're thinking about performance and you're thinking about performance efficiency, so prefer PinoJS over other libraries. That can change, but that is like for now, the most efficient log library out there. Another tip that I would like to give you, which is important, is, you know, when we're doing logs, we have a lot of string concatenations, but the log library should manage this string concatenation. Don't do the string concatenation in your application by yourself. As you can see in the example which is below, because it seems like minor to you. What is actually happening behind the scene, that if my log level for now is like error, then that string concatenation won't be done by the log library. If I'm doing it by myself, it will always happen. So it looks minor, but really in large application, complex application, those things add up. So always prefer that the string building would happen with an interface, which is suitable for that from the log library, which receives parameters. Okay, now actually I was doing like tons of examples with Express. So the question is whether we can do something else, maybe not Express. I mean, Express is, to be honest, is kind of old. I don't know if you know that, but Express release version is version 4, and that was released in 2014. That was quite a long time ago.
Comments