1baa3858d3f5d128a5c8466b700098109edcad5f2repo sync// BenchCon.cpp
2baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
3baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "StdAfx.h"
4baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
5baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../../../Common/IntToString.h"
6baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../../../Common/MyCom.h"
7baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
8baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#if !defined(_7ZIP_ST) || defined(_WIN32)
9baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../../../Windows/System.h"
10baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#endif
11baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
12baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "../Common/Bench.h"
13baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
14baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "BenchCon.h"
15baa3858d3f5d128a5c8466b700098109edcad5f2repo sync#include "ConsoleClose.h"
16baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
17baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstruct CTotalBenchRes
18baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
19baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 NumIterations;
20baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 Rating;
21baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 Usage;
22baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 RPU;
23baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  void Init() { NumIterations = 0; Rating = 0; Usage = 0; RPU = 0; }
24baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  void Normalize()
25baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
26baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (NumIterations == 0)
27baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      return;
28baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    Rating /= NumIterations;
29baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    Usage /= NumIterations;
30baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    RPU /= NumIterations;
31baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    NumIterations = 1;
32baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
33baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  void SetMid(const CTotalBenchRes &r1, const CTotalBenchRes &r2)
34baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
35baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    Rating = (r1.Rating + r2.Rating) / 2;
36baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    Usage = (r1.Usage + r2.Usage) / 2;
37baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    RPU = (r1.RPU + r2.RPU) / 2;
38baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    NumIterations = (r1.NumIterations + r2.NumIterations) / 2;
39baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
40baa3858d3f5d128a5c8466b700098109edcad5f2repo sync};
41baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
42baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstruct CBenchCallback: public IBenchCallback
43baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
44baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CTotalBenchRes EncodeRes;
45baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CTotalBenchRes DecodeRes;
46baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  FILE *f;
47baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  void Init() { EncodeRes.Init(); DecodeRes.Init(); }
48baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  void Normalize() { EncodeRes.Normalize(); DecodeRes.Normalize(); }
49baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 dictionarySize;
50baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  HRESULT SetEncodeResult(const CBenchInfo &info, bool final);
51baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  HRESULT SetDecodeResult(const CBenchInfo &info, bool final);
52baa3858d3f5d128a5c8466b700098109edcad5f2repo sync};
53baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
54baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void NormalizeVals(UInt64 &v1, UInt64 &v2)
55baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
56baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  while (v1 > 1000000)
57baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
58baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    v1 >>= 1;
59baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    v2 >>= 1;
60baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
61baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
62baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
63baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic UInt64 MyMultDiv64(UInt64 value, UInt64 elapsedTime, UInt64 freq)
64baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
65baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 elTime = elapsedTime;
66baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  NormalizeVals(freq, elTime);
67baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (elTime == 0)
68baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    elTime = 1;
69baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return value * freq / elTime;
70baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
71baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
72baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void PrintNumber(FILE *f, UInt64 value, int size)
73baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
74baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  char s[32];
75baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  ConvertUInt64ToString(value, s);
76baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  fprintf(f, " ");
77baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (int len = (int)strlen(s); len < size; len++)
78baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    fprintf(f, " ");
79baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  fputs(s, f);
80baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
81baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
82baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void PrintRating(FILE *f, UInt64 rating)
83baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
84baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  PrintNumber(f, rating / 1000000, 6);
85baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
86baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
87baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void PrintResults(FILE *f, UInt64 usage, UInt64 rpu, UInt64 rating)
88baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
89baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  PrintNumber(f, (usage + 5000) / 10000, 5);
90baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  PrintRating(f, rpu);
91baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  PrintRating(f, rating);
92baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
93baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
94baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
95baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void PrintResults(FILE *f, const CBenchInfo &info, UInt64 rating, CTotalBenchRes &res)
96baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
97baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 speed = MyMultDiv64(info.UnpackSize, info.GlobalTime, info.GlobalFreq);
98baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  PrintNumber(f, speed / 1024, 7);
99baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 usage = GetUsage(info);
100baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 rpu = GetRatingPerUsage(info, rating);
101baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  PrintResults(f, usage, rpu, rating);
102baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  res.NumIterations++;
103baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  res.RPU += rpu;
104baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  res.Rating += rating;
105baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  res.Usage += usage;
106baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
107baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
108baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void PrintTotals(FILE *f, const CTotalBenchRes &res)
109baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
110baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  fprintf(f, "       ");
111baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  PrintResults(f, res.Usage, res.RPU, res.Rating);
112baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
113baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
114baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
115baa3858d3f5d128a5c8466b700098109edcad5f2repo syncHRESULT CBenchCallback::SetEncodeResult(const CBenchInfo &info, bool final)
116baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
117baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (NConsoleClose::TestBreakSignal())
118baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return E_ABORT;
119baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (final)
120baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
121baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt64 rating = GetCompressRating(dictionarySize, info.GlobalTime, info.GlobalFreq, info.UnpackSize);
122baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    PrintResults(f, info, rating, EncodeRes);
123baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
124baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return S_OK;
125baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
126baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
127baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic const char *kSep = "  | ";
128baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
129baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
130baa3858d3f5d128a5c8466b700098109edcad5f2repo syncHRESULT CBenchCallback::SetDecodeResult(const CBenchInfo &info, bool final)
131baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
132baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (NConsoleClose::TestBreakSignal())
133baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return E_ABORT;
134baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (final)
135baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
136baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    UInt64 rating = GetDecompressRating(info.GlobalTime, info.GlobalFreq, info.UnpackSize, info.PackSize, info.NumIterations);
137baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    fputs(kSep, f);
138baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    CBenchInfo info2 = info;
139baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    info2.UnpackSize *= info2.NumIterations;
140baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    info2.PackSize *= info2.NumIterations;
141baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    info2.NumIterations = 1;
142baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    PrintResults(f, info2, rating, DecodeRes);
143baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
144baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return S_OK;
145baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
146baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
147baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstatic void PrintRequirements(FILE *f, const char *sizeString, UInt64 size, const char *threadsString, UInt32 numThreads)
148baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
149baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  fprintf(f, "\nRAM %s ", sizeString);
150baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  PrintNumber(f, (size >> 20), 5);
151baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  fprintf(f, " MB,  # %s %3d", threadsString, (unsigned int)numThreads);
152baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
153baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
154baa3858d3f5d128a5c8466b700098109edcad5f2repo syncHRESULT LzmaBenchCon(
155baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  DECL_EXTERNAL_CODECS_LOC_VARS
156baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  FILE *f, UInt32 numIterations, UInt32 numThreads, UInt32 dictionary)
157baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
158baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (!CrcInternalTest())
159baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return S_FALSE;
160baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #ifndef _7ZIP_ST
161baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 ramSize = NWindows::NSystem::GetRamSize();  //
162baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 numCPUs = NWindows::NSystem::GetNumberOfProcessors();
163baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  PrintRequirements(f, "size: ", ramSize, "CPU hardware threads:", numCPUs);
164baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (numThreads == (UInt32)-1)
165baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    numThreads = numCPUs;
166baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (numThreads > 1)
167baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    numThreads &= ~1;
168baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (dictionary == (UInt32)-1)
169baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
170baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    int dicSizeLog;
171baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    for (dicSizeLog = 25; dicSizeLog > kBenchMinDicLogSize; dicSizeLog--)
172baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (GetBenchMemoryUsage(numThreads, ((UInt32)1 << dicSizeLog)) + (8 << 20) <= ramSize)
173baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        break;
174baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    dictionary = (1 << dicSizeLog);
175baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
176baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #else
177baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (dictionary == (UInt32)-1)
178baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    dictionary = (1 << 22);
179baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  numThreads = 1;
180baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #endif
181baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
182baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  PrintRequirements(f, "usage:", GetBenchMemoryUsage(numThreads, dictionary), "Benchmark threads:   ", numThreads);
183baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
184baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CBenchCallback callback;
185baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  callback.Init();
186baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  callback.f = f;
187baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
188baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  fprintf(f, "\n\nDict        Compressing          |        Decompressing\n   ");
189baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  int j;
190baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (j = 0; j < 2; j++)
191baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
192baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    fprintf(f, "   Speed Usage    R/U Rating");
193baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (j == 0)
194baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      fputs(kSep, f);
195baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
196baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  fprintf(f, "\n   ");
197baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (j = 0; j < 2; j++)
198baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
199baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    fprintf(f, "    KB/s     %%   MIPS   MIPS");
200baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    if (j == 0)
201baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      fputs(kSep, f);
202baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
203baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  fprintf(f, "\n\n");
204baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (UInt32 i = 0; i < numIterations; i++)
205baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
206baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    const int kStartDicLog = 22;
207baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    int pow = (dictionary < ((UInt32)1 << kStartDicLog)) ? kBenchMinDicLogSize : kStartDicLog;
208baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    while (((UInt32)1 << pow) > dictionary)
209baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      pow--;
210baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    for (; ((UInt32)1 << pow) <= dictionary; pow++)
211baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
212baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      fprintf(f, "%2d:", pow);
213baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      callback.dictionarySize = (UInt32)1 << pow;
214baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      HRESULT res = LzmaBench(
215baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        EXTERNAL_CODECS_LOC_VARS
216baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        numThreads, callback.dictionarySize, &callback);
217baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      fprintf(f, "\n");
218baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      RINOK(res);
219baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
220baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
221baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  callback.Normalize();
222baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  fprintf(f, "----------------------------------------------------------------\nAvr:");
223baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  PrintTotals(f, callback.EncodeRes);
224baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  fprintf(f, "     ");
225baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  PrintTotals(f, callback.DecodeRes);
226baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  fprintf(f, "\nTot:");
227baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CTotalBenchRes midRes;
228baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  midRes.SetMid(callback.EncodeRes, callback.DecodeRes);
229baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  PrintTotals(f, midRes);
230baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  fprintf(f, "\n");
231baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return S_OK;
232baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
233baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
234baa3858d3f5d128a5c8466b700098109edcad5f2repo syncstruct CTempValues
235baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
236baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 *Values;
237baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CTempValues(UInt32 num) { Values = new UInt64[num]; }
238baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  ~CTempValues() { delete []Values; }
239baa3858d3f5d128a5c8466b700098109edcad5f2repo sync};
240baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
241baa3858d3f5d128a5c8466b700098109edcad5f2repo syncHRESULT CrcBenchCon(FILE *f, UInt32 numIterations, UInt32 numThreads, UInt32 dictionary)
242baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
243baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (!CrcInternalTest())
244baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    return S_FALSE;
245baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
246baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #ifndef _7ZIP_ST
247baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 ramSize = NWindows::NSystem::GetRamSize();
248baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt32 numCPUs = NWindows::NSystem::GetNumberOfProcessors();
249baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  PrintRequirements(f, "size: ", ramSize, "CPU hardware threads:", numCPUs);
250baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (numThreads == (UInt32)-1)
251baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    numThreads = numCPUs;
252baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #else
253baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  numThreads = 1;
254baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  #endif
255baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (dictionary == (UInt32)-1)
256baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    dictionary = (1 << 24);
257baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
258baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  CTempValues speedTotals(numThreads);
259baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  fprintf(f, "\n\nSize");
260baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (UInt32 ti = 0; ti < numThreads; ti++)
261baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
262baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    fprintf(f, " %5d", ti + 1);
263baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    speedTotals.Values[ti] = 0;
264baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
265baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  fprintf(f, "\n\n");
266baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
267baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  UInt64 numSteps = 0;
268baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  for (UInt32 i = 0; i < numIterations; i++)
269baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
270baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    for (int pow = 10; pow < 32; pow++)
271baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    {
272baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UInt32 bufSize = (UInt32)1 << pow;
273baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      if (bufSize > dictionary)
274baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        break;
275baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      fprintf(f, "%2d: ", pow);
276baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      UInt64 speed;
277baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      for (UInt32 ti = 0; ti < numThreads; ti++)
278baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      {
279baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        if (NConsoleClose::TestBreakSignal())
280baa3858d3f5d128a5c8466b700098109edcad5f2repo sync          return E_ABORT;
281baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        RINOK(CrcBench(ti + 1, bufSize, speed));
282baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        PrintNumber(f, (speed >> 20), 5);
283baa3858d3f5d128a5c8466b700098109edcad5f2repo sync        speedTotals.Values[ti] += speed;
284baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      }
285baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      fprintf(f, "\n");
286baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      numSteps++;
287baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    }
288baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
289baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  if (numSteps != 0)
290baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  {
291baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    fprintf(f, "\nAvg:");
292baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    for (UInt32 ti = 0; ti < numThreads; ti++)
293baa3858d3f5d128a5c8466b700098109edcad5f2repo sync      PrintNumber(f, ((speedTotals.Values[ti] / numSteps) >> 20), 5);
294baa3858d3f5d128a5c8466b700098109edcad5f2repo sync    fprintf(f, "\n");
295baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  }
296baa3858d3f5d128a5c8466b700098109edcad5f2repo sync  return S_OK;
297baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
298