1
0
mirror of https://github.com/stefanocasazza/ULib.git synced 2025-10-05 19:18:01 +08:00
ULib/src/ulib/net/server/plugin/usp/equery.usp
stefanocasazza 58edeb9b26 sync
2016-09-20 15:40:52 +02:00

103 lines
2.0 KiB
Plaintext

<!--#
Test type 3: Multiple database queries
TechEmpower Web Framework Benchmarks
-->
<!--#declaration
#include "world.h"
static char buffer[128];
static UElasticSearchClient* es;
#ifndef AS_cpoll_cppsp_DO
static UValue* pvalue;
static UVector<World*>* pvworld_query;
#endif
#define QLEN U_CONSTANT_SIZE("{\"query\":{\"match\":{\"_id\":\"")
static void usp_fork_equery()
{
U_TRACE(5, "::usp_fork_equery()")
U_NEW(UElasticSearchClient, es, UElasticSearchClient);
if (es->connect() == false)
{
U_WARNING("usp_fork_equery(): connection disabled or failed");
return;
}
U_MEMCPY(buffer, "{\"query\":{\"match\":{\"_id\":\"", QLEN);
#ifndef AS_cpoll_cppsp_DO
U_NEW(UValue, pvalue, UValue);
U_NEW(UVector<World*>, pvworld_query, UVector<World*>(500));
#endif
}
#ifdef DEBUG
static void usp_end_equery()
{
U_TRACE(5, "::usp_end_equery()")
delete es;
#ifndef AS_cpoll_cppsp_DO
if (pvalue)
{
delete pvalue;
delete pvworld_query;
}
#endif
}
#endif
-->
<!--#header
Content-Type: application/json
-->
<!--#code
uint32_t id;
UString rnumber;
int i = 0, num_queries = UHTTP::getFormFirstNumericValue(1, 500);
#ifdef AS_cpoll_cppsp_DO
USP_PUTS_CHAR('[');
#endif
while (true)
{
(void) es->sendPOST(U_CONSTANT_TO_PARAM("/tfb/world/_search"), buffer, QLEN+u__snprintf(buffer+QLEN, 128, U_CONSTANT_TO_PARAM("%u\"}}}"), id = u_get_num_random(10000-1)));
rnumber.clear();
(void) U_JFIND(es->getContent(), "randomNumber", rnumber);
#ifdef AS_cpoll_cppsp_DO
USP_PRINTF("{\"id\":%u,\"randomNumber\":%v}", id, rnumber.rep);
#else
World* pworld;
U_NEW(World, pworld, World(id, rnumber.strtoul()));
pvworld_query->push_back(pworld);
#endif
if (++i == num_queries) break;
#ifdef AS_cpoll_cppsp_DO
USP_PUTS_CHAR(',');
#endif
rnumber.clear();
}
#ifdef AS_cpoll_cppsp_DO
USP_PUTS_CHAR(']');
#else
USP_JSON_stringify(*pvalue, UVector<World*>, *pvworld_query);
pvalue->clear();
pvworld_query->clear();
#endif
-->