Last active
October 14, 2016 01:44
-
-
Save davidmoreno/5264730 to your computer and use it in GitHub Desktop.
Comments to @dmoreno_cb. http://blog.coralbits.com.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Compile it with: | |
// $ gcc -o bench bench.c -lonion -Wall -O2 -lsqlite3 | |
// $ export ONION_LOG=noinfo # To properly test, you may need some control over logging. Here almost disabled. | |
// $ ./bench | |
// It listens to localhost:8080, known addresses: http://localhost:8080/ , http://localhost:8080/db , http://localhost:8080/db20 | |
// Test it with ab: | |
// $ ab -k -t 10 -c 20 http://localhost:8080/ | |
// It gave me (Intel(R) Core(TM) i7-2677M CPU @ 1.80GHz): | |
// Requests per second: 58288.10 [#/sec] (mean) | |
// Done in response of http://www.techempower.com/blog/2013/03/28/framework-benchmarks/ | |
// Although onion is not a framework. | |
// Copyright (c) 2013, David Moreno | |
// Under BSD license | |
// All rights reserved. | |
// | |
// Redistribution and use in source and binary forms, with or without | |
// modification, are permitted provided that the following conditions are met: | |
// | |
// 1. Redistributions of source code must retain the above copyright notice, this | |
// list of conditions and the following disclaimer. | |
// 2. Redistributions in binary form must reproduce the above copyright notice, | |
// this list of conditions and the following disclaimer in the documentation | |
// and/or other materials provided with the distribution. | |
// | |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND | |
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED | |
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR | |
// ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND | |
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS | |
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
// | |
// The views and conclusions contained in the software and documentation are those | |
// of the authors and should not be interpreted as representing official policies, | |
// either expressed or implied, of the FreeBSD Project. | |
#include <onion/onion.h> | |
#include <onion/handler.h> | |
#include <onion/dict.h> | |
#include <onion/block.h> | |
#include <onion/log.h> | |
#include <string.h> | |
#include <sqlite3.h> | |
#include <assert.h> | |
#include <stdio.h> | |
#include <string.h> | |
/// Gets the dict and converts it to JSON and writes it into the response | |
onion_connection_status return_json(onion_dict *json, onion_request *req, onion_response *res){ | |
onion_block *bl=onion_dict_to_json(json); | |
size_t size=onion_block_size(bl); | |
onion_response_set_header(res, "Content-Type","application/json"); | |
onion_response_set_length(res, size); | |
onion_response_write(res, onion_block_data(bl), size); | |
onion_block_free(bl); | |
return OCS_PROCESSED; | |
} | |
/// Adds the answer to the dict | |
static int return_db_1_callback(onion_dict *d, int argc, char **argv, char **azColName){ | |
int i; | |
for(i=0;i<argc;i++){ | |
onion_dict_add(d, azColName[i], argv[i], OD_DUP_ALL); | |
} | |
return 0; | |
} | |
/// Do one sqlite petition | |
onion_connection_status return_db_1(sqlite3 *db, onion_request *req, onion_response *res){ | |
char query[256]; | |
char *error; | |
onion_dict *d=onion_dict_new(); | |
snprintf(query,sizeof(query), "SELECT * FROM World WHERE id = %d", rand()%100); | |
int rc = sqlite3_exec(db, query, (void*)&return_db_1_callback, d, &error); | |
if (rc<0){ | |
ONION_ERROR("Error on query: %s",error); | |
return OCS_INTERNAL_ERROR; | |
} | |
onion_block *bl=onion_dict_to_json(d); | |
onion_response_set_header(res,"Content-Type","application/json"); | |
int size=onion_block_size(bl); | |
onion_response_set_length(res, size); | |
onion_response_write(res, onion_block_data(bl), size); | |
onion_block_free(bl); | |
onion_dict_free(d); | |
return OCS_PROCESSED; | |
} | |
// Do 20 sqlite petitions. | |
onion_connection_status return_db_20(sqlite3 *db, onion_request *req, onion_response *res){ | |
char query[256]; | |
char *error; | |
onion_dict *d=onion_dict_new(); | |
int i; | |
for (i=0;i<20;i++){ | |
snprintf(query,sizeof(query), "SELECT * FROM World WHERE id = %d", rand()%100); | |
int rc = sqlite3_exec(db, query, (void*)&return_db_1_callback, d, &error); | |
if (rc<0){ | |
ONION_ERROR("Error on query: %s",error); | |
return OCS_INTERNAL_ERROR; | |
} | |
} | |
onion_block *bl=onion_dict_to_json(d); | |
onion_response_set_header(res,"Content-Type","application/json"); | |
int size=onion_block_size(bl); | |
onion_response_set_length(res, size); | |
onion_response_write(res, onion_block_data(bl), size); | |
onion_block_free(bl); | |
onion_dict_free(d); | |
return OCS_PROCESSED; | |
} | |
// Some data needed by the handler | |
struct test_data{ | |
onion_dict *hello; | |
sqlite3 *db[10]; | |
}; | |
#define NCONN 10 | |
/// Multiplexes to the proper handler depending on the path. | |
/// As there is no proper database connection pool, take one connection randomly, and uses it. | |
onion_connection_status muxer(struct test_data *data, onion_request *req, onion_response *res){ | |
const char *path=onion_request_get_path(req); | |
if (strcmp(path, "")==0) | |
return return_json(data->hello, req, res); | |
if (strcmp(path, "db")==0) | |
return return_db_1(data->db[rand()%NCONN], req, res); | |
if (strcmp(path, "db20")==0) | |
return return_db_20(data->db[rand()%NCONN], req, res); | |
return OCS_INTERNAL_ERROR; | |
} | |
/// Creates the onion http server, creates some server data, creates the handler, listens. | |
int main(void){ | |
onion *o=onion_new(O_POOL); | |
struct test_data data; | |
data.hello=onion_dict_new(); | |
int i; | |
for (i=0;i<NCONN;i++){ | |
int rc=sqlite3_open("hello.sqlite", &data.db[i]); | |
if (rc<0){ | |
perror("Cant open hello.sqlite"); | |
return 1; | |
} | |
} | |
onion_dict_add(data.hello,"message","Hello, world", 0); | |
onion_set_root_handler(o, onion_handler_new((void*)&muxer, (void*)&data, NULL)); | |
printf("Listening at http://localhost:8080/\n"); | |
onion_listen(o); | |
onion_dict_free(data.hello); | |
for (i=0;i<NCONN;i++){ | |
sqlite3_close(data.db[i]); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment