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
|
// **********************************************************************
//
// Copyright (c) 2003-2005 ZeroC, Inc. All rights reserved.
//
// This copy of Ice is licensed to you under the terms described in the
// ICE_LICENSE file included in this distribution.
//
// **********************************************************************
#include <Ice/Ice.h>
#include <Compression.h>
#include <fstream>
using namespace std;
using namespace Compression;
//
// Implementation of the Ice::Stats interface.
//
class StatsI : public Ice::Stats
{
public:
StatsI() : _total(0) {}
virtual void bytesSent(const string&, Ice::Int sz)
{
_total += sz;
}
virtual void bytesReceived(const string&, Ice::Int)
{
}
void reset()
{
_total = 0;
}
Ice::Int total() const
{
return _total;
}
private:
Ice::Int _total;
};
typedef IceUtil::Handle<StatsI> StatsIPtr;
class Client : public Ice::Application
{
public:
Client() : _stats(new StatsI)
{
}
Ice::StatsPtr stats() const
{
return _stats;
}
virtual int run(int, char*[]);
private:
StatsIPtr _stats;
};
int
main(int argc, char* argv[])
{
Client app;
Ice::InitializationData initData;
initData.properties = Ice::createProperties();
initData.properties->load("config.client");
initData.stats = app.stats();
return app.main(argc, argv, initData);
}
int
Client::run(int argc, char* argv[])
{
if(argc == 1)
{
cerr << "Usage: " << argv[0] << " file" << endl;
return 1;
}
//
// Read the input file into a byte sequence.
//
ifstream in;
in.open(argv[1], ios::binary);
if(!in.good())
{
cerr << argv[0] << ": unable to open file `" << argv[1] << "'" << endl;
return 1;
}
in.seekg(0, ios::end);
ifstream::pos_type len = in.tellg();
in.seekg(0, ios::beg);
ByteSeq source;
source.resize(len);
in.read(reinterpret_cast<char*>(&source[0]), len);
//
// Create our proxies.
//
Ice::PropertiesPtr properties = communicator()->getProperties();
const char* proxyProperty = "Compression.Proxy";
std::string proxy = properties->getProperty(proxyProperty);
if(proxy.empty())
{
cerr << argv[0] << ": property `" << proxyProperty << "' not set" << endl;
return EXIT_FAILURE;
}
Ice::ObjectPrx base = communicator()->stringToProxy(proxy);
ReceiverPrx uncompressed = ReceiverPrx::checkedCast(base->ice_compress(false));
if(!uncompressed)
{
cerr << argv[0] << ": invalid proxy" << endl;
return EXIT_FAILURE;
}
ReceiverPrx compressed = ReceiverPrx::uncheckedCast(base->ice_compress(true));
const int repetitions = 10;
int sz = 200;
while(true)
{
cout << "time for " << sz << " bytes:" << endl;
ByteSeq seq;
seq.resize(sz);
copy(source.begin(), source.begin() + sz, seq.begin());
IceUtil::Time tm;
int i;
//
// Measure uncompressed latency.
//
_stats->reset();
tm = IceUtil::Time::now();
for(i = 0; i < repetitions; ++i)
{
uncompressed->sendData(seq);
}
tm = IceUtil::Time::now() - tm;
cout << " uncompressed = " << tm * 1000 / repetitions << "ms" << flush;
Ice::Int uncompressedTotal = _stats->total();
//
// Measure compressed latency.
//
_stats->reset();
tm = IceUtil::Time::now();
for(i = 0; i < repetitions; ++i)
{
compressed->sendData(seq);
}
tm = IceUtil::Time::now() - tm;
cout << "\tcompressed = " << tm * 1000 / repetitions << "ms" << flush;
Ice::Int compressedTotal = _stats->total();
cout << "\tcompression = " << compressedTotal * 100.0 / uncompressedTotal << "%" << endl;
if(sz == len)
{
break;
}
else
{
//
// ifstream::pos_type has not *= operator.
//
sz = sz * 10;
if(sz > len)
{
sz = static_cast<int>(len);
}
}
}
cout << "done" << endl;
return EXIT_SUCCESS;
}
|