hello.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. // Compile it with:
  2. // $ gcc -o bench bench.c -lonion -Wall -O2 -lsqlite3
  3. // $ export ONION_LOG=noinfo # To properly test, you may need some control over logging. Here almost disabled.
  4. // $ ./bench
  5. // It listens to localhost:8080, known addresses: http://localhost:8080/ , http://localhost:8080/db , http://localhost:8080/db20
  6. // Test it with ab:
  7. // $ ab -k -t 10 -c 20 http://localhost:8080/
  8. // It gave me (Intel(R) Core(TM) i7-2677M CPU @ 1.80GHz):
  9. // Requests per second: 58288.10 [#/sec] (mean)
  10. // Done in response of http://www.techempower.com/blog/2013/03/28/framework-benchmarks/
  11. // Although onion is not a framework.
  12. // Copyright (c) 2013, David Moreno
  13. // Under BSD license
  14. // All rights reserved.
  15. //
  16. // Redistribution and use in source and binary forms, with or without
  17. // modification, are permitted provided that the following conditions are met:
  18. //
  19. // 1. Redistributions of source code must retain the above copyright notice, this
  20. // list of conditions and the following disclaimer.
  21. // 2. Redistributions in binary form must reproduce the above copyright notice,
  22. // this list of conditions and the following disclaimer in the documentation
  23. // and/or other materials provided with the distribution.
  24. //
  25. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  26. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  27. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  28. // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  29. // ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  30. // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  31. // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  32. // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  33. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34. // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. //
  36. // The views and conclusions contained in the software and documentation are those
  37. // of the authors and should not be interpreted as representing official policies,
  38. // either expressed or implied, of the FreeBSD Project.
  39. #include <onion/onion.h>
  40. #include <onion/handler.h>
  41. #include <onion/dict.h>
  42. #include <onion/block.h>
  43. #include <onion/log.h>
  44. #include <string.h>
  45. #include <mysql/mysql.h>
  46. #include <assert.h>
  47. #include <stdio.h>
  48. #include <string.h>
  49. #include <json/json.h>
  50. #include <semaphore.h>
  51. #include <pthread.h>
  52. #include <signal.h>
  53. #include <malloc.h>
  54. /// Gets the dict and converts it to JSON and writes it into the response.
  55. onion_connection_status return_json(onion_dict *json, onion_request *req, onion_response *res){
  56. onion_block *bl=onion_dict_to_json(json);
  57. size_t size=onion_block_size(bl);
  58. onion_response_set_header(res, "Content-Type","application/json");
  59. onion_response_set_length(res, size);
  60. onion_response_write(res, onion_block_data(bl), size);
  61. onion_block_free(bl);
  62. return OCS_PROCESSED;
  63. }
  64. /// Gets the dict and converts it to JSON and writes it into the response.
  65. onion_connection_status return_json_libjson(void *_, onion_request *req, onion_response *res){
  66. json_object *hello=json_object_new_object();
  67. json_object_object_add(hello, "message", json_object_new_string("Hello, World!"));
  68. const char *hello_str=json_object_to_json_string(hello);
  69. int size=strlen(hello_str);
  70. onion_response_set_header(res, "Content-Type","application/json");
  71. onion_response_set_length(res, size);
  72. onion_response_write(res, hello_str, size);
  73. json_object_put(hello);
  74. return OCS_PROCESSED;
  75. }
  76. /// Do one sqlite petition
  77. onion_connection_status return_db(MYSQL *db, onion_request *req, onion_response *res){
  78. char query[256];
  79. char *error;
  80. const char *nqueries_str=onion_request_get_query(req,"queries");
  81. int queries=(nqueries_str) ? atoi(nqueries_str) : 1;
  82. if (queries<=0)
  83. queries=1;
  84. else if (queries>500)
  85. queries=500;
  86. json_object *json=json_object_new_object();
  87. json_object *array=json_object_new_array();
  88. int i;
  89. for (i=0;i<queries;i++){
  90. json_object *obj=json_object_new_object();
  91. snprintf(query,sizeof(query), "SELECT * FROM World WHERE id = %d", 1 + (rand()%10000));
  92. mysql_query(db, query);
  93. MYSQL_RES *sqlres = mysql_store_result(db);
  94. MYSQL_ROW row = mysql_fetch_row(sqlres);
  95. json_object_object_add(obj, "id", json_object_new_int( atoi(row[0]) ));
  96. json_object_object_add(obj, "randomNumber", json_object_new_int( atoi(row[1]) ));
  97. //json_object_array_add(array, obj);
  98. json_object_array_add(array, obj);
  99. mysql_free_result(sqlres);
  100. }
  101. json = array;
  102. //json_object_object_add(json,"json",array);
  103. const char *str=json_object_to_json_string(json);
  104. int size=strlen(str);
  105. onion_response_set_header(res,"Content-Type","application/json");
  106. onion_response_set_length(res, size);
  107. onion_response_write(res, str, size);
  108. json_object_put(json);
  109. return OCS_PROCESSED;
  110. }
  111. onion_connection_status return_one_db(MYSQL *db, onion_request *req, onion_response *res){
  112. char query[256];
  113. char *error;
  114. json_object *json=json_object_new_object();
  115. snprintf(query,sizeof(query), "SELECT * FROM World WHERE id = %d", 1 + (rand()%10000));
  116. mysql_query(db, query);
  117. MYSQL_RES *sqlres = mysql_store_result(db);
  118. MYSQL_ROW row = mysql_fetch_row(sqlres);
  119. json_object_object_add(json, "id", json_object_new_int( atoi(row[0]) ));
  120. json_object_object_add(json, "randomNumber", json_object_new_int( atoi(row[1]) ));
  121. mysql_free_result(sqlres);
  122. //json_object_object_add(json,"json",array);
  123. const char *str=json_object_to_json_string(json);
  124. int size=strlen(str);
  125. onion_response_set_header(res,"Content-Type","application/json");
  126. onion_response_set_length(res, size);
  127. onion_response_write(res, str, size);
  128. json_object_put(json);
  129. return OCS_PROCESSED;
  130. }
  131. onion_connection_status fortunes_html_template(onion_dict *context, onion_request *req, onion_response *res);
  132. typedef struct fortune{
  133. char id[10];
  134. char message[2048];
  135. }fortune_t;
  136. typedef struct fortune_list{
  137. int count;
  138. int size;
  139. fortune_t *list;
  140. }fortune_list_t;
  141. int cmp_fortune(fortune_t *a, fortune_t *b){
  142. return strcmp(a->message, b->message);
  143. }
  144. onion_connection_status return_fortune(MYSQL *db, onion_request *req, onion_response *res){
  145. mysql_query(db, "SELECT id, message FROM Fortune;");
  146. MYSQL_RES *sqlres = mysql_store_result(db);
  147. if (!sqlres)
  148. return OCS_INTERNAL_ERROR;
  149. MYSQL_ROW row;
  150. fortune_list_t fortune_list;
  151. fortune_list.count=0;
  152. fortune_list.size=16;
  153. fortune_list.list=calloc(16,sizeof(fortune_t));
  154. while( (row=mysql_fetch_row(sqlres)) ){
  155. if (fortune_list.count>=fortune_list.size){
  156. fortune_list.size+=fortune_list.size;
  157. fortune_list.list=realloc(fortune_list.list, fortune_list.size * sizeof(fortune_list.size));
  158. }
  159. strncpy(fortune_list.list[fortune_list.count].id,row[0],sizeof(fortune_list.list[fortune_list.count].id));
  160. strncpy(fortune_list.list[fortune_list.count].message,row[1],sizeof(fortune_list.list[fortune_list.count].message));
  161. fortune_list.count++;
  162. }
  163. if (fortune_list.count>=fortune_list.size){
  164. fortune_list.size+=fortune_list.size;
  165. fortune_list.list=realloc(fortune_list.list, fortune_list.size * sizeof(fortune_list.size));
  166. }
  167. strncpy(fortune_list.list[fortune_list.count].id,"0",sizeof(fortune_list.list[fortune_list.count].id));
  168. strncpy(fortune_list.list[fortune_list.count].message,"Additional fortune added at request time.",sizeof(fortune_list.list[fortune_list.count].message));
  169. fortune_list.count++;
  170. qsort(fortune_list.list, fortune_list.count, sizeof(fortune_t), (__compar_fn_t)cmp_fortune);
  171. onion_dict *context=onion_dict_new();
  172. onion_dict_add(context, "title", "Fortunes", 0);
  173. onion_dict *fortunes=onion_dict_new();
  174. int i;
  175. for (i=0;i<fortune_list.count;i++){
  176. char nr[16];
  177. snprintf(nr,sizeof(nr),"%010d",i);
  178. onion_dict *fortune=onion_dict_new();
  179. onion_dict_add(fortune, "id", fortune_list.list[i].id, 0);
  180. onion_dict_add(fortune, "message", fortune_list.list[i].message, 0);
  181. onion_dict_add(fortunes, nr, fortune, OD_DUP_KEY|OD_FREE_VALUE|OD_DICT);
  182. }
  183. onion_dict_add(context,"fortunes",fortunes, OD_DICT|OD_FREE_VALUE);
  184. onion_response_set_header(res, "Content-Type","text/html;charset=utf-8");
  185. onion_connection_status ret=fortunes_html_template(context, req, res);
  186. free(fortune_list.list);
  187. return ret;
  188. }
  189. #define NCONN 10
  190. // Some data needed by the handler
  191. struct test_data{
  192. onion_dict *hello;
  193. MYSQL *db[NCONN];
  194. int free_db[NCONN];
  195. pthread_mutex_t mutex;
  196. sem_t sem;
  197. };
  198. MYSQL *get_connection(struct test_data *data){
  199. while( 1 ){
  200. sem_wait(&data->sem);
  201. pthread_mutex_lock(&data->mutex);
  202. int i;
  203. for (i=0;i<NCONN;i++){
  204. if (data->free_db[i]){
  205. data->free_db[i]=0;
  206. pthread_mutex_unlock(&data->mutex);
  207. return data->db[i];
  208. }
  209. }
  210. pthread_mutex_unlock(&data->mutex); // I think it should never get here, but just in case
  211. sem_post(&data->sem);
  212. }
  213. }
  214. void free_connection(struct test_data *data, MYSQL *db){
  215. int i;
  216. for (i=0;i<NCONN;i++){
  217. if (data->db[i]==db){
  218. pthread_mutex_lock(&data->mutex);
  219. data->free_db[i]=1;
  220. pthread_mutex_unlock(&data->mutex);
  221. sem_post(&data->sem);
  222. }
  223. }
  224. }
  225. onion_connection_status return_plaintext(onion_request *req, onion_response *res){
  226. onion_response_set_header(res, "Content-Type","text/plain");
  227. onion_response_write0(res, "Hello, World!");
  228. return OCS_PROCESSED;
  229. }
  230. /// Multiplexes to the proper handler depending on the path.
  231. /// As there is no proper database connection pool, take one connection randomly, and uses it.
  232. onion_connection_status muxer(struct test_data *data, onion_request *req, onion_response *res){
  233. const char *path=onion_request_get_path(req);
  234. if (strcmp(path, "")==0)
  235. return return_json(data->hello, req, res);
  236. if (strcmp(path, "json")==0)
  237. return return_json_libjson(NULL, req, res);
  238. if (strcmp(path, "db")==0){
  239. MYSQL *db=get_connection(data);
  240. int ret=return_one_db(db, req, res);
  241. free_connection(data, db);
  242. return ret;
  243. }
  244. if (strcmp(path, "queries")==0){
  245. MYSQL *db=get_connection(data);
  246. int ret=return_db(db, req, res);
  247. free_connection(data, db);
  248. return ret;
  249. }
  250. if (strcmp(path, "fortune")==0){
  251. MYSQL *db=get_connection(data);
  252. int ret=return_fortune(db, req, res);
  253. free_connection(data, db);
  254. return ret;
  255. }
  256. if (strcmp(path, "plaintext")==0){
  257. return return_plaintext(req, res);
  258. }
  259. return OCS_INTERNAL_ERROR;
  260. }
  261. onion *o=NULL;
  262. static void shutdown_server(int _){
  263. if (o)
  264. onion_listen_stop(o);
  265. }
  266. /// Creates the onion http server, creates some server data, creates the handler, listens.
  267. int main(void){
  268. signal(SIGINT,shutdown_server);
  269. signal(SIGTERM,shutdown_server);
  270. o=onion_new(O_POOL);
  271. struct test_data data;
  272. data.hello=onion_dict_new();
  273. int i;
  274. for (i=0;i<NCONN;i++){
  275. data.db[i]=mysql_init(NULL);
  276. mysql_options(data.db[i], MYSQL_SET_CHARSET_NAME, "utf8");
  277. data.free_db[i]=1;
  278. if (data.db[i]==NULL){
  279. ONION_ERROR("Cant create db connection: %s", mysql_error(data.db[i]));
  280. return 1;
  281. }
  282. if (mysql_real_connect(data.db[i], "tfb-database",
  283. "benchmarkdbuser", "benchmarkdbpass", "hello_world", 0, NULL, 0) == NULL) {
  284. ONION_ERROR("Error %u: %s\n", mysql_errno(data.db[i]), mysql_error(data.db[i]));
  285. return 1;
  286. }
  287. }
  288. pthread_mutex_init(&data.mutex,NULL);
  289. sem_init(&data.sem,0, NCONN);
  290. onion_dict_add(data.hello,"message","Hello, world", 0);
  291. onion_set_root_handler(o, onion_handler_new((void*)&muxer, (void*)&data, NULL));
  292. printf("Listening at http://localhost:8080/\n");
  293. onion_listen(o);
  294. onion_dict_free(data.hello);
  295. for (i=0;i<NCONN;i++){
  296. mysql_close(data.db[i]);
  297. }
  298. onion_free(o);
  299. return 0;
  300. }