Quellcode durchsuchen

cppsp: fixed database test formatting

xaxaxa vor 12 Jahren
Ursprung
Commit
4d6dcc5006
4 geänderte Dateien mit 35 neuen und 26 gelöschten Zeilen
  1. 10 9
      cpoll_cppsp/www/db
  2. 10 8
      cpoll_cppsp/www/db_pg_async
  3. 10 9
      cpoll_cppsp/www/db_pg_threadpool
  4. 5 0
      cpoll_cppsp/www/world.H

+ 10 - 9
cpoll_cppsp/www/db

@@ -5,6 +5,7 @@
 #include <list>
 #include "connectioninfo.H"
 #include "generic_pool.H"
+#include "world.H"
 
 using namespace CP;
 using namespace cppsp;
@@ -73,18 +74,18 @@ extern "C" void initModule() {
 %><%$
 EventFD efd{0,EFD_SEMAPHORE};
 int queries=1;
-int* items;
+world* items;
 myStatement* stmt;
 
 void tpFunc() {
 	//mysql_thread_init();
 	for (int i=0;i<queries;i++){
-		stmt->rn=rand()%10000;
+		items[i].id=stmt->rn=rand()%10000;
 		if(mysql_stmt_execute(stmt->stmt)) throw runtime_error(mysql_stmt_error(stmt->stmt));
 		if(mysql_stmt_fetch(stmt->stmt)==0) {
-			items[i]=stmt->r;
+			items[i].rnd=stmt->r;
 		} else {
-			items[i]=0;
+			items[i].rnd=0;
 		}
 	}
 	efd.sendEvent(1);
@@ -107,7 +108,7 @@ void doInit() override {
 	if(queries>500)queries=500;
 	int i;
 	
-	items=(int*)sp->alloc(sizeof(int)*queries);
+	items=(world*)sp->alloc(sizeof(world)*queries);
 	stmt=stmtPool.get();
 	poll->add(efd);
 	tp.invoke({&mypage::tpFunc,this});
@@ -128,12 +129,12 @@ void waitCB(eventfd_t efdVal) {
 	this->doInit();
 }
 
-%>{ "json": [ <%
+%>[<%
 for (int i=0;i<queries;i++){
-	if(i>0) {%>, <%}
-	%>{ "randomNumber": <%=items[i]%> }<%
+	if(i>0) output.write(',');
+	%>{"id":<%=items[i].id%>,"randomNumber":<%=items[i].rnd%>}<%
 }
 
 response->headers["Content-Type"]="application/json";
 response->headers["Server"]="cppsp/0.2";
-%> ] }
+%>]

+ 10 - 8
cpoll_cppsp/www/db_pg_async

@@ -7,6 +7,7 @@
 #include <list>
 #include "connectioninfo.H"
 #include "generic_pool.H"
+#include "world.H"
 
 using namespace CP;
 using namespace cppsp;
@@ -60,7 +61,7 @@ genericPool<myStatement*,128> stmtPool(&cStatement,&dStatement);
 
 %><%$
 int queries=1;
-int* items;
+world* items;
 int n=0;
 myStatement* stmt;
 //asynchronously load the data in the doInit() function, and defer page rendering until data is available
@@ -72,7 +73,7 @@ void doInit() override {
 	if(queries<1)queries=1;
 	if(queries>500)queries=500;
 	int i;
-	items=(int*)sp->alloc(sizeof(int)*queries);
+	items=(world*)sp->alloc(sizeof(world)*queries);
 	stmt=stmtPool.get();
 	if(!stmt->addedToPoll) {
 		poll->add(stmt->f);
@@ -87,7 +88,8 @@ void beginGetItems() {
 		Page::doInit();
 		return;
 	}
-	stmt->exec(rand()%10000);
+	items[n++].id=rand()%10000;
+	stmt->exec(items[n-1].id);
 	stmt->f.waitForEvent(Events::in,{&mypage::evtIn,this});
 }
 void evtIn(int) {
@@ -101,17 +103,17 @@ void evtIn(int) {
 	if(PQntuples(res)>0)
 		tmp=*(const int*)PQgetvalue(res,0,0);
 	else tmp=0;
-	items[n++]=ntohl(tmp);
+	items[n-1].rnd=ntohl(tmp);
 	PQclear(res);
 	beginGetItems();
 }
 
-%>{ "json": [ <%
+%>[<%
 for (int i=0;i<queries;i++){
-	if(i>0) {%>, <%}
-	%>{ "randomNumber": <%=items[i]%> }<%
+	if(i>0) output.write(',');
+	%>{"id":<%=items[i].id%>,"randomNumber":<%=items[i].rnd%>}<%
 }
 
 response->headers["Content-Type"]="application/json";
 response->headers["Server"]="cppsp/0.2";
-%> ] }
+%>]

+ 10 - 9
cpoll_cppsp/www/db_pg_threadpool

@@ -7,6 +7,7 @@
 #include <list>
 #include "connectioninfo.H"
 #include "generic_pool.H"
+#include "world.H"
 
 using namespace CP;
 using namespace cppsp;
@@ -56,17 +57,17 @@ ThreadPool tp(32);
 %><%$
 EventFD efd{0,EFD_SEMAPHORE};
 int queries=1;
-int* items;
+world* items;
 myStatement* stmt;
 
 void tpFunc() {
 	for (int i=0;i<queries;i++){
 		PGresult* res;
-		if((res=stmt->exec(rand()%10000))==NULL) throw bad_alloc();
+		if((res=stmt->exec(items[i].id=(rand()%10000)))==NULL) throw bad_alloc();
 		if(PQntuples(res)>0) {
-			items[i]=ntohl(*(const int*)PQgetvalue(res,0,0));
+			items[i].rnd=ntohl(*(const int*)PQgetvalue(res,0,0));
 		}
-		else items[i]=0;
+		else items[i].rnd=0;
 		PQclear(res);
 	}
 	efd.sendEvent(1);
@@ -80,7 +81,7 @@ void doInit() override {
 	if(queries<1)queries=1;
 	if(queries>500)queries=500;
 	
-	items=(int*)sp->alloc(sizeof(int)*queries);
+	items=(world*)sp->alloc(sizeof(world)*queries);
 	stmt=stmtPool.get();
 	poll->add(efd);
 	tp.invoke({&mypage::tpFunc,this});
@@ -94,12 +95,12 @@ void waitCB(eventfd_t efdVal) {
 	this->doInit();
 }
 
-%>{ "json": [ <%
+%>[<%
 for (int i=0;i<queries;i++){
-	if(i>0) {%>, <%}
-	%>{ "randomNumber": <%=items[i]%> }<%
+	if(i>0) output.write(',');
+	%>{"id":<%=items[i].id%>,"randomNumber":<%=items[i].rnd%>}<%
 }
 
 response->headers["Content-Type"]="application/json";
 response->headers["Server"]="cppsp/0.2";
-%> ] }
+%>]

+ 5 - 0
cpoll_cppsp/www/world.H

@@ -0,0 +1,5 @@
+class world
+{
+public:
+	int id,rnd;
+};