forked from yeyouqun/xdeltalib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestserver.cpp
223 lines (190 loc) · 5.12 KB
/
testserver.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <iostream>
#include <stdio.h>
#include <time.h>
#include <string>
#include <sys/types.h>
#include <sys/stat.h>
#include <assert.h>
#include <set>
#include <list>
#include <string.h>
#ifdef _WIN32
#include <errno.h>
#include <io.h>
#include <direct.h>
#include <functional>
#include <hash_map>
#else
#include <dirent.h>
#include <memory>
#include <ext/functional>
#if !defined (__CXX_11__)
#include <ext/hash_map>
#else
#include <unordered_map>
#endif
#include <memory.h>
#include <unistd.h>
#define _stricmp strcasecmp
#endif
#include <algorithm>
#include <typeinfo>
#include <math.h>
#include "mytypes.h"
#include "digest.h"
#include "buffer.h"
#include "SimpleSocket.h"
#include "PassiveSocket.h"
#include "rw.h"
#include "tinythread.h"
#include "rollsum.h"
#include "xdeltalib.h"
#include "platform.h"
#include "reconstruct.h"
#include "inplace.h"
#include "xdelta_server.h"
int files_nr = 0;
xdelta::uint64_t total_file_size = 0;
xdelta::uint64_t trans_file_size = 0;
class my_observer : public xdelta::xdelta_observer
{
public:
my_observer () : recv_bytes_ (0)
, send_bytes_ (0)
, files_nr_ (0)
, tfilsize_ (0)
{}
virtual ~my_observer () {}
xdelta::uint64_t recv_bytes_;
xdelta::uint64_t send_bytes_;
int files_nr_;
xdelta::uint64_t tfilsize_;
private:
virtual void start_hash_stream (const std::string & fname, const xdelta::int32_t blk_len)
{
files_nr_++;
}
virtual void end_hash_stream (const xdelta::uint64_t filsize)
{
tfilsize_ += filsize;
}
virtual void end_first_round ()
{
}
virtual void next_round (const xdelta::int32_t blk_len)
{
}
virtual void end_one_round ()
{
}
virtual void on_error (const std::string & errmsg, const int errorno)
{
}
virtual void bytes_send (const xdelta::uint32_t bytes)
{
send_bytes_ += bytes;
}
virtual void bytes_recv (const xdelta::uint32_t bytes)
{
recv_bytes_ += bytes;
}
};
using xdelta::xdelta_server;
using xdelta::f_local_creator;
using xdelta::file_operator;
void test_multiround (const std::string & path)
{
my_observer mo;
f_local_creator localop (path);
file_operator &fop = localop;
xdelta_server multi;
multi.set_multiround_size (5 * 1024 * 1024);
xdelta::uint64_t start = time (0);
multi.run (fop, mo);
xdelta::uint64_t end = time (0);
if (mo.tfilsize_ == 0)
mo.tfilsize_ = mo.send_bytes_ + mo.recv_bytes_;
xdelta::uint64_t average = (xdelta::uint64_t)((mo.send_bytes_ + mo.recv_bytes_ + \
mo.tfilsize_)/(float)(end - start));
printf ("TIME:\t %d seconds\nSEND:\t%lld bytes\nRECV:\t%lld bytes\n"
"TOTAL:\t%lld\nRATIO:\t%.05f\nAVERAGE: %lld per second.\n"
, (int)(end - start), mo.send_bytes_, mo.recv_bytes_
, mo.tfilsize_
, (float)((mo.send_bytes_ + mo.recv_bytes_)/(float)(mo.tfilsize_))
, average);
}
void test_single_round (const std::string & path)
{
my_observer mo;
f_local_creator localop (path);
file_operator &fop = localop;
xdelta_server server;
xdelta::uint64_t start = time (0);
server.run (fop, mo);
xdelta::uint64_t end = time (0);
if (mo.tfilsize_ == 0)
mo.tfilsize_ = mo.send_bytes_ + mo.recv_bytes_;
xdelta::uint64_t average = (xdelta::uint64_t)((mo.send_bytes_ + mo.recv_bytes_ + \
mo.tfilsize_)/(float)(end - start));
printf ("TIME:\t %d seconds\nSEND:\t%lld bytes\nRECV:\t%lld bytes\n"
"TOTAL:\t%lld\nRATIO:\t%.05f\nAVERAGE: %lld per second.\n"
, (int)(end - start), mo.send_bytes_, mo.recv_bytes_
, mo.tfilsize_
, (float)((mo.send_bytes_ + mo.recv_bytes_)/(float)(mo.tfilsize_))
, average);
}
void test_inplace_with_single_round (const std::string & path)
{
my_observer mo;
f_local_creator localop (path);
file_operator &fop = localop;
xdelta_server server;
server.set_inplace ();
xdelta::uint64_t start = time (0);
server.run (fop, mo);
xdelta::uint64_t end = time (0);
if (mo.tfilsize_ == 0)
mo.tfilsize_ = mo.send_bytes_ + mo.recv_bytes_;
xdelta::uint64_t average = (xdelta::uint64_t)((mo.send_bytes_ + mo.recv_bytes_ + \
mo.tfilsize_)/(float)(end - start));
printf ("TIME:\t %d seconds\nSEND:\t%lld bytes\nRECV:\t%lld bytes\n"
"TOTAL:\t%lld\nRATIO:\t%.05f\nAVERAGE: %lld per second.\n"
, (int)(end - start), mo.send_bytes_, mo.recv_bytes_
, mo.tfilsize_
, (float)((mo.send_bytes_ + mo.recv_bytes_)/(float)(mo.tfilsize_))
, average);
}
int main (int argn, char ** argc)
{
if (argn != 3) {
return -1;
}
try {
std::string path = argc[1];
if (_stricmp (argc[2], "m") == 0) {
test_multiround (path);
}
else if (_stricmp (argc[2], "s") == 0) {
test_single_round (path);
}
else if (_stricmp (argc[2], "i") == 0) {
test_inplace_with_single_round (path);
}
else if (_stricmp (argc[2], "a") == 0) {
test_multiround (path);
test_single_round (path);
test_inplace_with_single_round (path);
}
else {
printf ("Error test type.\n");
exit (-1);
}
}
catch (xdelta::xdelta_exception &e) {
printf ("%s\n", e.what ());
system ("pause");
return -1;
}
system ("pause");
return 0;
}