hello.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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. /// Gets the dict and converts it to JSON and writes it into the response.
  54. onion_connection_status return_json(onion_dict *json, onion_request *req, onion_response *res){
  55. onion_block *bl=onion_dict_to_json(json);
  56. size_t size=onion_block_size(bl);
  57. onion_response_set_header(res, "Content-Type","application/json");
  58. onion_response_set_length(res, size);
  59. onion_response_write(res, onion_block_data(bl), size);
  60. onion_block_free(bl);
  61. return OCS_PROCESSED;
  62. }
  63. /// Gets the dict and converts it to JSON and writes it into the response.
  64. onion_connection_status return_json_libjson(void *_, onion_request *req, onion_response *res){
  65. json_object *hello=json_object_new_object();
  66. json_object_object_add(hello, "message", json_object_new_string("Hello, world"));
  67. const char *hello_str=json_object_to_json_string(hello);
  68. int size=strlen(hello_str);
  69. onion_response_set_header(res, "Content-Type","application/json");
  70. onion_response_set_length(res, size);
  71. onion_response_write(res, hello_str, size);
  72. json_object_put(hello);
  73. return OCS_PROCESSED;
  74. }
  75. /// Do one sqlite petition
  76. onion_connection_status return_db(MYSQL *db, onion_request *req, onion_response *res){
  77. char query[256];
  78. char *error;
  79. const char *nqueries_str=onion_request_get_query(req,"queries");
  80. int queries=(nqueries_str) ? atoi(nqueries_str) : 1;
  81. json_object *json=json_object_new_object();
  82. json_object *array=json_object_new_array();
  83. int i;
  84. for (i=0;i<queries;i++){
  85. json_object *obj=json_object_new_object();
  86. snprintf(query,sizeof(query), "SELECT * FROM World WHERE id = %d", 1 + (rand()%10000));
  87. mysql_query(db, query);
  88. MYSQL_RES *sqlres = mysql_store_result(db);
  89. MYSQL_ROW row = mysql_fetch_row(sqlres);
  90. json_object_object_add(obj, "randomNumber", json_object_new_int( atoi(row[1]) ));
  91. json_object_array_add(array, obj);
  92. mysql_free_result(sqlres);
  93. }
  94. json_object_object_add(json,"json",array);
  95. const char *str=json_object_to_json_string(json);
  96. int size=strlen(str);
  97. onion_response_set_header(res,"Content-Type","application/json");
  98. onion_response_set_length(res, size);
  99. onion_response_write(res, str, size);
  100. json_object_put(json);
  101. return OCS_PROCESSED;
  102. }
  103. #define NCONN 10
  104. // Some data needed by the handler
  105. struct test_data{
  106. onion_dict *hello;
  107. MYSQL *db[NCONN];
  108. int free_db[NCONN];
  109. pthread_mutex_t mutex;
  110. sem_t sem;
  111. };
  112. MYSQL *get_connection(struct test_data *data){
  113. while( 1 ){
  114. sem_wait(&data->sem);
  115. pthread_mutex_lock(&data->mutex);
  116. int i;
  117. for (i=0;i<NCONN;i++){
  118. if (data->free_db[i]){
  119. data->free_db[i]=0;
  120. pthread_mutex_unlock(&data->mutex);
  121. return data->db[i];
  122. }
  123. }
  124. pthread_mutex_unlock(&data->mutex); // I think it should never get here, but just in case
  125. sem_post(&data->sem);
  126. }
  127. }
  128. void free_connection(struct test_data *data, MYSQL *db){
  129. int i;
  130. for (i=0;i<NCONN;i++){
  131. if (data->db[i]==db){
  132. pthread_mutex_lock(&data->mutex);
  133. data->free_db[i]=1;
  134. pthread_mutex_unlock(&data->mutex);
  135. sem_post(&data->sem);
  136. }
  137. }
  138. }
  139. /// Multiplexes to the proper handler depending on the path.
  140. /// As there is no proper database connection pool, take one connection randomly, and uses it.
  141. onion_connection_status muxer(struct test_data *data, onion_request *req, onion_response *res){
  142. const char *path=onion_request_get_path(req);
  143. if (strcmp(path, "")==0)
  144. return return_json(data->hello, req, res);
  145. if (strcmp(path, "json")==0)
  146. return return_json_libjson(NULL, req, res);
  147. if (strcmp(path, "db")==0){
  148. MYSQL *db=get_connection(data);
  149. int ret=return_db(db, req, res);
  150. free_connection(data, db);
  151. return ret;
  152. }
  153. return OCS_INTERNAL_ERROR;
  154. }
  155. onion *o=NULL;
  156. static void shutdown_server(int _){
  157. if (o)
  158. onion_listen_stop(o);
  159. }
  160. /// Creates the onion http server, creates some server data, creates the handler, listens.
  161. int main(void){
  162. signal(SIGINT,shutdown_server);
  163. signal(SIGTERM,shutdown_server);
  164. o=onion_new(O_POOL);
  165. struct test_data data;
  166. data.hello=onion_dict_new();
  167. int i;
  168. for (i=0;i<NCONN;i++){
  169. data.db[i]=mysql_init(NULL);
  170. data.free_db[i]=1;
  171. if (data.db[i]==NULL){
  172. ONION_ERROR("Cant create db connection: %s", mysql_error(data.db[i]));
  173. return 1;
  174. }
  175. if (mysql_real_connect(data.db[i], "localhost",
  176. "benchmarkdbuser", "benchmarkdbpass", "hello_world", 0, NULL, 0) == NULL) {
  177. ONION_ERROR("Error %u: %s\n", mysql_errno(data.db[i]), mysql_error(data.db[i]));
  178. return 1;
  179. }
  180. }
  181. pthread_mutex_init(&data.mutex,NULL);
  182. sem_init(&data.sem,0, NCONN);
  183. onion_dict_add(data.hello,"message","Hello, world", 0);
  184. onion_set_root_handler(o, onion_handler_new((void*)&muxer, (void*)&data, NULL));
  185. printf("Listening at http://localhost:8080/\n");
  186. onion_listen(o);
  187. onion_dict_free(data.hello);
  188. for (i=0;i<NCONN;i++){
  189. mysql_close(data.db[i]);
  190. }
  191. onion_free(o);
  192. return 0;
  193. }