-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbenchmark_train.cc
More file actions
52 lines (49 loc) · 1.26 KB
/
benchmark_train.cc
File metadata and controls
52 lines (49 loc) · 1.26 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
#include <iostream>
#include <queue>
#include <cstdlib>
#include <sys/time.h>
#include "order.h"
#include "train.h"
#include "random.h"
using namespace std;
#define N 65536
#define STATION_N 64
#define SEAT_N 512
#define MILLION 1000000
int main() {
struct timeval start, end;
Train *train = new Train(0, STATION_N, SEAT_N);
Order *orders = new Order[N]();
queue<Order*> success_orders;
for (unsigned int i = 0; i < N; i++) {
orders[i].id = i;
orders[i].from = rand() % STATION_N;
orders[i].to = orders[i].from + rand() % (STATION_N - orders[i].from);
orders[i].seat = -1;
}
Random rnd(301);
gettimeofday(&start, NULL);
for (unsigned int i = 0; i < N; i++) {
if (rnd.OneIn(10)) {
// refund
if (!success_orders.empty()) {
train->refund(success_orders.front());
success_orders.pop();
}
} else if (rnd.OneIn(3)) {
// book
if (train->book(orders + i) == 0) {
success_orders.push(orders + i);
}
} else {
// query
train->query(orders + i);
}
}
gettimeofday(&end, NULL);
double use_time = (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec) / (double)MILLION;
cout << "Process " << N << " operations" << endl;
cout << "Use time: " << use_time << " s" << endl;
cout << "ops: " << (long)(N / use_time) << endl;
return 0;
}