-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
65 lines (60 loc) · 1.6 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <ctime>
#include "src/DSMgr.cpp"
#include "src/BMgr.cpp"
using namespace std;
int main()
{
BMgr *bmgr = new BMgr(CacheAlgorithm::LRU);
int diskIO = 0;
double BufferHitRate = 0;
clock_t start, end;
string line;
// 创建一个 5万个page(页号从0到49999)的堆文件
bmgr->dsmgr.OpenFile("data/data.dbf");
for (int i = 0; i < 50000; i++)
{
bmgr->FixNewPage();
bmgr->UnfixPage(i);
}
bmgr->WriteDirtys();
bmgr->dsmgr.CloseFile();
delete bmgr;
// 读取trace文件
bmgr = new BMgr(CacheAlgorithm::CLOCK);
bmgr->PrintCacheAlgorithm();
bmgr->dsmgr.OpenFile("data/data.dbf");
ifstream trace("data/data-5w-50w-zipf.txt");
if (!trace.is_open())
{
cerr << "Error: Failed to open trace file." << endl;
return -1;
}
start = clock();
while (getline(trace, line))
{
// 读取trace文件中的每一行 mode,page_id
int mode, page_id;
sscanf(line.c_str(), "%d,%d", &mode, &page_id);
page_id--;
if (mode == 0) // 读
bmgr->FixPage(page_id, 0);
else // 写
bmgr->FixPage(page_id, 1);
bmgr->UnfixPage(page_id);
}
bmgr->WriteDirtys();
bmgr->dsmgr.CloseFile();
delete bmgr;
trace.close();
end = clock();
bmgr->dsmgr.GetDiskIO();
cout << "BufferHitRate: " << bmgr->GetBufferHitRate() << endl;
cout << "Time: " << (double)(end - start) / CLOCKS_PER_SEC << "s" << endl;
return 0;
}