Counter Strike : Global Offensive Source Code
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.

168 lines
4.9 KiB

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