Commit f97be21d19dbab2753f8eb592d538745b3e1ca6e

Authored by Jordan Cheney
Committed by Jordan Cheney
1 parent 36d2607d

A stopwatch and profiler to get time data for specified openbr transforms

Showing 1 changed file with 105 additions and 0 deletions
openbr/plugins/time.cpp 0 → 100644
  1 +#include <openbr/plugins/openbr_internal.h>
  2 +
  3 +namespace br
  4 +{
  5 +
  6 +class StopWatchTransform : public UntrainableTransform
  7 +{
  8 + Q_OBJECT
  9 +
  10 + Q_PROPERTY(br::Transform* child READ get_child WRITE set_child RESET reset_child)
  11 + BR_PROPERTY(br::Transform*, child, NULL)
  12 +
  13 + mutable QMutex watchLock;
  14 +
  15 + void project(const Template &src, Template &dst) const
  16 + {
  17 + QTime watch;
  18 +
  19 + if (child == NULL)
  20 + qFatal("Can't find child transform! Command line syntax is StopWatch(name of transform to profile)");
  21 +
  22 + watchLock.lock();
  23 + watch.start();
  24 +
  25 + child->project(src, dst);
  26 +
  27 + int time = watch.elapsed();
  28 + watchLock.unlock();
  29 +
  30 + dst.file.set("time", QVariant::fromValue(time));
  31 + }
  32 +
  33 + void project(const TemplateList &src, TemplateList &dst) const
  34 + {
  35 + QTime watch;
  36 + foreach (const Template &t, src) {
  37 + watchLock.lock();
  38 + watch.start();
  39 +
  40 + Template u;
  41 + child->project(t, u);
  42 +
  43 + int time = watch.elapsed();
  44 + watchLock.unlock();
  45 +
  46 + u.file.set("time", QVariant::fromValue(time));
  47 + dst << u;
  48 + }
  49 + }
  50 +};
  51 +BR_REGISTER(Transform, StopWatchTransform)
  52 +
  53 +class StopWatchProfiler : public TimeVaryingTransform
  54 +{
  55 + Q_OBJECT
  56 +
  57 + TemplateList buffer;
  58 +
  59 +public:
  60 + StopWatchProfiler() : TimeVaryingTransform(false, false) {}
  61 +
  62 +private:
  63 + void train(const TemplateList &data)
  64 + {
  65 + (void) data;
  66 + }
  67 + void projectUpdate(const Template &src, Template &dst)
  68 + {
  69 + dst = src;
  70 + buffer.append(src);
  71 + }
  72 + void projectUpdate(const TemplateList &src, TemplateList &dst)
  73 + {
  74 + dst = src;
  75 + buffer.append(src);
  76 + }
  77 +
  78 + void finalize(TemplateList &output) {
  79 + printf("\n\nProfiling data....\n\n");
  80 +
  81 + output = buffer;
  82 +
  83 + if (buffer.isEmpty())
  84 + qFatal("Empty buffer! Something must have gone wrong.");
  85 +
  86 + if (!buffer.first().file.contains("time"))
  87 + qFatal("No time attribute in the metadata! Did you forget your StopWatch?");
  88 +
  89 + unsigned long imgs = buffer.length();
  90 + unsigned long totalTime = 0;
  91 +
  92 + foreach(const Template &t, buffer) {
  93 + totalTime += t.file.value("time").toUInt();
  94 + }
  95 +
  96 + printf("Profiled %lu images:\n"
  97 + "\tavg time per image: %f ms\n",
  98 + imgs, (double)totalTime / imgs);
  99 + }
  100 +};
  101 +BR_REGISTER(Transform, StopWatchProfiler)
  102 +
  103 +} //namespace br
  104 +
  105 +#include "time.moc"