hello.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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. json_object *json=json_object_new_object();
  83. json_object *array=json_object_new_array();
  84. int i;
  85. for (i=0;i<queries;i++){
  86. json_object *obj=json_object_new_object();
  87. snprintf(query,sizeof(query), "SELECT * FROM World WHERE id = %d", 1 + (rand()%10000));
  88. mysql_query(db, query);
  89. MYSQL_RES *sqlres = mysql_store_result(db);
  90. MYSQL_ROW row = mysql_fetch_row(sqlres);
  91. json_object_object_add(obj, "id", json_object_new_int( atoi(row[0]) ));
  92. json_object_object_add(obj, "randomNumber", json_object_new_int( atoi(row[1]) ));
  93. //json_object_array_add(array, obj);
  94. if (queries > 1){
  95. json_object_array_add(array, obj);
  96. }
  97. else {
  98. json = obj;
  99. }
  100. mysql_free_result(sqlres);
  101. }
  102. if (queries > 1){
  103. json = array;
  104. }
  105. //json_object_object_add(json,"json",array);
  106. const char *str=json_object_to_json_string(json);
  107. int size=strlen(str);
  108. onion_response_set_header(res,"Content-Type","application/json");
  109. onion_response_set_length(res, size);
  110. onion_response_write(res, str, size);
  111. json_object_put(json);
  112. return OCS_PROCESSED;
  113. }
  114. onion_connection_status fortunes_html_template(onion_dict *context, onion_request *req, onion_response *res);
  115. typedef struct fortune{
  116. char id[10];
  117. char message[2048];
  118. }fortune_t;
  119. typedef struct fortune_list{
  120. int count;
  121. int size;
  122. fortune_t *list;
  123. }fortune_list_t;
  124. int cmp_fortune(fortune_t *a, fortune_t *b){
  125. return strcmp(a->message, b->message);
  126. }
  127. onion_connection_status return_fortune(MYSQL *db, onion_request *req, onion_response *res){
  128. mysql_query(db, "SELECT id, message FROM Fortune;");
  129. MYSQL_RES *sqlres = mysql_store_result(db);
  130. if (!sqlres)
  131. return OCS_INTERNAL_ERROR;
  132. MYSQL_ROW row;
  133. fortune_list_t fortune_list;
  134. fortune_list.count=0;
  135. fortune_list.size=16;
  136. fortune_list.list=calloc(16,sizeof(fortune_t));
  137. while( (row=mysql_fetch_row(sqlres)) ){
  138. if (fortune_list.count>=fortune_list.size){
  139. fortune_list.size+=fortune_list.size;
  140. fortune_list.list=realloc(fortune_list.list, fortune_list.size * sizeof(fortune_list.size));
  141. }
  142. strncpy(fortune_list.list[fortune_list.count].id,row[0],sizeof(fortune_list.list[fortune_list.count].id));
  143. strncpy(fortune_list.list[fortune_list.count].message,row[1],sizeof(fortune_list.list[fortune_list.count].message));
  144. fortune_list.count++;
  145. }
  146. qsort(fortune_list.list, fortune_list.count, sizeof(fortune_t), (__compar_fn_t)cmp_fortune);
  147. onion_dict *context=onion_dict_new();
  148. onion_dict_add(context, "title", "Fortunes", 0);
  149. onion_dict *fortunes=onion_dict_new();
  150. int i;
  151. for (i=0;i<fortune_list.count;i++){
  152. char nr[16];
  153. snprintf(nr,sizeof(nr),"%010d",nr);
  154. onion_dict *fortune=onion_dict_new();
  155. onion_dict_add(fortune, "id", fortune_list.list[i].id, 0);
  156. onion_dict_add(fortune, "message", fortune_list.list[i].message, 0);
  157. onion_dict_add(fortunes, nr, fortune, OD_DUP_KEY|OD_FREE_VALUE|OD_DICT);
  158. }
  159. onion_dict_add(context,"fortunes",fortunes, OD_DICT|OD_FREE_VALUE);
  160. onion_connection_status ret=fortunes_html_template(context, req, res);
  161. free(fortune_list.list);
  162. return ret;
  163. }
  164. #define NCONN 10
  165. // Some data needed by the handler
  166. struct test_data{
  167. onion_dict *hello;
  168. MYSQL *db[NCONN];
  169. int free_db[NCONN];
  170. pthread_mutex_t mutex;
  171. sem_t sem;
  172. };
  173. MYSQL *get_connection(struct test_data *data){
  174. while( 1 ){
  175. sem_wait(&data->sem);
  176. pthread_mutex_lock(&data->mutex);
  177. int i;
  178. for (i=0;i<NCONN;i++){
  179. if (data->free_db[i]){
  180. data->free_db[i]=0;
  181. pthread_mutex_unlock(&data->mutex);
  182. return data->db[i];
  183. }
  184. }
  185. pthread_mutex_unlock(&data->mutex); // I think it should never get here, but just in case
  186. sem_post(&data->sem);
  187. }
  188. }
  189. void free_connection(struct test_data *data, MYSQL *db){
  190. int i;
  191. for (i=0;i<NCONN;i++){
  192. if (data->db[i]==db){
  193. pthread_mutex_lock(&data->mutex);
  194. data->free_db[i]=1;
  195. pthread_mutex_unlock(&data->mutex);
  196. sem_post(&data->sem);
  197. }
  198. }
  199. }
  200. onion_connection_status return_plaintext(onion_request *req, onion_response *res){
  201. onion_response_set_header(res, "Content-Type","text/plain");
  202. onion_response_write0(res, "Hello, World!");
  203. return OCS_PROCESSED;
  204. }
  205. /// Multiplexes to the proper handler depending on the path.
  206. /// As there is no proper database connection pool, take one connection randomly, and uses it.
  207. onion_connection_status muxer(struct test_data *data, onion_request *req, onion_response *res){
  208. const char *path=onion_request_get_path(req);
  209. if (strcmp(path, "")==0)
  210. return return_json(data->hello, req, res);
  211. if (strcmp(path, "json")==0)
  212. return return_json_libjson(NULL, req, res);
  213. if (strcmp(path, "db")==0){
  214. MYSQL *db=get_connection(data);
  215. int ret=return_db(db, req, res);
  216. free_connection(data, db);
  217. return ret;
  218. }
  219. if (strcmp(path, "fortune")==0){
  220. MYSQL *db=get_connection(data);
  221. int ret=return_fortune(db, req, res);
  222. free_connection(data, db);
  223. return ret;
  224. }
  225. if (strcmp(path, "plaintext")==0){
  226. return return_plaintext(req, res);
  227. }
  228. return OCS_INTERNAL_ERROR;
  229. }
  230. onion *o=NULL;
  231. static void shutdown_server(int _){
  232. if (o)
  233. onion_listen_stop(o);
  234. }
  235. /// Creates the onion http server, creates some server data, creates the handler, listens.
  236. int main(void){
  237. signal(SIGINT,shutdown_server);
  238. signal(SIGTERM,shutdown_server);
  239. o=onion_new(O_POOL);
  240. struct test_data data;
  241. data.hello=onion_dict_new();
  242. int i;
  243. for (i=0;i<NCONN;i++){
  244. data.db[i]=mysql_init(NULL);
  245. mysql_options(data.db[i], MYSQL_SET_CHARSET_NAME, "utf8");
  246. data.free_db[i]=1;
  247. if (data.db[i]==NULL){
  248. ONION_ERROR("Cant create db connection: %s", mysql_error(data.db[i]));
  249. return 1;
  250. }
  251. if (mysql_real_connect(data.db[i], "localhost",
  252. "benchmarkdbuser", "benchmarkdbpass", "hello_world", 0, NULL, 0) == NULL) {
  253. ONION_ERROR("Error %u: %s\n", mysql_errno(data.db[i]), mysql_error(data.db[i]));
  254. return 1;
  255. }
  256. }
  257. pthread_mutex_init(&data.mutex,NULL);
  258. sem_init(&data.sem,0, NCONN);
  259. onion_dict_add(data.hello,"message","Hello, world", 0);
  260. onion_set_root_handler(o, onion_handler_new((void*)&muxer, (void*)&data, NULL));
  261. printf("Listening at http://localhost:8080/\n");
  262. onion_listen(o);
  263. onion_dict_free(data.hello);
  264. for (i=0;i<NCONN;i++){
  265. mysql_close(data.db[i]);
  266. }
  267. onion_free(o);
  268. return 0;
  269. }