No Description

TestApp1.cpp 4.6KB

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