Monday, September 29, 2014

Tiny C++ Benchmark Framework

Lately I had to improve a little some of my algorithms. I wanted to check, how changes affect to time of algorithms execution. So I started to search benchmark framework for C++. After a while, I have found quite small, but powerful framework - google benchmark [1].

Simple usage

The simplest usage of google benchmark is shown below:
#include <benchmark/benchmark.h>
#include <algorithm>

int complex_computation(int n)
  if (n == 0) 
    return 0;

  unsigned int a = 1, b = 1;

  for (unsigned int i=0; i < n-1; i++) 
      std::swap(a, b);
      b += a;

  return b;

static void BM_Fibonacci(benchmark::State& state)
  int ret;

  while (state.KeepRunning())
    ret |= complex_computation(500);

  CHECK(ret != 0);


int main(int argc, const char* argv[]) 
  benchmark::Initialize(&argc, argv);

  return 0;
At the beginning, we have to define function, which will be measured (complex_computation, in my case). Next, we're defining benchmark method - BM_Fibonacci. It has one argument - state (I will talk about it later). In while loop we call our method, until benchmark's working. ret variable is used only because of compiler optimizations (we'd like to avoid removing dummy code). Benchmark method should be later registered (using BENCHMARK macro). In main function, framework should be initialized, and after that, we can run all our benchmark methods.
loganek@hf-gcs-computer:~/Documents/blog-benchmark$ g++ benchmark.cpp -std=c++11 -lbenchmark -lpthread -O2 -o benchmark
loganek@hf-gcs-computer:~/Documents/blog-benchmark$ ./benchmark 
Reading /proc/self/cputime_ns failed. Using getrusage().
Benchmarking on 4 X 2701 MHz CPUs
CPU scaling is enabled: Benchmark timings may be noisy.
DEBUG: Benchmark      Time(ns)    CPU(ns) Iterations
DEBUG: BM_Fibonacci        208        225    2222788                                  

As you can see, there is computation time, cpu and iterations count - everything what we need :) Moreover, we can also get a few informations about machine.

Arguments, range of arguments

Snippet shown above is very simple, but this framework offers users much more. We can define a set of arguments for benchmark. Let's remind previous example. We tried to measure fibonacci implementation. But I hardcoded an argument. Sometimes we'd like to measure one method with different arguments. We don't have to define BM_Fibonacci_5, BM_Fibonacci_42, BM_Fibonacci_87 functions for different arguments. Let's look at the improvement of previous code:
static void BM_Fibonacci(benchmark::State& state)
  int ret;
  while (state.KeepRunning())
    ret |= complex_computation(state.range_x());
  CHECK(ret != 0);

and the output is also quite different:
loganek@hf-gcs-computer:~/Documents/blog-benchmark$ ./benchmark 
Reading /proc/self/cputime_ns failed. Using getrusage().
Benchmarking on 4 X 2701 MHz CPUs
CPU scaling is enabled: Benchmark timings may be noisy.
DEBUG: Benchmark         Time(ns)    CPU(ns) Iterations
DEBUG: BM_Fibonacci/5           3         19   25832109                                  
DEBUG: BM_Fibonacci/42         18         35   14398500                                  
DEBUG: BM_Fibonacci/87         44         61    8224413   
We've access for results of every argument, which I passed to a benchmark method.
It is also possible to pass range of arguments to our benchmark method.
BENCHMARK(BM_Fibonacci)->Range(0, 1024);
Defined range runs benchmark for min, max in this range, and every power of 8 from this range. So in my case: 0, 1, 8, 64, 512 and 1024.

Multithreading, other features

Google Benchmark framework gives a few other features for developers:
  • multithreading
  • benchmark templates
  • pair of arguments
  • custom reporter class
You may see usage of other features in google benchmark example code [2] or on README page[3].


That's one of first test framework, which I used for now. Previously I wrote simple methods for measurements, but it wasn't so convenient, and in the long run, it's just waste of time. Moreover, benchmark helps me keep in order my tests, so my code is much cleaner than before.
In the future, I'd like to check also other frameworks; I heard about Celero [4] and hayai [5]. But for now, google benchmark fits my needs.


Friday, August 29, 2014

gstreamermm on Windows & Visual Studio

Hi everyone!
Recently me and my friends have ported quite big project from Linux to Windows, and had to build it in Visual Studio environment. (Un)fortunatelly, we're strongly dependent on gstreamermm project, so I had to prepare .vcxproj files, which allows us to compile gstreamermm in Microsoft's IDE.
I spent one evening on that, and that's it! I've commited Visual Studio project file for building gstreamermm project, and also property sheet (.props), which contains all necessary dependencies for gstreamermm-based projects.
I've tested it in Debug/Release configuration, both platforms (Win32 and x64), and have used Visual Studio 2010 (project file probably can be upgraded to VS2013, but I didn't test it yet).
You can find it in the nearest gstreamermm release (currently I'm working on 1.2.4 version, it should be finished in a next month), but I've already prepared unofficial release for you [1].
Go ahead, download, use, and file  a bugs on a bugzilla [2] ;)

If I have a time, I will prepare windows installer for sure, and upload it with official v1.2.4 release. If not, maybe someone else will pick up the gauntlet, and do one-click installer for gstreamermm community - if you'd like to help, just let me know, I'll help you so far as it is possible.

gstreamermm depends on many other libraries, so you need to install gtkmm package (can be found here [3]), or build necessary yourself from sources (that's way which I have chosen).


Monday, August 18, 2014

Week with drones - IMAV 2014 report

Last week I spent far away of my homely hacker zone, I was in Netherlands, and took part in IMAV - International Micro Air Vehicle Conference and Competition [1], as a member of High Flyers - Academic Scientific Association from Silesian University of Technology [2].

Briefly about IMAV
IMAV is an annual competition for UAV aficionado, combined with conference concerning micro air vehicles. Last year, IMAV was organized by ENAC University, from Toulouse [3]. That was my first time, when I participated in IMAV, and I really enjoyed. Me and my friends (from High Flyers also) decided to attend in both the conference and the competition. We did our best, and we won two silver medals in indoor category (autonomy and operation) [4]!!! The atmosphere on the conference was amazing - everyone was very friendly, you could talk about UAV technologies with people from another universities, countries, and even continents. Moreover, organizers invited us to Airbus factory [5], and we could see, how one of the biggest airliners are made.
HF-4X - our last year platform, and awards :)

IMAV 2014
This year, IMAV was organized by University of Technology in Delf, in Netherlands. Organizers made every effort, to prepare conference as good as in France. And this is it! Guys from Delf have organized conference very well.

This year there was no division into outdoor and indoor category. Whole mission, which should be done, took place in both inside the buildings, and also outside. So participants have to prepare platform, which can fly in various conditions, or just made two kind of platforms - for indoor and outdoor operations. Most of (or maybe all) teams have chosen later variant (including High Flyers).
Competition has took place in the village of Oostdorp. Participants had to make a map of Oostdorp, find obstacles on the roads, read numbers on the buildings, precisely scan one of them, observe digits shown on seven-segment display. And the most important - every mission should be performed fully autonomous. If an operator interfere during a mission, a team will get less points. Competition rules might be found on an official IMAV 2014 website [6].

Me and my team on IMAV 2014
High Flyers has participated with three platforms. Two of them was designed for outdoor conditions, and one - for flying inside a building. Unfortunately, one of outdoor platforms has broken down on competition's day (one day earlier, during a tests, everything has worked fine...:), and we used only two platforms. Even so, we have won very good, 4th place. Gold medal for team from Singapore, silver - Germany, and bronze - for previous organizers - France. Final competition results might be found on the IMAV 2014 website [7].

High Flyers with UAVs before...

...and during competition

Next year - Achen, Germany. I'm looking forward to the rules. If it is possible, I definitely will go there. I think, every one interested in UAV, should be on IMAV, meet people crazy about drones.

IMAV's disadvantages
IMAV was really amazing, but when I backed home, I get to know the only one disadvantage of this conference. I wasn't at home whole week, and I totally forgot about my plants :/ Rosemary looks quite ok, but basil probably can't be rescued. I'll do my best, but I'm afraid, I have to buy a new one.
Almost dead basil


Wednesday, August 6, 2014

GSoC Report #4

Last stage of my Google Summer of Code 2014 project was to implement mechanism for sending metadata to AcoustID service.
Me and my mentor decided, that metadata will be sent automatically in a background. There is no need for user interaction. Data is sent on adding new audio file to a music library, or on a change metadata of existing track.
AcoustID requires API key for acceptance new metadata, so user has to enter his key into a preferences window.

Metadata Fixer preferences window

Key might be obtained on a acoust id website [1]. While user doesn't enter api key, in plugin window, "AcoustID Settings" button will be displayed.

Plugin info with button referring to an AcoustID preferences window


Sunday, August 3, 2014

gstreamermm 1.0 finally released!

Yeah, after more than year, I finally released gstreamermm [1] 1.0 version - it might be downloaded from the website [2]. I'm really sorry people, who have waited for this release, I had not so much time to do it. Ok. I had time, I didn't it earlier because of my laziness. GUADEC finally motivated me to do this release, and I'm promis, I'll work on gstreamermm project regular, and also official releases will be done as soon as it will be possible.

The most important change, compared to previous version, is supported gstreamer 1.0.
Moreover, possibility for writing your own plugin was added (thanks peper0 [3]).
I also used gtest [4] for writing testcases.

Future plans
  • There is a lot of bugs, which I'll for sure fix in the next stable version. I'm going to write much more test cases (my aim - cover every single function). Feel free to use this library, file a bugs and send patches on a bugzilla [5]!
  • I'm also going to wrap new things from 1.2 version, and later also from 1.4 version.
Packaging - HELP!
I'm looking for help with releasing Ubuntu/Fedora packages. I'd never done it, and it would be nice, if somebody help me, or even do it instead of me :)

Sunday, July 13, 2014

GstCreator - introduction

I'd like to introduce to you my OpenSource project - GstCreator [1]. It's some kind of IDE for GStreamer framework [2]. User can rapidly prototype GStreamer pipeline, and (in the future, not implemented yet) generate C or C++ file with code.
However, user can peek buffers, events and queries on every single pad.

Entire project is written in C++. I'm using Qt library [3](maybe in the future it will be replaced by Gtk+ [4], will see). CMake [5] is my build system. I don't use GStreamer directly, but gstreamermm library [6], which is C++ wrapper for GStreamer framework. It's because of two reasons: 
  • don't like mix 2 languages in one project (even if syntax of both languages is very similar)
  • I'm currently maintainer of gstreamermm project, and use this library by myself helps me find and eliminate a lot of bugs.
Development plan
GstCreator is not ready, ah, it's quite long long way to make first release of this application. But I'm of good cheer.
The list of most important features, which should be implemented in the nearest future:
  • linking to a `sometimes` pads
  • improved `ProbesWatcher` module by showing more informations about buffers, events, and queries, and adding possibility for filtering data
  • save & load project
List above shows only the most important tasks, but there is a lot of minor bugs, which should be implemented ASAP. There is a lot ideas in my head, how to improve this tool, made it more useful and powerful ;)
I hope, some of you will enjoy this application, maybe there is also somebody, who'd like to help me in development ;)
I'm also waiting for your ideas connected to this project. Maybe some features are especially useful, but I didn't notice it;)

I will inform you here about my progress, new features ;)

And at the end, short (and easy) quiz: What film is played on a screenshoot shown below?

GstCreator during playing udp stream


Friday, July 11, 2014

Creating SQL database `on the fly` -

Recently I had to write some SQL queries in a quite advanced application (Banshee [1]). I'm not an expert in SQL, so I had to work through trial and error. But recompilation and restart application every time, when I changed something, was very time consuming. Unfortunately, I didn't use my own computer, and I hadn't installed(and I wasn't able to install) any SQLite shell. I started to looking for web interface for SQLite database.
One of the first result in Google was sqlfiddle [2] page. At first sight website looks very simple. And in reality - it is. On the left side you have to write DDL statements, and on the right side, you may use created schema by sending some queries.
For now, service offers many different databases:
  • MySQL (a few versions)
  • Oracle
  • PostgreSQL (a few versions)
  • SQLite
  • MS SQL Server (a few versions)
You may also generate link to your case, and paste id e.g. in some forum or irc channel.
There is also a lot of minor features, which can be use
ful in specified cases:
  • 3 types of displaying results,
  • execution time measurement,
  • query execution plan,
  • different query terminators,
and many others, which I probably missed. I would highly recommend at least try this tool. Maybe you would like it, and will use it in your daily work ;)

sqlfiddle in action