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.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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 size_t overrun(size_t itemSize, size_t nItems);
  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. size_t DummyOutStream::overrun(size_t itemSize, size_t nItems)
  85. {
  86. flush();
  87. if (itemSize * nItems > (size_t)(end - ptr))
  88. nItems = (end - ptr) / itemSize;
  89. return nItems;
  90. }
  91. CConn::CConn(const char *filename)
  92. {
  93. cpuTime = 0.0;
  94. in = new rdr::FileInStream(filename);
  95. out = new DummyOutStream;
  96. setStreams(in, out);
  97. // Need to skip the initial handshake
  98. setState(RFBSTATE_INITIALISATION);
  99. // That also means that the reader and writer weren't setup
  100. setReader(new rfb::CMsgReader(this, in));
  101. setWriter(new rfb::CMsgWriter(&server, out));
  102. }
  103. CConn::~CConn()
  104. {
  105. delete in;
  106. delete out;
  107. }
  108. void CConn::initDone()
  109. {
  110. setFramebuffer(new rfb::ManagedPixelBuffer(filePF,
  111. server.width(),
  112. server.height()));
  113. }
  114. void CConn::setPixelFormat(const rfb::PixelFormat& pf)
  115. {
  116. // Override format
  117. CConnection::setPixelFormat(filePF);
  118. }
  119. void CConn::setCursor(int, int, const rfb::Point&, const rdr::U8*)
  120. {
  121. }
  122. void CConn::framebufferUpdateStart()
  123. {
  124. CConnection::framebufferUpdateStart();
  125. startCpuCounter();
  126. }
  127. void CConn::framebufferUpdateEnd()
  128. {
  129. CConnection::framebufferUpdateEnd();
  130. endCpuCounter();
  131. cpuTime += getCpuCounter();
  132. }
  133. void CConn::setColourMapEntries(int, int, rdr::U16*)
  134. {
  135. }
  136. void CConn::bell()
  137. {
  138. }
  139. void CConn::serverCutText(const char*)
  140. {
  141. }
  142. struct stats
  143. {
  144. double decodeTime;
  145. double realTime;
  146. };
  147. static struct stats runTest(const char *fn)
  148. {
  149. CConn *cc;
  150. struct timeval start, stop;
  151. struct stats s;
  152. gettimeofday(&start, NULL);
  153. try {
  154. cc = new CConn(fn);
  155. } catch (rdr::Exception& e) {
  156. fprintf(stderr, "Failed to open rfb file: %s\n", e.str());
  157. exit(1);
  158. }
  159. try {
  160. while (true)
  161. cc->processMsg();
  162. } catch (rdr::EndOfStream& e) {
  163. } catch (rdr::Exception& e) {
  164. fprintf(stderr, "Failed to run rfb file: %s\n", e.str());
  165. exit(1);
  166. }
  167. gettimeofday(&stop, NULL);
  168. s.decodeTime = cc->cpuTime;
  169. s.realTime = (double)stop.tv_sec - start.tv_sec;
  170. s.realTime += ((double)stop.tv_usec - start.tv_usec)/1000000.0;
  171. delete cc;
  172. return s;
  173. }
  174. static void sort(double *array, int count)
  175. {
  176. bool sorted;
  177. int i;
  178. do {
  179. sorted = true;
  180. for (i = 1;i < count;i++) {
  181. if (array[i-1] > array[i]) {
  182. double d;
  183. d = array[i];
  184. array[i] = array[i-1];
  185. array[i-1] = d;
  186. sorted = false;
  187. }
  188. }
  189. } while (!sorted);
  190. }
  191. static const int runCount = 9;
  192. int main(int argc, char **argv)
  193. {
  194. int i;
  195. struct stats runs[runCount];
  196. double values[runCount], dev[runCount];
  197. double median, meddev;
  198. if (argc != 2) {
  199. printf("Syntax: %s <rfb file>\n", argv[0]);
  200. return 1;
  201. }
  202. // Warmup
  203. runTest(argv[1]);
  204. // Multiple runs to get a good average
  205. for (i = 0;i < runCount;i++)
  206. runs[i] = runTest(argv[1]);
  207. // Calculate median and median deviation for CPU usage
  208. for (i = 0;i < runCount;i++)
  209. values[i] = runs[i].decodeTime;
  210. sort(values, runCount);
  211. median = values[runCount/2];
  212. for (i = 0;i < runCount;i++)
  213. dev[i] = fabs((values[i] - median) / median) * 100;
  214. sort(dev, runCount);
  215. meddev = dev[runCount/2];
  216. printf("CPU time: %g s (+/- %g %%)\n", median, meddev);
  217. // And for CPU core usage
  218. for (i = 0;i < runCount;i++)
  219. values[i] = runs[i].decodeTime / runs[i].realTime;
  220. sort(values, runCount);
  221. median = values[runCount/2];
  222. for (i = 0;i < runCount;i++)
  223. dev[i] = fabs((values[i] - median) / median) * 100;
  224. sort(dev, runCount);
  225. meddev = dev[runCount/2];
  226. printf("Core usage: %g (+/- %g %%)\n", median, meddev);
  227. return 0;
  228. }