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 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286
  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. #include <stdio.h>
  26. #include <stdlib.h>
  27. #include <math.h>
  28. #include <sys/time.h>
  29. #include <rdr/Exception.h>
  30. #include <rdr/FileInStream.h>
  31. #include <rdr/OutStream.h>
  32. #include <rfb/CConnection.h>
  33. #include <rfb/CMsgReader.h>
  34. #include <rfb/CMsgWriter.h>
  35. #include <rfb/PixelBuffer.h>
  36. #include <rfb/PixelFormat.h>
  37. #include "util.h"
  38. // FIXME: Files are always in this format
  39. static const rfb::PixelFormat filePF(32, 24, false, true, 255, 255, 255, 0, 8, 16);
  40. class DummyOutStream : public rdr::OutStream {
  41. public:
  42. DummyOutStream();
  43. virtual size_t length();
  44. virtual void flush();
  45. private:
  46. virtual void overrun(size_t needed);
  47. int offset;
  48. rdr::U8 buf[131072];
  49. };
  50. class CConn : public rfb::CConnection {
  51. public:
  52. CConn(const char *filename);
  53. ~CConn();
  54. virtual void initDone();
  55. virtual void setPixelFormat(const rfb::PixelFormat& pf);
  56. virtual void setCursor(int, int, const rfb::Point&, const rdr::U8*);
  57. virtual void framebufferUpdateStart();
  58. virtual void framebufferUpdateEnd();
  59. virtual void setColourMapEntries(int, int, rdr::U16*);
  60. virtual void bell();
  61. virtual void serverCutText(const char*);
  62. public:
  63. double cpuTime;
  64. protected:
  65. rdr::FileInStream *in;
  66. DummyOutStream *out;
  67. };
  68. DummyOutStream::DummyOutStream()
  69. {
  70. offset = 0;
  71. ptr = buf;
  72. end = buf + sizeof(buf);
  73. }
  74. size_t DummyOutStream::length()
  75. {
  76. flush();
  77. return offset;
  78. }
  79. void DummyOutStream::flush()
  80. {
  81. offset += ptr - buf;
  82. ptr = buf;
  83. }
  84. void DummyOutStream::overrun(size_t needed)
  85. {
  86. flush();
  87. if (avail() < needed)
  88. throw rdr::Exception("Insufficient dummy output buffer");
  89. }
  90. CConn::CConn(const char *filename)
  91. {
  92. cpuTime = 0.0;
  93. in = new rdr::FileInStream(filename);
  94. out = new DummyOutStream;
  95. setStreams(in, out);
  96. // Need to skip the initial handshake
  97. setState(RFBSTATE_INITIALISATION);
  98. // That also means that the reader and writer weren't setup
  99. setReader(new rfb::CMsgReader(this, in));
  100. setWriter(new rfb::CMsgWriter(&server, out));
  101. }
  102. CConn::~CConn()
  103. {
  104. delete in;
  105. delete out;
  106. }
  107. void CConn::initDone()
  108. {
  109. setFramebuffer(new rfb::ManagedPixelBuffer(filePF,
  110. server.width(),
  111. server.height()));
  112. }
  113. void CConn::setPixelFormat(const rfb::PixelFormat& pf)
  114. {
  115. // Override format
  116. CConnection::setPixelFormat(filePF);
  117. }
  118. void CConn::setCursor(int, int, const rfb::Point&, const rdr::U8*)
  119. {
  120. }
  121. void CConn::framebufferUpdateStart()
  122. {
  123. CConnection::framebufferUpdateStart();
  124. startCpuCounter();
  125. }
  126. void CConn::framebufferUpdateEnd()
  127. {
  128. CConnection::framebufferUpdateEnd();
  129. endCpuCounter();
  130. cpuTime += getCpuCounter();
  131. }
  132. void CConn::setColourMapEntries(int, int, rdr::U16*)
  133. {
  134. }
  135. void CConn::bell()
  136. {
  137. }
  138. void CConn::serverCutText(const char*)
  139. {
  140. }
  141. struct stats
  142. {
  143. double decodeTime;
  144. double realTime;
  145. };
  146. static struct stats runTest(const char *fn)
  147. {
  148. CConn *cc;
  149. struct timeval start, stop;
  150. struct stats s;
  151. gettimeofday(&start, NULL);
  152. try {
  153. cc = new CConn(fn);
  154. } catch (rdr::Exception& e) {
  155. fprintf(stderr, "Failed to open rfb file: %s\n", e.str());
  156. exit(1);
  157. }
  158. try {
  159. while (true)
  160. cc->processMsg();
  161. } catch (rdr::EndOfStream& e) {
  162. } catch (rdr::Exception& e) {
  163. fprintf(stderr, "Failed to run rfb file: %s\n", e.str());
  164. exit(1);
  165. }
  166. gettimeofday(&stop, NULL);
  167. s.decodeTime = cc->cpuTime;
  168. s.realTime = (double)stop.tv_sec - start.tv_sec;
  169. s.realTime += ((double)stop.tv_usec - start.tv_usec)/1000000.0;
  170. delete cc;
  171. return s;
  172. }
  173. static void sort(double *array, int count)
  174. {
  175. bool sorted;
  176. int i;
  177. do {
  178. sorted = true;
  179. for (i = 1;i < count;i++) {
  180. if (array[i-1] > array[i]) {
  181. double d;
  182. d = array[i];
  183. array[i] = array[i-1];
  184. array[i-1] = d;
  185. sorted = false;
  186. }
  187. }
  188. } while (!sorted);
  189. }
  190. static const int runCount = 9;
  191. int main(int argc, char **argv)
  192. {
  193. int i;
  194. struct stats runs[runCount];
  195. double values[runCount], dev[runCount];
  196. double median, meddev;
  197. if (argc != 2) {
  198. printf("Syntax: %s <rfb file>\n", argv[0]);
  199. return 1;
  200. }
  201. // Warmup
  202. runTest(argv[1]);
  203. // Multiple runs to get a good average
  204. for (i = 0;i < runCount;i++)
  205. runs[i] = runTest(argv[1]);
  206. // Calculate median and median deviation for CPU usage
  207. for (i = 0;i < runCount;i++)
  208. values[i] = runs[i].decodeTime;
  209. sort(values, runCount);
  210. median = values[runCount/2];
  211. for (i = 0;i < runCount;i++)
  212. dev[i] = fabs((values[i] - median) / median) * 100;
  213. sort(dev, runCount);
  214. meddev = dev[runCount/2];
  215. printf("CPU time: %g s (+/- %g %%)\n", median, meddev);
  216. // And for CPU core usage
  217. for (i = 0;i < runCount;i++)
  218. values[i] = runs[i].decodeTime / runs[i].realTime;
  219. sort(values, runCount);
  220. median = values[runCount/2];
  221. for (i = 0;i < runCount;i++)
  222. dev[i] = fabs((values[i] - median) / median) * 100;
  223. sort(dev, runCount);
  224. meddev = dev[runCount/2];
  225. printf("Core usage: %g (+/- %g %%)\n", median, meddev);
  226. return 0;
  227. }