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.

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 setDesktopSize(int w, int h);
  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::setDesktopSize(int w, int h)
  70. {
  71. CConnection::setDesktopSize(w, h);
  72. setFramebuffer(new rfb::ManagedPixelBuffer(filePF, cp.width, cp.height));
  73. }
  74. void CConn::setPixelFormat(const rfb::PixelFormat& pf)
  75. {
  76. // Override format
  77. CConnection::setPixelFormat(filePF);
  78. }
  79. void CConn::setCursor(int, int, const rfb::Point&, const rdr::U8*)
  80. {
  81. }
  82. void CConn::framebufferUpdateStart()
  83. {
  84. CConnection::framebufferUpdateStart();
  85. startCpuCounter();
  86. }
  87. void CConn::framebufferUpdateEnd()
  88. {
  89. CConnection::framebufferUpdateEnd();
  90. endCpuCounter();
  91. cpuTime += getCpuCounter();
  92. }
  93. void CConn::setColourMapEntries(int, int, rdr::U16*)
  94. {
  95. }
  96. void CConn::bell()
  97. {
  98. }
  99. void CConn::serverCutText(const char*, rdr::U32)
  100. {
  101. }
  102. struct stats
  103. {
  104. double decodeTime;
  105. double realTime;
  106. };
  107. static struct stats runTest(const char *fn)
  108. {
  109. CConn *cc;
  110. struct timeval start, stop;
  111. struct stats s;
  112. gettimeofday(&start, NULL);
  113. try {
  114. cc = new CConn(fn);
  115. } catch (rdr::Exception& e) {
  116. fprintf(stderr, "Failed to open rfb file: %s\n", e.str());
  117. exit(1);
  118. }
  119. try {
  120. while (true)
  121. cc->processMsg();
  122. } catch (rdr::EndOfStream& e) {
  123. } catch (rdr::Exception& e) {
  124. fprintf(stderr, "Failed to run rfb file: %s\n", e.str());
  125. exit(1);
  126. }
  127. gettimeofday(&stop, NULL);
  128. s.decodeTime = cc->cpuTime;
  129. s.realTime = (double)stop.tv_sec - start.tv_sec;
  130. s.realTime += ((double)stop.tv_usec - start.tv_usec)/1000000.0;
  131. delete cc;
  132. return s;
  133. }
  134. static void sort(double *array, int count)
  135. {
  136. bool sorted;
  137. int i;
  138. do {
  139. sorted = true;
  140. for (i = 1;i < count;i++) {
  141. if (array[i-1] > array[i]) {
  142. double d;
  143. d = array[i];
  144. array[i] = array[i-1];
  145. array[i-1] = d;
  146. sorted = false;
  147. }
  148. }
  149. } while (!sorted);
  150. }
  151. static const int runCount = 9;
  152. int main(int argc, char **argv)
  153. {
  154. int i;
  155. struct stats runs[runCount];
  156. double values[runCount], dev[runCount];
  157. double median, meddev;
  158. if (argc != 2) {
  159. printf("Syntax: %s <rfb file>\n", argv[0]);
  160. return 1;
  161. }
  162. // Warmup
  163. runTest(argv[1]);
  164. // Multiple runs to get a good average
  165. for (i = 0;i < runCount;i++)
  166. runs[i] = runTest(argv[1]);
  167. // Calculate median and median deviation for CPU usage
  168. for (i = 0;i < runCount;i++)
  169. values[i] = runs[i].decodeTime;
  170. sort(values, runCount);
  171. median = values[runCount/2];
  172. for (i = 0;i < runCount;i++)
  173. dev[i] = fabs((values[i] - median) / median) * 100;
  174. sort(dev, runCount);
  175. meddev = dev[runCount/2];
  176. printf("CPU time: %g s (+/- %g %%)\n", median, meddev);
  177. // And for CPU core usage
  178. for (i = 0;i < runCount;i++)
  179. values[i] = runs[i].decodeTime / runs[i].realTime;
  180. sort(values, runCount);
  181. median = values[runCount/2];
  182. for (i = 0;i < runCount;i++)
  183. dev[i] = fabs((values[i] - median) / median) * 100;
  184. sort(dev, runCount);
  185. meddev = dev[runCount/2];
  186. printf("Core usage: %g (+/- %g %%)\n", median, meddev);
  187. return 0;
  188. }