1
0
mirror of https://github.com/stefanocasazza/ULib.git synced 2025-09-28 19:05:55 +08:00
ULib/src/ulib/net/server/plugin/usp/update.usp
2015-03-05 14:52:12 +01:00

118 lines
2.5 KiB
Plaintext

<!--#
TechEmpower Web Framework Benchmarks
Test type 5: Database updates
-->
<!--#declaration
#include "world.h"
#ifndef AS_cpoll_cppsp_DO
static UValue* pvalue;
#endif
static UOrmSession* psql_update;
static UOrmStatement* pstmt1;
static UOrmStatement* pstmt2;
static World* pworld_update;
static UVector<World*>* pvworld_update;
static void usp_init_update()
{
U_TRACE(5, "::usp_init_update()")
pworld_update = U_NEW(World);
pvworld_update = U_NEW(UVector<World*>(500));
#ifndef AS_cpoll_cppsp_DO
pvalue = U_NEW(UValue(ARRAY_VALUE));
#endif
}
static void usp_fork_update()
{
U_TRACE(5, "::usp_fork_update()")
psql_update = U_NEW(UOrmSession(U_CONSTANT_TO_PARAM("hello_world")));
if (psql_update->isReady())
{
pstmt1 = U_NEW(UOrmStatement(*psql_update, U_CONSTANT_TO_PARAM("SELECT randomNumber FROM World WHERE id = ?")));
pstmt2 = U_NEW(UOrmStatement(*psql_update, U_CONSTANT_TO_PARAM("UPDATE World SET randomNumber = ? WHERE id = ?")));
if (pstmt1 == 0 ||
pstmt2 == 0)
{
U_ERROR("usp_fork_update(): we cound't connect to db");
}
pstmt1->use( pworld_update->id);
pstmt1->into(pworld_update->randomNumber);
pstmt2->use( pworld_update->randomNumber, pworld_update->id);
}
}
#ifdef DEBUG
static void usp_end_update()
{
U_TRACE(5, "::usp_end_update()")
if (pstmt1) delete pstmt1;
if (pstmt2) delete pstmt2;
delete psql_update;
delete pvworld_update;
delete pworld_update;
#ifndef AS_cpoll_cppsp_DO
delete pvalue;
#endif
}
#endif
-->
<!--#args
queries;
-->
<!--#header
Content-Type: application/json; charset=UTF-8
-->
<!--#code
int i = 0, num_queries = queries.strtol();
if (num_queries < 1) num_queries = 1;
else if (num_queries > 500) num_queries = 500;
#ifdef AS_cpoll_cppsp_DO
USP_PUTS_CHAR('[');
#endif
while (true)
{
pworld_update->id = u_get_num_random(10000);
pstmt1->execute();
U_INTERNAL_DUMP("pworld_update->randomNumber = %u", pworld_update->randomNumber)
pworld_update->randomNumber = u_get_num_random(10000);
pstmt2->execute();
#ifdef AS_cpoll_cppsp_DO
USP_PRINTF("{\"id\":%u,\"randomNumber\":%u}", pworld_update->id, pworld_update->randomNumber);
#endif
pvworld_update->push_back(U_NEW(World(*pworld_update)));
if (++i == num_queries) break;
#ifdef AS_cpoll_cppsp_DO
USP_PUTS_CHAR(',');
#endif
}
#ifdef AS_cpoll_cppsp_DO
USP_PUTS_CHAR(']');
#else
USP_JSON_stringify(*pvalue, UVector<World*>, *pvworld_update);
#endif
pvworld_update->clear();
-->