Generating Reports With Code
Last week I was running load tests against a new server and needed to produce reports from the results. I wanted to have graphs to show the response time as the test progressed, and thought this would be a good time to try a couple of different methods of creating the reports. The first report was generated with Microsoft Word and Excel, and as I struggled with Excel's insane copy and paste, and Word's inane auto layout decisions, the one thought that kept occurring to me was "why does anyone put up with this?" The next step was to break out the power tools with Python and LaTeX.
I used siege for the load testing, and redirected the output to a file. The siege output gives me a nice baseline to work from, but simply redirecting the output also gives some cruft that needs to be cleaned up. During the first go around with Excel, I needed to open up each file in Vim to clean it up before I could import the data. In the process of cleaning up the files, of course the thought occurred to me that I should automate that task, but I try to avoid unnecessary scripting when I can. Once the graphs were created, they needed to be copied and pasted into the Word document, which I then spent ten minutes trying to get each graph to look uniform. Admitted, I'm not a Word or Excel expert, but I do know that repetitive tasks and document layout are two things that computers do well. I should let them do it.
LaTeX is complicated, but it is very good at what it does. Unfortunately, outside of the TeX family of layout engines, there is very little that can do good document preparation without resorting to a WYSIWYG word processor. Markdown, HTML, and CSS seem like good candidates, but HTML simply was not made for representation on paper, and handles page breaks poorly. I would love for an easier layout engine to be developed, but once you get the hang of LaTeX, and use a reliable template, it's not bad. The good thing about writing in LaTeX is that you can decide how you want your document to look in code, as a declaration, instead of clicking and dragging with a mouse and getting things almost right. Once I had a good template to work with, creating new reports became very easy. In the end, it is much faster to create multiple similar reports with LaTeX than a word processor, but it may be not always be faster to create a single report. Creating a report in a word processor will take the same amount of time each time the report is generated, but doing it in LaTeX takes a long time the first time, and then only a fraction of the time each subsequent time.
To create the graphs I turned to the NumPy Python project, the "fundamental package for scientific computing with Python". It was very easy to get started using the provided tutorial, and having a bit of a background in Python didn't hurt. Once my script was written, I was able to generate new vector PDF graphs from each run of siege with a single command, which I then dropped into the LaTeX report. Again, it took longer to write the script than to do one graph in Excel, but the speed up in generating subsequent graphs was significant. And, I get more control over how the graph is laid out, and in my opinion, a better looking graph.
Obviously, my experiences are not typical, but I would encourage you to take a critical look at how you use traditional office software suites. Dr. Drang has been kind enough to share his experiences with writing reports over the past twenty years, I highly recommend reading the entire series. I don't quite have his range of experience, but I feel similarly about how I write and the time I spend solving problems.