123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- // TestApp1.cpp : Defines the entry point for the application.
- //
-
- #ifdef _WIN32
- #include "Win32/stdafx.h"
- #endif
-
- #include <DK.h>
-
-
- class TestApp1 : public DKApplication
- {
- public:
-
- void OnInitialize(void) override
- {
- DKLogD("%s", DKGL_FUNCTION_NAME);
-
- DKTimer timer;
- for (auto format : { DKCompressor::Deflate, DKCompressor::LZ4, DKCompressor::LZ4HC })
- {
- const char* fmt = [](DKCompressor::Method m) {
- switch (m)
- {
- case DKCompressor::Deflate: return "DEFLATE";
- case DKCompressor::LZ4: return "LZ4";
- case DKCompressor::LZ4HC: return "LZ4HC";
- }
- return "Unknown";
- }(format);
-
- auto GetFileSizeStr = [](const DKFile* file)
- {
- size_t s = file->TotalLength();
- if (s > size_t(1) << 40)
- {
- size_t d = size_t(1) << 40;
- double n = static_cast<double>(s / d);
- double f = static_cast<double>(s % d) / static_cast<double>(d);
- return DKString::Format("%.3fTB", n + f);
- }
- if (s > size_t(1) << 30)
- {
- size_t d = size_t(1) << 30;
- double n = static_cast<double>(s / d);
- double f = static_cast<double>(s % d) / static_cast<double>(d);
- return DKString::Format("%.3fGB", n + f);
- }
- if (s > size_t(1) << 20)
- {
- size_t d = size_t(1) << 20;
- double n = static_cast<double>(s / d);
- double f = static_cast<double>(s % d) / static_cast<double>(d);
- return DKString::Format("%.3fMB", n + f);
- }
- if (s > size_t(1) << 10)
- {
- size_t d = size_t(1) << 10;
- double n = static_cast<double>(s / d);
- double f = static_cast<double>(s % d) / static_cast<double>(d);
- return DKString::Format("%.3fKB", n + f);
- }
- return DKString::Format("%dB", s);
- };
-
- DKString filename1 = "C:\\Users\\hong\\desktop\\test\\1";
- DKString filename2 = filename1 + "." + fmt;
- DKString filename3 = filename2 + ".dec";
- DKObject<DKFile> f1 = DKFile::Create(filename1, DKFile::ModeOpenExisting, DKFile::ModeShareRead);
- DKObject<DKFile> f2 = DKFile::Create(filename2, DKFile::ModeOpenNew, DKFile::ModeShareExclusive);
- DKObject<DKFile> f3 = DKFile::Create(filename3, DKFile::ModeOpenNew, DKFile::ModeShareExclusive);
-
- DKLog("--> Compressing %s", fmt);
- DKCompressor comp(format);
-
- bool result;
- timer.Reset();
- // compress
- result = comp.Compress(f1, f2);
- double elapsed = timer.Elapsed();
-
- if (result)
- DKLog("File Compressed (%s): %ls -> %ls (%fsec)", fmt,
- (const wchar_t*)GetFileSizeStr(f1), (const wchar_t*)GetFileSizeStr(f2), elapsed);
- else
- {
- DKLog("File Compression failed");
- continue;
- }
-
- f2->SetCurrentPosition(0);
- timer.Reset();
- // decompress
- result = DKCompressor::Decompress(f2, f3);
- elapsed = timer.Elapsed();
-
- if (result)
- DKLog("File Decompressed (%s): %ls -> %ls (%fsec)", fmt,
- (const wchar_t*)GetFileSizeStr(f2), (const wchar_t*)GetFileSizeStr(f3), elapsed);
- else
- {
- DKLog("File Decompression failed");
- continue;
- }
-
- // generate hash
- for (DKFile* f : { f1.Ptr(), f2.Ptr(), f3.Ptr() })
- {
- DKString path = f->Path();
- f->SetCurrentPosition(0);
- DKHashResultSHA1 sha1;
- if (DKHashSHA1(f, sha1))
- {
- DKLog("SHA1(%ls): %ls", (const wchar_t*)path, (const wchar_t*)sha1.String());
- }
- else
- DKLog("SHA1(%ls) failed!", (const wchar_t*)path);
- }
-
- }
-
- Terminate(0);
- }
- void OnTerminate(void) override
- {
- DKLogD("%s", DKGL_FUNCTION_NAME);
-
- DKLogI("Memory Pool Statistics");
- size_t numBuckets = DKMemoryPoolNumberOfBuckets();
- DKMemoryPoolBucketStatus* buckets = new DKMemoryPoolBucketStatus[numBuckets];
- DKMemoryPoolQueryAllocationStatus(buckets, numBuckets);
- size_t usedBytes = 0;
- for (int i = 0; i < numBuckets; ++i)
- {
- if (buckets[i].totalChunks > 0)
- {
- DKLogI("--> %5lu: %5lu/%5lu, usage: %.1f%%, used: %.1fKB, total: %.1fKB",
- buckets[i].chunkSize,
- buckets[i].usedChunks, buckets[i].totalChunks,
- double(buckets[i].usedChunks) / double(buckets[i].totalChunks) * 100.0,
- double(buckets[i].chunkSize * buckets[i].usedChunks) / 1024.0,
- double(buckets[i].chunkSize * buckets[i].totalChunks) / 1024.0
- );
- usedBytes += buckets[i].chunkSize * buckets[i].usedChunks;
- }
- }
- DKLogI("MemoryPool Usage: %.1fMB / %.1fMB", double(usedBytes) / (1024 * 1024), double(DKMemoryPoolSize()) / (1024 * 1024));
- delete[] buckets;
- }
- };
-
-
- #ifdef _WIN32
- int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
- _In_opt_ HINSTANCE hPrevInstance,
- _In_ LPWSTR lpCmdLine,
- _In_ int nCmdShow)
- #else
- int main(int argc, const char * argv[])
- #endif
- {
- TestApp1 app;
- DKPropertySet::SystemConfig().SetValue("AppDelegate", "AppDelegate");
- DKPropertySet::SystemConfig().SetValue("GraphicsAPI", "Vulkan");
- return app.Run();
- }
|