Team Fortress 2 Source Code as on 22/4/2020
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.

169 lines
4.9 KiB

  1. //========= Copyright Valve Corporation, All rights reserved. ============//
  2. //
  3. // Purpose:
  4. //
  5. //=============================================================================//
  6. #include "client_pch.h"
  7. #ifdef IS_WINDOWS_PC
  8. #include "winlite.h"
  9. #include <winsock2.h> // INADDR_ANY defn
  10. #endif
  11. #include "cbenchmark.h"
  12. #include "tier0/vcrmode.h"
  13. #include "filesystem_engine.h"
  14. #include "sys.h"
  15. #include "KeyValues.h"
  16. #include "sv_uploaddata.h"
  17. #include "FindSteamServers.h"
  18. #include "vstdlib/random.h"
  19. #include "cl_steamauth.h"
  20. // memdbgon must be the last include file in a .cpp file!!!
  21. #include "tier0/memdbgon.h"
  22. #define DEFAULT_RESULTS_FOLDER "results"
  23. #define DEFAULT_RESULTS_FILENAME "results.txt"
  24. CBenchmarkResults g_BenchmarkResults;
  25. extern ConVar host_framerate;
  26. extern void GetMaterialSystemConfigForBenchmarkUpload(KeyValues *dataToUpload);
  27. //-----------------------------------------------------------------------------
  28. // Purpose: Constructor
  29. //-----------------------------------------------------------------------------
  30. CBenchmarkResults::CBenchmarkResults()
  31. {
  32. m_bIsTestRunning = false;
  33. m_szFilename[0] = 0;
  34. }
  35. //-----------------------------------------------------------------------------
  36. // Purpose:
  37. //-----------------------------------------------------------------------------
  38. bool CBenchmarkResults::IsBenchmarkRunning()
  39. {
  40. return m_bIsTestRunning;
  41. }
  42. //-----------------------------------------------------------------------------
  43. // Purpose: starts recording data
  44. //-----------------------------------------------------------------------------
  45. void CBenchmarkResults::StartBenchmark( const CCommand &args )
  46. {
  47. const char *pszFilename = DEFAULT_RESULTS_FILENAME;
  48. if ( args.ArgC() > 1 )
  49. {
  50. pszFilename = args[1];
  51. }
  52. // check path first
  53. if ( !COM_IsValidPath( pszFilename ) )
  54. {
  55. ConMsg( "bench_start %s: invalid path.\n", pszFilename );
  56. return;
  57. }
  58. m_bIsTestRunning = true;
  59. SetResultsFilename( pszFilename );
  60. // set any necessary settings
  61. host_framerate.SetValue( (float)(1.0f / host_state.interval_per_tick) );
  62. // get the current frame and time
  63. m_iStartFrame = host_framecount;
  64. m_flStartTime = realtime;
  65. }
  66. //-----------------------------------------------------------------------------
  67. // Purpose: writes out results to file
  68. //-----------------------------------------------------------------------------
  69. void CBenchmarkResults::StopBenchmark()
  70. {
  71. m_bIsTestRunning = false;
  72. // reset
  73. host_framerate.SetValue( 0 );
  74. // print out some stats
  75. int numticks = host_framecount - m_iStartFrame;
  76. float framerate = numticks / ( realtime - m_flStartTime );
  77. Msg( "Average framerate: %.2f\n", framerate );
  78. // work out where to write the file
  79. g_pFileSystem->CreateDirHierarchy( DEFAULT_RESULTS_FOLDER, "MOD" );
  80. char szFilename[256];
  81. Q_snprintf( szFilename, sizeof( szFilename ), "%s\\%s", DEFAULT_RESULTS_FOLDER, m_szFilename );
  82. // write out the data as keyvalues
  83. KeyValues *kv = new KeyValues( "benchmark" );
  84. kv->SetFloat( "framerate", framerate );
  85. kv->SetInt( "build", build_number() );
  86. // get material system info
  87. GetMaterialSystemConfigForBenchmarkUpload( kv );
  88. // save
  89. kv->SaveToFile( g_pFileSystem, szFilename, "MOD" );
  90. kv->deleteThis();
  91. }
  92. //-----------------------------------------------------------------------------
  93. // Purpose: Sets which file the results will be written to
  94. //-----------------------------------------------------------------------------
  95. void CBenchmarkResults::SetResultsFilename( const char *pFilename )
  96. {
  97. Q_strncpy( m_szFilename, pFilename, sizeof( m_szFilename ) );
  98. Q_DefaultExtension( m_szFilename, ".txt", sizeof( m_szFilename ) );
  99. }
  100. //-----------------------------------------------------------------------------
  101. // Purpose: uploads the most recent results to Steam
  102. //-----------------------------------------------------------------------------
  103. void CBenchmarkResults::Upload()
  104. {
  105. #ifndef SWDS
  106. if ( !m_szFilename[0] || !Steam3Client().SteamUtils() )
  107. return;
  108. uint32 cserIP = 0;
  109. uint16 cserPort = 0;
  110. while ( cserIP == 0 )
  111. {
  112. Steam3Client().SteamUtils()->GetCSERIPPort( &cserIP, &cserPort );
  113. if ( !cserIP )
  114. Sys_Sleep( 10 );
  115. }
  116. netadr_t netadr_CserIP( cserIP, cserPort );
  117. // upload
  118. char szFilename[256];
  119. Q_snprintf( szFilename, sizeof( szFilename ), "%s\\%s", DEFAULT_RESULTS_FOLDER, m_szFilename );
  120. KeyValues *kv = new KeyValues( "benchmark" );
  121. if ( kv->LoadFromFile( g_pFileSystem, szFilename, "MOD" ) )
  122. {
  123. // this sends the data to the Steam CSER
  124. UploadData( netadr_CserIP.ToString(), "benchmark", kv );
  125. }
  126. kv->deleteThis();
  127. #endif
  128. }
  129. CON_COMMAND_F( bench_start, "Starts gathering of info. Arguments: filename to write results into", FCVAR_CHEAT )
  130. {
  131. GetBenchResultsMgr()->StartBenchmark( args );
  132. }
  133. CON_COMMAND_F( bench_end, "Ends gathering of info.", FCVAR_CHEAT )
  134. {
  135. GetBenchResultsMgr()->StopBenchmark();
  136. }
  137. CON_COMMAND_F( bench_upload, "Uploads most recent benchmark stats to the Valve servers.", FCVAR_CHEAT )
  138. {
  139. GetBenchResultsMgr()->Upload();
  140. }