44
55static bool __show_results = false ;
66
7- class mysql_oper
8- {
7+ class mysql_oper {
98public:
10- mysql_oper (acl::db_pool& dbp, unsigned long id)
11- : dbp_(dbp), id_(id)
12- {
13- }
9+ mysql_oper (acl::db_pool& dbp, unsigned long id) : dbp_(dbp), id_(id) {}
1410
15- ~mysql_oper (void )
16- {
17- }
11+ ~mysql_oper (void ) {}
1812
19- void mysql_add (int count)
20- {
21- for (int i = 0 ; i < count; i++)
22- {
13+ void mysql_add (int count) {
14+ for (int i = 0 ; i < count; i++) {
2315 acl::db_handle* db = dbp_.peek_open ();
24- if (db == NULL )
25- {
16+ if (db == NULL ) {
2617 printf (" peek db connection error i: %d\r\n " , i);
2718 break ;
2819 }
@@ -32,13 +23,10 @@ class mysql_oper
3223 }
3324 }
3425
35- void mysql_get (int count)
36- {
37- for (int i = 0 ; i < count; i++)
38- {
26+ void mysql_get (int count) {
27+ for (int i = 0 ; i < count; i++) {
3928 acl::db_handle* db = dbp_.peek_open ();
40- if (db == NULL )
41- {
29+ if (db == NULL ) {
4230 printf (" peek db connection error i: %d\r\n " , i);
4331 break ;
4432 }
@@ -52,51 +40,52 @@ class mysql_oper
5240 acl::db_pool& dbp_;
5341 unsigned long id_;
5442
55- bool add (acl::db_handle& db, int n)
56- {
43+ bool add (acl::db_handle& db, int n) {
5744 acl::query query;
5845 query.create_sql (" insert into group_tbl(group_name, uvip_tbl,"
5946 " update_date) values(:group, :test, :date)" )
6047 .set_format (" group" , " group:%lu:%d" , id_, n)
6148 .set_parameter (" test" , " test" )
6249 .set_date (" date" , time (NULL ), " %Y-%m-%d" );
63- if (db.exec_update (query) == false )
64- {
50+ if (db.exec_update (query) == false ) {
6551 printf (" exec_update error: %s\r\n " , db.get_error ());
6652 return false ;
6753 }
6854
55+ if (__show_results && n < 10 ) {
56+ printf (" >>>fiber-%d: add ok\r\n " , acl::fiber::self ());
57+ }
6958 return true ;
7059 }
7160
72- bool get (acl::db_handle& db, int n)
73- {
61+ bool get (acl::db_handle& db, int n) {
7462 acl::query query;
7563 query.create_sql (" select * from group_tbl"
7664 " where group_name=:group"
7765 " and uvip_tbl=:test" )
7866 .set_format (" group" , " group:%lu:%d" , id_, n)
7967 .set_format (" test" , " test" );
80- if (db.exec_select (query) == false )
81- {
68+ if (db.exec_select (query) == false ) {
8269 printf (" exec_select error: %s\r\n " , db.get_error ());
8370 return false ;
8471 }
8572
8673 // printf("query=%s\r\n", query.to_string().c_str());
8774
8875 const acl::db_rows* result = db.get_result ();
89- if (__show_results && result)
90- {
76+ if (__show_results && result) {
9177 const std::vector<acl::db_row*>& rows =
9278 result->get_rows ();
93- for (size_t i = 0 ; i < rows.size (); i++)
94- {
95- if (n > 10 )
79+ for (size_t i = 0 ; i < rows.size (); i++) {
80+ if (n > 10 ) {
9681 continue ;
82+ }
83+
84+ printf (" >>>fiber-%d: " , acl::fiber::self ());
9785 const acl::db_row* row = rows[i];
98- for (size_t j = 0 ; j < row->length (); j++)
86+ for (size_t j = 0 ; j < row->length (); j++) {
9987 printf (" %s, " , (*row)[j]);
88+ }
10089 printf (" \r\n " );
10190 }
10291 }
@@ -109,24 +98,23 @@ class mysql_oper
10998// ////////////////////////////////////////////////////////////////////////////
11099// mysql thread class
111100
112- class mysql_thread : public acl ::thread
113- {
101+ class mysql_thread : public acl ::thread {
114102public:
115103 mysql_thread (int id, acl::db_pool& dbp, const char * oper, int count)
116104 : id_(id), dbp_(dbp), oper_(oper), count_(count) {}
117105 ~mysql_thread (void ) {}
118106
119107protected:
120- void * run (void )
121- {
108+ void * run (void ) {
122109 mysql_oper dboper (dbp_, id_);
123110
124- if (oper_.equal (" add" , false ))
111+ if (oper_.equal (" add" , false )) {
125112 dboper.mysql_add (count_);
126- else if (oper_.equal (" get" , false ))
113+ } else if (oper_.equal (" get" , false )) {
127114 dboper.mysql_get (count_);
128- else
115+ } else {
129116 printf (" unknown command: %s\r\n " , oper_.c_str ());
117+ }
130118
131119 return NULL ;
132120 }
@@ -144,35 +132,33 @@ class mysql_thread : public acl::thread
144132static int __max_fibers = 2 ;
145133static int __cur_fibers = 2 ;
146134
147- class mysql_fiber : public acl ::fiber
148- {
135+ class mysql_fiber : public acl ::fiber {
149136public:
150137 mysql_fiber (int id, acl::db_pool& dbp, const char * oper, int count)
151138 : id_(id), dbp_(dbp), oper_(oper), count_(count) {}
152139 ~mysql_fiber (void ) {}
153140
154141protected:
155142 // @override
156- void run (void )
157- {
143+ void run (void ) {
158144// printf("fiber-%d-%d running\r\n", get_id(), acl::fiber::self());
159145 acl_fiber_delay (10 );
160146
161147 mysql_oper dboper (dbp_, id_);
162148
163- if (oper_.equal (" add" , false ))
149+ if (oper_.equal (" add" , false )) {
164150 dboper.mysql_add (count_);
165- else if (oper_.equal (" get" , false ))
151+ } else if (oper_.equal (" get" , false )) {
166152 dboper.mysql_get (count_);
167- else
153+ } else {
168154 printf (" unknown command: %s\r\n " , oper_.c_str ());
155+ }
169156
170157 delete this ;
171158
172159 printf (" ----__cur_fibers: %d----\r\n " , __cur_fibers);
173160
174- if (--__cur_fibers == 0 )
175- {
161+ if (--__cur_fibers == 0 ) {
176162 printf (" All fibers Over\r\n " );
177163 acl::fiber::schedule_stop ();
178164 }
@@ -187,8 +173,7 @@ class mysql_fiber : public acl::fiber
187173
188174// ////////////////////////////////////////////////////////////////////////////
189175
190- static void usage (const char * procname)
191- {
176+ static void usage (const char * procname) {
192177 printf (" usage: %s\r\n "
193178 " -h [help]\r\n "
194179 " -c cocurrent\r\n "
@@ -206,8 +191,7 @@ static void usage(const char* procname)
206191 procname);
207192}
208193
209- int main (int argc, char *argv[])
210- {
194+ int main (int argc, char *argv[]) {
211195 int ch, count = 10 , conn_timeout = 10 , rw_timeout = 10 , cocurrent = 2 ;
212196 acl::string mysql_path (" ../../lib/libmysqlclient_r.so" );
213197 acl::string dbaddr (" 127.0.0.1:3306" ), dbname (" acl_db" );
@@ -217,10 +201,8 @@ int main(int argc, char *argv[])
217201 acl::acl_cpp_init ();
218202 acl::log::stdout_open (true );
219203
220- while ((ch = getopt (argc, argv, " hc:tn:f:s:u:o:p:C:R:d:D" )) > 0 )
221- {
222- switch (ch)
223- {
204+ while ((ch = getopt (argc, argv, " hc:tn:f:s:u:o:p:C:R:d:D" )) > 0 ) {
205+ switch (ch) {
224206 case ' h' :
225207 usage (argv[0 ]);
226208 return 0 ;
@@ -280,12 +262,10 @@ int main(int argc, char *argv[])
280262 // init mysql connections pool
281263 acl::mysql_pool dbpool (dbconf);
282264
283- if (use_threads)
284- {
265+ if (use_threads) {
285266 std::vector<acl::thread*> threads;
286267
287- for (int i = 0 ; i < cocurrent; i++)
288- {
268+ for (int i = 0 ; i < cocurrent; i++) {
289269 acl::thread* thread = new
290270 mysql_thread (i, dbpool, oper, count);
291271
@@ -295,19 +275,15 @@ int main(int argc, char *argv[])
295275 }
296276
297277 for (std::vector<acl::thread*>::iterator it = threads.begin ();
298- it != threads.end (); ++it)
299- {
278+ it != threads.end (); ++it) {
300279 (*it)->wait (NULL );
301280 delete (*it);
302281 }
303- }
304- else
305- {
282+ } else {
306283 __max_fibers = cocurrent;
307284 __cur_fibers = __max_fibers;
308285
309- for (int i = 0 ; i < __max_fibers; i++)
310- {
286+ for (int i = 0 ; i < __max_fibers; i++) {
311287 acl::fiber* f = new mysql_fiber (i, dbpool, oper, count);
312288 f->start ();
313289 }
0 commit comments