No Description

TestApp1.cpp 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // TestApp1.cpp : Defines the entry point for the application.
  2. //
  3. #ifdef _WIN32
  4. #include "Win32/stdafx.h"
  5. #endif
  6. #include <DK.h>
  7. class TestApp1 : public DKApplication
  8. {
  9. public:
  10. void OnInitialize(void) override
  11. {
  12. DKLogD("%s", DKGL_FUNCTION_NAME);
  13. DKTimer timer;
  14. for (auto format : { DKCompressor::Zlib, DKCompressor::Zstd, DKCompressor::ZstdMax, DKCompressor::LZ4, DKCompressor::LZ4HC })
  15. {
  16. const char* fmt = [](DKCompressor::Method m) {
  17. switch (m)
  18. {
  19. case DKCompressor::Zlib: return "Zlib";
  20. case DKCompressor::Zstd: return "Zstd";
  21. case DKCompressor::ZstdMax: return "ZstdMax";
  22. case DKCompressor::LZ4: return "LZ4";
  23. case DKCompressor::LZ4HC: return "LZ4HC";
  24. }
  25. return "Unknown";
  26. }(format);
  27. auto GetFileSizeStr = [](const DKFile* file)
  28. {
  29. size_t s = file->TotalLength();
  30. if (s > size_t(1) << 40)
  31. {
  32. size_t d = size_t(1) << 40;
  33. double n = static_cast<double>(s / d);
  34. double f = static_cast<double>(s % d) / static_cast<double>(d);
  35. return DKString::Format("%.3fTB", n + f);
  36. }
  37. if (s > size_t(1) << 30)
  38. {
  39. size_t d = size_t(1) << 30;
  40. double n = static_cast<double>(s / d);
  41. double f = static_cast<double>(s % d) / static_cast<double>(d);
  42. return DKString::Format("%.3fGB", n + f);
  43. }
  44. if (s > size_t(1) << 20)
  45. {
  46. size_t d = size_t(1) << 20;
  47. double n = static_cast<double>(s / d);
  48. double f = static_cast<double>(s % d) / static_cast<double>(d);
  49. return DKString::Format("%.3fMB", n + f);
  50. }
  51. if (s > size_t(1) << 10)
  52. {
  53. size_t d = size_t(1) << 10;
  54. double n = static_cast<double>(s / d);
  55. double f = static_cast<double>(s % d) / static_cast<double>(d);
  56. return DKString::Format("%.3fKB", n + f);
  57. }
  58. return DKString::Format("%dB", s);
  59. };
  60. DKString filename1 = "C:\\Users\\hong\\desktop\\test\\1";
  61. DKString filename2 = filename1 + "." + fmt;
  62. DKString filename3 = filename2 + ".dec";
  63. DKObject<DKFile> f1 = DKFile::Create(filename1, DKFile::ModeOpenExisting, DKFile::ModeShareRead);
  64. DKObject<DKFile> f2 = DKFile::Create(filename2, DKFile::ModeOpenNew, DKFile::ModeShareExclusive);
  65. DKObject<DKFile> f3 = DKFile::Create(filename3, DKFile::ModeOpenNew, DKFile::ModeShareExclusive);
  66. DKLog("--> Compressing %s", fmt);
  67. DKCompressor comp(format);
  68. bool result;
  69. timer.Reset();
  70. // compress
  71. result = comp.Compress(f1, f2);
  72. double elapsed = timer.Elapsed();
  73. if (result)
  74. DKLog("File Compressed (%s): %ls -> %ls (ratio:%f, %fsec)", fmt,
  75. (const wchar_t*)GetFileSizeStr(f1), (const wchar_t*)GetFileSizeStr(f2),
  76. double(f1->TotalLength()) / double(f2->TotalLength()), elapsed);
  77. else
  78. {
  79. DKLog("File Compression failed");
  80. continue;
  81. }
  82. f2->SetCurrentPosition(0);
  83. timer.Reset();
  84. // decompress
  85. result = DKCompressor::Decompress(f2, f3);
  86. elapsed = timer.Elapsed();
  87. if (result)
  88. DKLog("File Decompressed (%s): %ls -> %ls (%fsec)", fmt,
  89. (const wchar_t*)GetFileSizeStr(f2), (const wchar_t*)GetFileSizeStr(f3), elapsed);
  90. else
  91. {
  92. DKLog("File Decompression failed");
  93. continue;
  94. }
  95. // generate hash
  96. for (DKFile* f : { f1.Ptr(), f2.Ptr(), f3.Ptr() })
  97. {
  98. DKString path = f->Path();
  99. f->SetCurrentPosition(0);
  100. DKHashResultSHA1 sha1;
  101. if (DKHashSHA1(f, sha1))
  102. {
  103. DKLog("SHA1(%ls): %ls", (const wchar_t*)path, (const wchar_t*)sha1.String());
  104. }
  105. else
  106. DKLog("SHA1(%ls) failed!", (const wchar_t*)path);
  107. }
  108. }
  109. Terminate(0);
  110. }
  111. void OnTerminate(void) override
  112. {
  113. DKLogD("%s", DKGL_FUNCTION_NAME);
  114. DKLogI("Memory Pool Statistics");
  115. size_t numBuckets = DKMemoryPoolNumberOfBuckets();
  116. DKMemoryPoolBucketStatus* buckets = new DKMemoryPoolBucketStatus[numBuckets];
  117. DKMemoryPoolQueryAllocationStatus(buckets, numBuckets);
  118. size_t usedBytes = 0;
  119. for (int i = 0; i < numBuckets; ++i)
  120. {
  121. if (buckets[i].totalChunks > 0)
  122. {
  123. DKLogI("--> %5lu: %5lu/%5lu, usage: %.1f%%, used: %.1fKB, total: %.1fKB",
  124. buckets[i].chunkSize,
  125. buckets[i].usedChunks, buckets[i].totalChunks,
  126. double(buckets[i].usedChunks) / double(buckets[i].totalChunks) * 100.0,
  127. double(buckets[i].chunkSize * buckets[i].usedChunks) / 1024.0,
  128. double(buckets[i].chunkSize * buckets[i].totalChunks) / 1024.0
  129. );
  130. usedBytes += buckets[i].chunkSize * buckets[i].usedChunks;
  131. }
  132. }
  133. DKLogI("MemoryPool Usage: %.1fMB / %.1fMB", double(usedBytes) / (1024 * 1024), double(DKMemoryPoolSize()) / (1024 * 1024));
  134. delete[] buckets;
  135. }
  136. };
  137. #ifdef _WIN32
  138. int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
  139. _In_opt_ HINSTANCE hPrevInstance,
  140. _In_ LPWSTR lpCmdLine,
  141. _In_ int nCmdShow)
  142. #else
  143. int main(int argc, const char * argv[])
  144. #endif
  145. {
  146. TestApp1 app;
  147. DKPropertySet::SystemConfig().SetValue("AppDelegate", "AppDelegate");
  148. DKPropertySet::SystemConfig().SetValue("GraphicsAPI", "Vulkan");
  149. return app.Run();
  150. }