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

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