iTranslated by AI
About isucandar and the ISUCON9 Qualifiers Benchmarker
Prerequisites
This entry provides an explanation of isucon/isucandar from the perspective of an implementer of the isucon/isucon9-qualify: ISUCON9 Qualifiers benchmarker. For an explanation of isucandar itself, please refer to the repository's README, as it is covered there to some extent.
This entry also includes some personal reminiscences about what I was thinking when I was implementing the ISUCON9 Qualifier benchmarker. I suspect this entry will be of little benefit to anyone who hasn't implemented a benchmarker before, but since that's the nature of this post, I recommend closing it and not reading further if you aren't interested.
Furthermore, this post assumes you have read the following entries I wrote last year. If you haven't, please read those first.
Also, the benchmarker implementation can be found below. You probably won't understand what I'm talking about unless you've read the implementation to some degree.
isucon/isucon9-qualify: ISUCON9 Qualifiers
What kind of program is an ISUCON benchmarker?
In ISUCON, there is a wide range of systems that the organizers must prepare. Among them, the benchmarker is unique and has historically been the program that question authors struggle with the most.
This is because, for example, the ISUCON reference implementations or the portal are Web applications with a certain scale of specifications; anyone with experience and knowledge of backends in a Web company can understand and implement them. While the portal has a job queue system to execute benchmarkers, Web applications with job queue systems are not rare—one doesn't even need to mention Sidekiq in Rails. Although there are various requirements, the portal can be built using general Web application know-how, so it isn't necessarily a system unique to ISUCON.
However, the benchmarker is different. I'm not sure how well this is understood, but if I were to summarize what a benchmarker does in one sentence, it is: "A program that continuously sends a massive volume of requests to a specific Web application in parallel, to the extent that the application doesn't die, while continuously validating in parallel whether those responses are correct."
The most essential knowledge required is related to concurrent programming, but knowledge of networking/HTTP and system programming is also demanded.
While it was different in the early days of ISUCON, since at least ISUCON4, benchmarkers have only been made in Go or Java (only ISUCON5 was Java; all others were Go). These two languages have well-established libraries and language mechanisms that can utilize concurrent programming, networking, and system programming. I believe they will continue to be created only in languages where these mechanisms are well-established.
Knowledge of concurrent programming, networking, and system programming is unfamiliar to many Web engineers, and only a handful of people have ever actually created an ISUCON benchmarker. Consequently, know-how hasn't accumulated much; it has been the program that successive authors have struggled with most, leading to numerous troubles.
One of the goals of the ISUCON9 Qualifier benchmarker was to break through this situation. If you check the git log, you'll see that the ISUCON9 Qualifier benchmarker started as a very small program and was implemented from scratch. One reason for this was the goal of providing as simple and easy-to-understand a program as possible so that future benchmarker implementers could use it as a reference.
About isucandar and the ISUCON9 Qualifier Benchmarker
As stated in the README, isucandar is:
isucandar is a benchmarker framework that collects features that can be used for load testing such as ISUCON
As a premise, the ISUCON9 Qualifier benchmarker does not have a framework-like structure. Regarding the libraries used, other than morikuni/failure for assembling error messages and a library for generating QR code images due to application specifications, only the standard library was used.
isucandar provides several mechanisms that the ISUCON9 Qualifier benchmarker equipped itself with internally.
The ISUCON9 Qualifier benchmarker executes multiple scenarios in parallel while controlling the speed of each scenario so that they do not run out of control. Since the execution time is fixed, all scenarios are designed so they can be interrupted at any timing using context. These mechanisms are extremely important for preventing the benchmarker from going out of control. The ISUCON9 Qualifier benchmarker does not guarantee these through some automated mechanism; rather, they are guaranteed by me writing OR reviewing all the programs. If even one program that does not exit via context remains, there is a possibility of it running out of control.
The issue where passing a program that doesn't exit via context can lead to it running out of control also exists in isucandar (as it does in almost all Go programs), but isucandar provides this functionality in a package called worker. The mechanism provided is almost identical to the ISUCON9 Qualifier benchmarker, but unlike the ISUCON9 Qualifier benchmarker where it is implemented as part of the scenario, it is made easily usable as a package.
Much of this logic is inherited from the ISUCON9 Qualifier benchmarker. I will discuss the differences later.
Why I didn't create isucandar
The ISUCON9 Qualifier benchmarker was not the first benchmarker I had ever created. I have experience as an organizer for the ISUCON6 Finals, and I created catatsuy/private-isu from scratch as a problem.
Based on this experience, as I described in About ISUCON9 Qualifier Problems, External Services, and Benchmarkers:
Based on previous reflections, I decided on the following policies:
- Expand from a single file
- Do not create framework-like mechanisms
ISUCON benchmarkers are a parade of exceptions. Even if you prepare a framework-like mechanism, it will either go to waste or become incredibly complex. By building it from scratch this time, although the initial cost was high, I think I was able to create a benchmarker with high maintainability.
Thus, I intentionally did not create a framework-like mechanism. This was because my own know-how for creating ISUCON benchmarkers had not yet accumulated, and I did not understand the specifications generally required for an ISUCON benchmarker.
Trying to provide a general-purpose mechanism poorly in a situation where you lack an understanding of generally required specifications usually leads to failure. Although I felt like providing an abstract mechanism, I judged that I couldn't create one with my knowledge and created the ISUCON9 Qualifier benchmarker with a strong will not to perform abstraction. If I had created benchmarkers for two or three more completely different problems, a common general-purpose mechanism might have solidified, and I might have tried to create a library like isucandar myself.
The ISUCON9 Qualifier benchmarker not only made it possible to terminate all scenarios via context but also included the following challenges that hadn't been done before (these are just examples, as I did quite a few "firsts in history"):
- Make the score dependent on a scenario like "sales" rather than per-request.
- Process what is called "Popular Item Listing," which is like a parade of concurrent processing.
- A single user lists an expensive item, and a large number of users try to buy it.
- The benchmarker not only acts as a client but also hosts its own servers for two external services directly.
- It hosts "Payment Service API" and "Shipping Service API" and performs internal verification.
To conduct several first-time challenges, it was necessary to ensure the extensibility of the benchmarker. Creating a framework-like mechanism poorly could have become a hindrance to extensibility, and I lacked confidence in providing a library that would not obstruct it. Therefore, aiming to realize what I wanted to do with the simplest code possible, I developed only the necessary functions by starting from scratch. As a result, while it took some time for the ISUCON9 Qualifier benchmarker to take shape, I was able to develop it without slowing down the development speed until the very end. I still believe that if I had the time, it would be easy to modify or add scenarios.
I still believe that not making a library like isucandar at the time of ISUCON9 Qualifiers was a success. If I had, the library's design would have collapsed midway through, and I would have likely given up on many of the planned features.
Differences between isucandar and the ISUCON9 Qualifier Benchmarker
Let me explain a few packages of isucon/isucandar.
Since I am writing about the implementation of the ISUCON9 Qualifier benchmarker, you probably won't understand what's written unless you've read the following entry and the actual benchmarker implementation.
agent
I'll talk about the agent package.
isucandar/agent is a package created for the purpose of behaving like (or mimicking) a browser.
It is based on net/http but has several extensions.
isucandar allows you to change behavior relatively easily by passing options using a method often seen in Go called Functional options.
Functional options for friendly APIs | Dave Cheney
Also, it is implemented with a browser in mind, such as handling 304s, supporting not only gzip but also Brotli, and the ability to send requests by parsing HTML.
In the ISUCON9 Qualifier benchmarker, I handcrafted each part within a package called session. This implementation has a lot of duplication and quite a bit of copy-pasting. While this might look inefficient at first glance, as I already explained, "ISUCON benchmarkers are a parade of exceptions," so I deliberately chose this implementation to avoid complicating the overall structure due to a few complex requirements.
One of the design philosophies of the ISUCON9 Qualifier benchmarker was to "prioritize readability over ease of writing." This makes writing a bit tedious, but each implementation becomes very simple and easy to read. Complex requirements correspond to complex functions, and simple requirements to simple functions. I believe this policy was one of the reasons I was able to maintain development speed until the very last minute.
Furthermore, in the ISUCON9 Qualifier benchmarker, I hardly considered browser behavior. This was a result of a policy not to implement things until necessary, based on a reflection that considering browser behavior had made implementations unnecessarily complex in the past. Additionally, since there was a policy not to make bandwidth an issue, the fact that requests for static files were kept to a minimum also had an effect.
The ISUCON9 Qualifier benchmarker had a unique behavior of "sending HTTPS requests to a domain not registered in DNS while verifying the certificate." To achieve this, an implementation like the following is necessary:
&http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{
ServerName: ShareTargetURLs.TargetHost,
},
},
}
The agent package does not have an option to pass this. Does that mean the ISUCON9 Qualifier benchmarker couldn't have been built with the agent package? Not at all. You could rewrite it by preparing a function like the one below using the Functional options feature mentioned earlier:
func WithTLSServerName(host string) agent.AgentOption {
return func(a *agent.Agent) error {
a.HttpClient.Transport = &http.Transport{
TLSClientConfig: &tls.Config{
ServerName: host,
},
}
return nil
}
}
The great thing about Functional options is that you can change the behavior even from functions outside the package if it's a simple modification. In Go, it's often difficult to extend a library without directly touching its code, so it's nice that it's designed to avoid that. You can feel a strong enthusiasm for making it truly usable as a framework from such aspects.
In general Go programs, it is considered taboo to generate http.Client or http.Transport every time, but in the case of an ISUCON benchmarker, since we want to mimic the existence of multiple clients, there is no other choice but to generate them every time. It seems convenient to use as an ISUCON HTTP client.
In the ISUCON9 Qualifier benchmarker, JSON decoding and simple checks were performed on the session package. If you prepare functions as needed, it is easy to implement such things. Since you can do anything if you implement it from the outside, isucandar is implemented in a way that minimizes the need to extend the library itself.
failure
isucandar's package for handling unique errors and their collections.
This is a mechanism for collecting errors. As I wrote in my own entry, error handling in benchmarkers is a bit different from other applications. To summarize:
- Organizers want to see raw errors, but we want to show contestants specific messages we've prepared.
- Errors need to be categorized into several types based on severity, such as fatal errors.
- Even if an error occurs, other processes should continue, and messages should be shown to the contestants all at once at the end.
To achieve this, the ISUCON9 Qualifier benchmarker used morikuni/failure to attach messages to errors and prepared its own fails package to collect those errors.
The fails package was a fairly simple implementation; it only output the original error to standard output and collected only the attached messages and error codes. Also, while scenarios were built to take a context and terminate entirely when the context was Done, fails did not support context. This was because the count is retrieved immediately after the scenario execution, so there was little point in intentionally stopping error collection via context. Ideally, it should have been able to take a context, but I skipped that implementation because it was not highly necessary given the structure of the ISUCON9 Qualifier benchmarker.
isucandar/failure properly implements the parts that were simplified in the ISUCON9 Qualifier benchmarker. It implements the mechanisms that are naturally necessary for a framework. I was surprised to see that it was implemented while taking into account the parts where I had cut corners. It's excellent.
score
This part did not exist in the ISUCON9 Qualifier benchmarker. While I think most ISUCON benchmarker implementations separate this into a package, in the ISUCON9 Qualifier benchmarker, the scoring method depended on sales rather than requests, and since deductions were retrieved from the error count, it was of a scale that could be sufficiently implemented without a dedicated package. As a result of my policy of not refactoring until necessary, no refactoring was done until the end.
The score package is designed to handle scores by tagging them. This mechanism is simple yet powerful, and I found myself saying "I see!" out loud. Furthermore, you can also pass a context here to stop collection. Since it consistently allows processes to be stopped via context, it can be used with peace of mind.
Unless you are in a special situation like the ISUCON9 Qualifiers where sales become the score, I think it can be utilized in most cases. It seems very convenient.
worker and parallel
worker
Provides control for processes that execute the same task multiple times or run infinitely while suppressing concurrency.
parallel
Provides processing to execute multiple jobs while controlling the number of simultaneous executions.
These two packages can be called the core. I think I could have created the packages introduced so far if I wanted to. However, these two packages were the parts where I least understood how to extract them into a package.
In the ISUCON9 Qualifier benchmarker, since I initially didn't know what kind of implementation was required overall, I proceeded with a policy of implementing what was needed as it came up. As a result, it became a simple implementation overall, but it is structured such that one cannot understand even a single scenario without deep knowledge of concurrent programming in Go. Regarding this, since it's impossible to understand ISUCON benchmarkers in the first place without deep knowledge of concurrent programming in Go, I wasn't particularly concerned, but looking at other people's implementations, I feel it is the part that is least understood.
The worker package uses the parallel package internally to perform concurrent processing. Basically, you should use the worker package, but the structure allows you to use the parallel package effectively if you want to execute special processes in parallel.
What it's actually doing is almost the same as the processing in the ISUCON9 Qualifier benchmarker, but there is one clearly different point.
In the ISUCON9 Qualifier benchmarker, the execution time of each scenario was limited using time.After. This was due to the following reasons:
- If it's a simple
forloop, there's a possibility of it running out of control due to an error or some loophole, so it's prevented from moving faster than a certain rate. - To prevent cheats where specific paths that are difficult to optimize are slowed down to increase requests to paths that are easy to optimize, specific goroutines are kept from making more than a certain number of requests.
If you implement something using the worker package without thinking, this problem will occur. For example, if the implementation of f passed in worker.NewWorker(f, WithInfinityLoop()) runs out of control, it will be executed a massive number of times unintentionally. So, should you limit the number of loops with worker.NewWorker(f, WithLoopCount(5))? In that case, if the scenario execution speed is not limited, it might finish too quickly, causing a problem where no requests come in at all toward the end of the 1-minute period.
So what should be done? This should be implemented individually on the function side to restrict it from moving above a certain speed. Of course, since a context is also passed, one must not forget to exit when the context is Done. Doing so should result in the same behavior as the ISUCON9 Qualifier benchmarker.
By using the parallel package, I believe special requirements like "Popular Item Listing" can also be met. Because it's a simple implementation, you need to craft the passed function thoroughly, but that's exactly why it's a package with a wide range of applications.
However, if you're going to build an ISUCON benchmarker using this package, you need to use it after reading the implementation of this package thoroughly. I think it's impossible for someone who cannot understand the concurrent processing in this package to build an ISUCON benchmarker using it.
Nevertheless, I was surprised that the concurrent processing of the ISUCON9 Qualifier benchmarker could be extracted into a package through such abstraction. In my mind, I hadn't been able to judge how much should be abstracted and how much should be implemented in the scenario. I am very happy to see the implementation of the ISUCON9 Qualifier benchmarker organized so beautifully.
Should future ISUCON benchmarker authors use isucandar?
My conclusion on this is:
If you are not confident you can build an ISUCON benchmarker from scratch, you should use it.
I want to support people who will implement ISUCON benchmarkers from scratch in the future, but if you're not confident, you will undoubtedly fail, so you'd better use isucandar. I'd also be happy if you used the ISUCON9 Qualifier benchmarker as a reference.
Closing
I think it is extraordinary that someone challenged the framework-ization that I completely gave up on when making the ISUCON9 Qualifier benchmarker and released something of this caliber. And I was able to feel that what I did with the ISUCON9 Qualifier benchmarker was not in vain. I feel a very strong respect for the ISUCON9 Qualifier benchmarker.
My own battle with ISUCON benchmarkers began in the year of ISUCON6. From there, I kept thinking for years about what should have been done, and finally arrived at the implementation of the ISUCON9 Qualifier benchmarker. When I started making it, I was worried whether I could complete it, but I cleared the problems one by one and ultimately was able to create something almost exactly as initially envisioned. While I was making it, I hoped that this benchmarker implementation would push ISUCON and the industry forward. Honestly, I wasn't sure if the code was easy for others to understand, and I was haunted by the anxiety that no one besides myself would be able to understand it, but I am truly glad to see an implementation that completely understands the ISUCON9 Qualifier benchmarker and advances it even further.
As a result of my desire at the time to advance the entire industry through the implementation of ISUCON, I am extremely happy. I expect that internal ISUCONs and the like will be implemented using isucandar from now on. I believe ISUCON still has the potential to push the industry forward. I'm looking forward to future ISUCONs as well.
Discussion