You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

decperf.cxx 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. /* Copyright 2015 Pierre Ossman <ossman@cendio.se> for Cendio AB
  2. *
  3. * This is free software; you can redistribute it and/or modify
  4. * it under the terms of the GNU General Public License as published by
  5. * the Free Software Foundation; either version 2 of the License, or
  6. * (at your option) any later version.
  7. *
  8. * This software is distributed in the hope that it will be useful,
  9. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. * GNU General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this software; if not, write to the Free Software
  15. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  16. * USA.
  17. */
  18. /*
  19. * This program reads files produced by TightVNC's/TurboVNC's
  20. * compare-encodings. It is basically a dump of the RFB protocol
  21. * from the server side from the ServerInit message and forward.
  22. * It is assumed that the client is using a bgr888 (LE) pixel
  23. * format.
  24. */
  25. #ifdef HAVE_CONFIG_H
  26. #include <config.h>
  27. #endif
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <math.h>
  31. #include <sys/time.h>
  32. #include <rdr/Exception.h>
  33. #include <rdr/FileInStream.h>
  34. #include <rdr/OutStream.h>
  35. #include <rfb/CConnection.h>
  36. #include <rfb/CMsgReader.h>
  37. #include <rfb/CMsgWriter.h>
  38. #include <rfb/PixelBuffer.h>
  39. #include <rfb/PixelFormat.h>
  40. #include "util.h"
  41. // FIXME: Files are always in this format
  42. static const rfb::PixelFormat filePF(32, 24, false, true, 255, 255, 255, 0, 8, 16);
  43. class DummyOutStream : public rdr::OutStream {
  44. public:
  45. DummyOutStream();
  46. virtual size_t length();
  47. virtual void flush();
  48. private:
  49. virtual void overrun(size_t needed);
  50. int offset;
  51. rdr::U8 buf[131072];
  52. };
  53. class CConn : public rfb::CConnection {
  54. public:
  55. CConn(const char *filename);
  56. ~CConn();
  57. virtual void initDone();
  58. virtual void setPixelFormat(const rfb::PixelFormat& pf);
  59. virtual void setCursor(int, int, const rfb::Point&, const rdr::U8*);
  60. virtual void setCursorPos(const rfb::Point&);
  61. virtual void framebufferUpdateStart();
  62. virtual void framebufferUpdateEnd();
  63. virtual void setColourMapEntries(int, int, rdr::U16*);
  64. virtual void bell();
  65. virtual void serverCutText(const char*);
  66. public:
  67. double cpuTime;
  68. protected:
  69. rdr::FileInStream *in;
  70. DummyOutStream *out;
  71. };
  72. DummyOutStream::DummyOutStream()
  73. {
  74. offset = 0;
  75. ptr = buf;
  76. end = buf + sizeof(buf);
  77. }
  78. size_t DummyOutStream::length()
  79. {
  80. flush();
  81. return offset;
  82. }
  83. void DummyOutStream::flush()
  84. {
  85. offset += ptr - buf;
  86. ptr = buf;
  87. }
  88. void DummyOutStream::overrun(size_t needed)
  89. {
  90. flush();
  91. if (avail() < needed)
  92. throw rdr::Exception("Insufficient dummy output buffer");
  93. }
  94. CConn::CConn(const char *filename)
  95. {
  96. cpuTime = 0.0;
  97. in = new rdr::FileInStream(filename);
  98. out = new DummyOutStream;
  99. setStreams(in, out);
  100. // Need to skip the initial handshake
  101. setState(RFBSTATE_INITIALISATION);
  102. // That also means that the reader and writer weren't setup
  103. setReader(new rfb::CMsgReader(this, in));
  104. setWriter(new rfb::CMsgWriter(&server, out));
  105. }
  106. CConn::~CConn()
  107. {
  108. delete in;
  109. delete out;
  110. }
  111. void CConn::initDone()
  112. {
  113. setFramebuffer(new rfb::ManagedPixelBuffer(filePF,
  114. server.width(),
  115. server.height()));
  116. }
  117. void CConn::setPixelFormat(const rfb::PixelFormat& /*pf*/)
  118. {
  119. // Override format
  120. CConnection::setPixelFormat(filePF);
  121. }
  122. void CConn::setCursor(int, int, const rfb::Point&, const rdr::U8*)
  123. {
  124. }
  125. void CConn::setCursorPos(const rfb::Point&)
  126. {
  127. }
  128. void CConn::framebufferUpdateStart()
  129. {
  130. CConnection::framebufferUpdateStart();
  131. startCpuCounter();
  132. }
  133. void CConn::framebufferUpdateEnd()
  134. {
  135. CConnection::framebufferUpdateEnd();
  136. endCpuCounter();
  137. cpuTime += getCpuCounter();
  138. }
  139. void CConn::setColourMapEntries(int, int, rdr::U16*)
  140. {
  141. }
  142. void CConn::bell()
  143. {
  144. }
  145. void CConn::serverCutText(const char*)
  146. {
  147. }
  148. struct stats
  149. {
  150. double decodeTime;
  151. double realTime;
  152. };
  153. static struct stats runTest(const char *fn)
  154. {
  155. CConn *cc;
  156. struct timeval start, stop;
  157. struct stats s;
  158. gettimeofday(&start, NULL);
  159. try {
  160. cc = new CConn(fn);
  161. } catch (rdr::Exception& e) {
  162. fprintf(stderr, "Failed to open rfb file: %s\n", e.str());
  163. exit(1);
  164. }
  165. try {
  166. while (true)
  167. cc->processMsg();
  168. } catch (rdr::EndOfStream& e) {
  169. } catch (rdr::Exception& e) {
  170. fprintf(stderr, "Failed to run rfb file: %s\n", e.str());
  171. exit(1);
  172. }
  173. gettimeofday(&stop, NULL);
  174. s.decodeTime = cc->cpuTime;
  175. s.realTime = (double)stop.tv_sec - start.tv_sec;
  176. s.realTime += ((double)stop.tv_usec - start.tv_usec)/1000000.0;
  177. delete cc;
  178. return s;
  179. }
  180. static void sort(double *array, int count)
  181. {
  182. bool sorted;
  183. int i;
  184. do {
  185. sorted = true;
  186. for (i = 1;i < count;i++) {
  187. if (array[i-1] > array[i]) {
  188. double d;
  189. d = array[i];
  190. array[i] = array[i-1];
  191. array[i-1] = d;
  192. sorted = false;
  193. }
  194. }
  195. } while (!sorted);
  196. }
  197. static const int runCount = 9;
  198. int main(int argc, char **argv)
  199. {
  200. int i;
  201. struct stats runs[runCount];
  202. double values[runCount], dev[runCount];
  203. double median, meddev;
  204. if (argc != 2) {
  205. printf("Syntax: %s <rfb file>\n", argv[0]);
  206. return 1;
  207. }
  208. // Warmup
  209. runTest(argv[1]);
  210. // Multiple runs to get a good average
  211. for (i = 0;i < runCount;i++)
  212. runs[i] = runTest(argv[1]);
  213. // Calculate median and median deviation for CPU usage
  214. for (i = 0;i < runCount;i++)
  215. values[i] = runs[i].decodeTime;
  216. sort(values, runCount);
  217. median = values[runCount/2];
  218. for (i = 0;i < runCount;i++)
  219. dev[i] = fabs((values[i] - median) / median) * 100;
  220. sort(dev, runCount);
  221. meddev = dev[runCount/2];
  222. printf("CPU time: %g s (+/- %g %%)\n", median, meddev);
  223. // And for CPU core usage
  224. for (i = 0;i < runCount;i++)
  225. values[i] = runs[i].decodeTime / runs[i].realTime;
  226. sort(values, runCount);
  227. median = values[runCount/2];
  228. for (i = 0;i < runCount;i++)
  229. dev[i] = fabs((values[i] - median) / median) * 100;
  230. sort(dev, runCount);
  231. meddev = dev[runCount/2];
  232. printf("Core usage: %g (+/- %g %%)\n", median, meddev);
  233. return 0;
  234. }