-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBenchmark.cpp
More file actions
181 lines (143 loc) · 4.6 KB
/
Benchmark.cpp
File metadata and controls
181 lines (143 loc) · 4.6 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#ifndef BENCHMARK_H_
#define BENCHMARK_H_
// -------------------------------------------------------
// Creates threads to execute queries on standard input
// -------------------------------------------------------
#include <iostream>
#include <stdlib.h>
#include <pthread.h>
#include <time.h>
#include <string>
#include <queue>
#include "Parser.h"
#include "Settings.h"
#include "Database.h"
#include "FileManager.h"
#include "BufferManager.h"
#include "Query.h"
#include "Schema.h"
#include "Attribute.h"
#include "Operators.h"
#include "Operand.h"
#include "BooleanExpression.h"
#include "DataCreator.h"
using namespace lexer;
// Declare global mutex for thread safety to make multi-threading easier
pthread_mutex_t mutexqueue;
class QueryContainer {
public:
int queryNum;
std::string * query;
};
void * executeQueries(void * ptr)
{
std::queue<QueryContainer*> * queue;
queue = (std::queue<QueryContainer*> *) ptr;
QueryContainer * container;
Parser p;
pthread_mutex_lock (&mutexqueue);
bool empty = queue->empty();
if(!empty) {
container = queue->front();
queue->pop();
}
pthread_mutex_unlock (&mutexqueue);
while(!empty) {
// Profile Query
Query q(0, p.parse(*container->query));
q.profile();
// Check if queue has more elements
pthread_mutex_lock (&mutexqueue);
empty = queue->empty();
delete container->query;
delete container;
if(!empty) {
// Free memory before resetting it
container = queue->front();
queue->pop();
}
pthread_mutex_unlock (&mutexqueue);
}
pthread_exit(0);
}
pthread_t ** initializeThreads(int numThreads, std::queue<QueryContainer*> * queries)
{
pthread_t ** threads = new pthread_t*[numThreads]; // Array of pointers to threads
for(int i = 0; i < numThreads; i++) {
threads[i] = new pthread_t();
int retVal = pthread_create( threads[i], NULL, executeQueries, (void*) queries);
}
return threads;
}
void destructThreads(pthread_t ** threads, int numThreads)
{
for(int i = 0; i < numThreads; i++) {
delete threads[i];
}
delete[] threads;
}
// Initializes the database
void initialize(const char * catalog = "db.xml", const char * files = "config")
{
std::cout << "initializing..."; std::cout.flush();
BufferManager::Initialize();
std::cout << "loading database info..."; std::cout.flush();
Database::Initialize(catalog);
std::cout << "loading database tables..."; std::cout.flush();
FileManager::Initialize(files);
std::cout << "done" << std::endl;
};
// entry point
// arg1: enable partition-materialization (1 or 0)
// arg2: number of threads to initialize
//
int main(int argc, char ** argv)
{
// check arguments
if(argc < 4) {
std::cout << "Must supply 3 arguments: [config file] [partition-materialization] [number of threads]" << std::endl;
exit(1);
}
bool materialization = atoi(argv[2]); // use materialization
int numThreads = atoi(argv[3]); // number of available threads
int availThreads = numThreads; // number of available threads
// Setup database
//DataCreator::CreateDB("createdb",false); // Should not create every time, just load existing files
const char * catalog = "db.xml";
const char * files = argv[1];
initialize(catalog, files);
// Set materialization according to cmd line argument
Settings::get("partition-materialization", materialization);
Database * db = Database::getInstance();
std::queue<QueryContainer*> * queries = new std::queue<QueryContainer*>();
// Read standard input
std::string * query = new std::string(); // Declare on heap for thread access. Thread will delete it
getline(std::cin, *query);
// Loop to load all queries until there are no more to execute
int queryNum = 0;
while(*query != "end" && *query != "execute") {
QueryContainer * container = new QueryContainer(); // Declared on heap, destroyed after execution
container->queryNum = queryNum;
container->query = query;
queries->push(container);
// Load next query from standard input
query = new std::string();
getline(std::cin, *query);
queryNum++;
}
std::cout << "Begining Execution of " << queryNum << " queries..." << std::endl;
// Setup and start threads
pthread_mutex_init(&mutexqueue, NULL);
pthread_t ** threads = initializeThreads(numThreads, queries);
// Wait for all threads to finish
for(int i = 0; i < numThreads; i++) {
pthread_join( *threads[i], NULL);
}
std::cout << "Done!" << std::endl;
// memory cleanup
pthread_mutex_destroy(&mutexqueue);
destructThreads(threads, numThreads);
delete queries;
return 0;
}
#endif