Menu

[r2337]: / framework / trunk / tests / pool_test.cpp  Maximize  Restore  History

Download this file

224 lines (188 with data), 5.5 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
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
///////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// See accompanying file COPYING.TXT file for licensing details.
//
///////////////////////////////////////////////////////////////////////////////
#include <cppcms/service.h>
#include <cppcms/application.h>
#include <cppcms/applications_pool.h>
#include <cppcms/http_request.h>
#include <cppcms/http_response.h>
#include <cppcms/http_context.h>
#include <cppcms/url_dispatcher.h>
#include <cppcms/mount_point.h>
#include <booster/aio/deadline_timer.h>
#include <booster/posix_time.h>
#include <cppcms/json.h>
#include <booster/thread.h>
#include <iostream>
#include "client.h"
#include <sstream>
#include <booster/log.h>
booster::thread_specific_ptr<int> g_thread_id;
booster::mutex g_id_lock;
int g_thread_id_counter=1000;
void set_thread_id(int v)
{
g_thread_id.reset(new int(v));
}
int get_thread_id()
{
if(g_thread_id.get()==0) {
booster::unique_lock<booster::mutex> guard(g_id_lock);
int new_id = ++g_thread_id_counter;
set_thread_id(new_id);
}
return *g_thread_id;
}
class counter {
typedef booster::unique_lock<booster::mutex> guard;
counter() :
current(0),
max(0),
total(0)
{
}
public:
static counter *instance(std::string const &name)
{
static const int max = 20;
static int curr;
static counter all[max];
for(int i=0;i<curr;i++)
if(all[i].name==name)
return all + i;
assert(curr < max);
all[curr].name = name;
return all + curr++;
}
void print(std::ostream &out)
{
guard g(lock_);
out<< "name="<<name<<"\n"
"current="<<current<<"\n"
"total="<<total<<"\n"
"max="<<max;
}
std::string name;
int current;
int max;
int total;
int inc() {
guard g(lock_);
int r = ++total;
current++;
if(max < current)
max = current;
return r;
}
void dec()
{
guard g(lock_);
current--;
}
private:
booster::mutex lock_;
};
class unit_test : public cppcms::application {
public:
unit_test(cppcms::service &s,counter *c) :
cppcms::application(s),
c_(c),
id_(0),
original_thread_id_(0)
{
id_ = c_->inc();
original_thread_id_ = get_thread_id();
}
~unit_test()
{
c_->dec();
}
void main(std::string path)
{
double sleep_for = atof(request().get("sleep").c_str());
BOOSTER_DEBUG("cppcms") << "--------- GOT " << path << " for " << sleep_for << " from " << id_ << " in " << get_thread_id();
booster::ptime::sleep(booster::ptime::from_number(sleep_for));
std::ostringstream ss;
ss <<
"url=" << request().script_name() << path << "\n"
"thread_id=" << get_thread_id() << "\n"
"original_thread_id=" << original_thread_id_ << "\n"
"app_id="<<id_;
BOOSTER_DEBUG("cppcms") << "RESPONSE " << path << " from " << id_ << "\n" << ss.str() ;
response().out() << ss.str();
}
private:
counter *c_;
int id_;
int original_thread_id_;
};
class tester : public cppcms::application {
public:
tester(cppcms::service &srv) : cppcms::application(srv) {}
void main(std::string name)
{
if(name=="/stats")
counter::instance(request().get("id"))->print(response().out());
else if(name=="/install") {
app_ = new unit_test(service(),counter::instance("/async/temporary"));
service().applications_pool().mount(app_,cppcms::mount_point("/async","/temporary",0));
}
else if(name=="/uninstall") {
app_ = 0;
}
}
private:
booster::intrusive_ptr<cppcms::application> app_;
};
int main(int argc,char **argv)
{
try {
using cppcms::mount_point;
cppcms::service srv(argc,argv);
set_thread_id(1);
srv.applications_pool().mount(
cppcms::create_pool<unit_test>(counter::instance("/sync")),
mount_point("/sync","",0),
cppcms::app::synchronous);
srv.applications_pool().mount(
cppcms::create_pool<unit_test>(counter::instance("/sync/work")),
mount_point("/sync","/work",0),
cppcms::app::synchronous);
srv.applications_pool().mount(
cppcms::create_pool<unit_test>(counter::instance("/sync/prepopulated")),
mount_point("/sync","/prepopulated",0),
cppcms::app::synchronous | cppcms::app::prepopulated);
srv.applications_pool().mount(
cppcms::create_pool<unit_test>(counter::instance("/sync/tss")),
mount_point("/sync","/tss",0),
cppcms::app::synchronous | cppcms::app::thread_specific);
srv.applications_pool().mount(
cppcms::applications_factory<unit_test>(counter::instance("/sync/legacy")),
mount_point("/sync","/legacy",0));
srv.applications_pool().mount(
cppcms::create_pool<unit_test>(counter::instance("/async")),
mount_point("/async","",0),
cppcms::app::asynchronous);
srv.applications_pool().mount(
cppcms::create_pool<unit_test>(counter::instance("/async/prepopulated")),
mount_point("/async","/prepopulated",0),
cppcms::app::asynchronous);
booster::intrusive_ptr<cppcms::application> app = new unit_test(srv,counter::instance("/async/legacy"));
srv.applications_pool().mount(
app,
mount_point("/async","/legacy",0));
counter::instance("/async/temporary");
srv.applications_pool().mount(cppcms::create_pool<tester>(),mount_point("/test"),cppcms::app::asynchronous);
srv.after_fork(submitter(srv));
srv.run();
}
catch(std::exception const &e) {
std::cerr << e.what() << std::endl;
return EXIT_FAILURE;
}
return run_ok ? EXIT_SUCCESS : EXIT_FAILURE;
}
MongoDB Logo MongoDB