Browse Source

ULib: Implement the cached queries test type (#2830)

* ULib: Implement the cached queries test type

* ULib: Implement the cached queries test type

* ULib: Implement the cached queries test type
stefano casazza 8 years ago
parent
commit
f005711c3a

+ 16 - 0
frameworks/C++/ulib/README.md

@@ -20,6 +20,10 @@ This is the [ULib](http://stefanocasazza.github.io/ULib/) portion of a [benchmar
 * [Variable Query test source (MONGODB)](src/mquery.usp)
 * [Variable Query test source (ELASTICSEARCH)](src/equery.usp)
 
+### Variable Query (caching) Test
+
+* [Variable Query caching test source (SQL)](src/cached_worlds.usp)
+
 ### Fortune Query Test
 
 * [Fortune Query test source (SQL)](src/fortune.usp)
@@ -81,6 +85,18 @@ Content-Type: application/json
 [{"id":6851,"randomNumber":7598},{"id":3968,"randomNumber":7325},{"id":8159,"randomNumber":348},{"id":9560,"randomNumber":7333},{"id":9938,"randomNumber":9080},{"id":1598,"randomNumber":1623},{"id":3280,"randomNumber":8707},{"id":4521,"randomNumber":6063},{"id":8173,"randomNumber":3690},{"id":3648,"randomNumber":8803}]
 ```
 
+[/cached_worlds?queries=10](http://www.techempower.com/benchmarks/#section=caching)
+-------------------
+```
+HTTP/1.1 200 OK
+Date: Thu, 03 Jul 2014 10:14:51 GMT
+Server: ULib 
+Content-Length: 320
+Content-Type: application/json
+
+[{"id":6851,"randomNumber":7598},{"id":3968,"randomNumber":7325},{"id":8159,"randomNumber":348},{"id":9560,"randomNumber":7333},{"id":9938,"randomNumber":9080},{"id":1598,"randomNumber":1623},{"id":3280,"randomNumber":8707},{"id":4521,"randomNumber":6063},{"id":8173,"randomNumber":3690},{"id":3648,"randomNumber":8803}]
+```
+
 [/fortune](http://www.techempower.com/benchmarks/#section=fortune)
 ---------
 ```

+ 2 - 0
frameworks/C++/ulib/benchmark_config.json

@@ -79,6 +79,7 @@
       "query_url": "/query?queries=",
       "fortune_url": "/fortune",
       "update_url": "/update?queries=",
+		"cached_query_url": "/cached_worlds?queries=",
       "port": 8080,
       "approach": "Realistic",
       "classification": "Platform",
@@ -100,6 +101,7 @@
       "query_url": "/query?queries=",
       "fortune_url": "/fortune",
       "update_url": "/update?queries=",
+		"cached_query_url": "/cached_worlds?queries=",
       "port": 8080,
       "approach": "Realistic",
       "classification": "Platform",

+ 1 - 0
frameworks/C++/ulib/source_code

@@ -17,3 +17,4 @@
 ./src/rfortune.usp
 ./src/mfortune.usp
 ./src/plaintext.usp
+./src/cached_worlds.usp

+ 106 - 0
frameworks/C++/ulib/src/cached_worlds.usp

@@ -0,0 +1,106 @@
+<!--#
+Test type 7: Caching
+TechEmpower Web Framework Benchmarks
+-->
+<!--#declaration
+#include "world.h"
+
+static UCache* cache;
+static World* pworld_query;
+
+#ifndef AS_cpoll_cppsp_DO
+static UVector<World*>* pvworld_query;
+#endif
+
+static void usp_init_cached_worlds()
+{
+   U_TRACE(5, "::usp_init_cached_worlds()")
+
+   UOrmSession sql_query(U_CONSTANT_TO_PARAM("hello_world"));
+
+   if (sql_query.isReady() == false)
+      {
+      U_WARNING("usp_init_cached_worlds(): we cound't connect to db");
+
+      return;
+      }
+
+   U_NEW(World, pworld_query, World);
+
+   UOrmStatement stmt_query(sql_query, U_CONSTANT_TO_PARAM("SELECT * FROM World"));
+
+   stmt_query.into(*pworld_query);
+
+   stmt_query.execute();
+
+   // creat and fill the cache
+
+   U_NEW(UCache, cache, UCache);
+
+   (void) cache->open(U_STRING_FROM_CONSTANT("/tmp/ULib_cached_worlds.cache"), 10000 * 64, U_NULLPTR, true);
+
+   do {
+      cache->add(pworld_query->id, pworld_query->randomNumber);
+      }
+   while (stmt_query.nextRow());
+
+#ifndef AS_cpoll_cppsp_DO
+   U_NEW(UVector<World*>, pvworld_query, UVector<World*>(500));
+#endif
+}
+
+#ifdef DEBUG
+static void usp_end_cached_worlds()
+{
+   U_TRACE(5, "::usp_end_cached_worlds()")
+
+   if (cache)
+      {
+      delete cache;
+      delete pworld_query;
+
+#  ifndef AS_cpoll_cppsp_DO
+      delete pvworld_query;
+#  endif
+      }
+}
+#endif
+-->
+<!--#header
+Content-Type: application/json
+-->
+<!--#code
+int i = 0, num_queries = UHTTP::getFormFirstNumericValue(1, 500);
+
+#ifdef AS_cpoll_cppsp_DO
+USP_PUTS_CHAR('[');
+#endif
+
+while (true)
+   {
+   pworld_query->randomNumber = cache->getNumber((pworld_query->id = u_get_num_random(10000-1)));
+
+#ifdef AS_cpoll_cppsp_DO
+   USP_PRINTF("{\"id\":%u,\"randomNumber\":%u}", pworld_query->id, pworld_query->randomNumber);
+#else
+   World* pworld;
+
+   U_NEW(World, pworld, World(*pworld_query));
+
+   pvworld_query->push_back(pworld);
+#endif
+
+   if (++i == num_queries) break;
+
+#ifdef AS_cpoll_cppsp_DO
+   USP_PUTS_CHAR(',');
+#endif
+   }
+
+#ifdef AS_cpoll_cppsp_DO
+USP_PUTS_CHAR(']');
+#else
+USP_OBJ_JSON_stringify(*pvworld_query);
+pvworld_query->clear();
+#endif
+-->

+ 8 - 11
frameworks/C++/ulib/src/db.usp

@@ -15,22 +15,19 @@ static void usp_fork_db()
 
    U_NEW(UOrmSession, psql_db, UOrmSession(U_CONSTANT_TO_PARAM("hello_world")));
 
-   if (psql_db->isReady())
+   if (psql_db->isReady() == false)
       {
-      U_NEW(UOrmStatement, pstmt_db, UOrmStatement(*psql_db, U_CONSTANT_TO_PARAM("SELECT randomNumber FROM World WHERE id = ?")));
+      U_WARNING("usp_fork_db(): we cound't connect to db");
 
-      if (pstmt_db == U_NULLPTR)
-         {
-         U_WARNING("usp_fork_db(): we cound't connect to db");
+      return;
+      }
 
-         return;
-         }
+   U_NEW(UOrmStatement, pstmt_db, UOrmStatement(*psql_db, U_CONSTANT_TO_PARAM("SELECT randomNumber FROM World WHERE id = ?")));
 
-      U_NEW(World, pworld_db, World);
+   U_NEW(World, pworld_db, World);
 
-      pstmt_db->use( pworld_db->id);
-      pstmt_db->into(pworld_db->randomNumber);
-      }
+   pstmt_db->use( pworld_db->id);
+   pstmt_db->into(pworld_db->randomNumber);
 }
 
 #ifdef DEBUG

+ 11 - 14
frameworks/C++/ulib/src/fortune.usp

@@ -20,27 +20,24 @@ static void usp_fork_fortune()
 
    U_INTERNAL_DUMP("psql_fortune = %p", psql_fortune)
 
-   if (psql_fortune->isReady())
+   if (psql_fortune->isReady() == false)
       {
-      U_NEW(UOrmStatement, pstmt_fortune, UOrmStatement(*psql_fortune, U_CONSTANT_TO_PARAM("SELECT id, message FROM Fortune")));
+      U_WARNING("usp_fork_fortune(): we cound't connect to db");
 
-      if (pstmt_fortune == U_NULLPTR)
-         {
-         U_WARNING("usp_fork_fortune(): we cound't connect to db");
+      return;
+      }
 
-         return;
-         }
+   U_NEW(UOrmStatement, pstmt_fortune, UOrmStatement(*psql_fortune, U_CONSTANT_TO_PARAM("SELECT id, message FROM Fortune")));
 
-      if (UOrmDriver::isPGSQL()) *psql_fortune << "BEGIN ISOLATION LEVEL SERIALIZABLE; COMMIT";
+   if (UOrmDriver::isPGSQL()) *psql_fortune << "BEGIN ISOLATION LEVEL SERIALIZABLE; COMMIT";
 
-      U_NEW(Fortune, pfortune, Fortune);
+   U_NEW(Fortune, pfortune, Fortune);
 
-      pstmt_fortune->into(*pfortune);
+   pstmt_fortune->into(*pfortune);
 
-      U_NEW(UString, pencoded, UString(100U));
-      U_NEW(UVector<Fortune*>, pvfortune, UVector<Fortune*>);
-      U_NEW(Fortune, pfortune2add, Fortune(0, U_STRING_FROM_CONSTANT("Additional fortune added at request time.")));
-      }
+   U_NEW(UString, pencoded, UString(100U));
+   U_NEW(UVector<Fortune*>, pvfortune, UVector<Fortune*>);
+   U_NEW(Fortune, pfortune2add, Fortune(0, U_STRING_FROM_CONSTANT("Additional fortune added at request time.")));
 }
 
 #ifdef DEBUG

+ 12 - 15
frameworks/C++/ulib/src/query.usp

@@ -19,28 +19,25 @@ static void usp_fork_query()
 
    U_NEW(UOrmSession, psql_query, UOrmSession(U_CONSTANT_TO_PARAM("hello_world")));
 
-   if (psql_query->isReady())
+   if (psql_query->isReady() == false)
       {
-      U_NEW(UOrmStatement, pstmt_query, UOrmStatement(*psql_query, U_CONSTANT_TO_PARAM("SELECT randomNumber FROM World WHERE id = ?")));
+      U_WARNING("usp_fork_query(): we cound't connect to db");
 
-      if (pstmt_query == U_NULLPTR)
-         {
-         U_WARNING("usp_fork_query(): we cound't connect to db");
+      return;
+      }
 
-         return;
-         }
+   U_NEW(UOrmStatement, pstmt_query, UOrmStatement(*psql_query, U_CONSTANT_TO_PARAM("SELECT randomNumber FROM World WHERE id = ?")));
 
-      if (UOrmDriver::isPGSQL()) *psql_query << "BEGIN TRANSACTION";
+   if (UOrmDriver::isPGSQL()) *psql_query << "BEGIN TRANSACTION";
 
-      U_NEW(World, pworld_query, World);
+   U_NEW(World, pworld_query, World);
 
-      pstmt_query->use( pworld_query->id);
-      pstmt_query->into(pworld_query->randomNumber);
+   pstmt_query->use( pworld_query->id);
+   pstmt_query->into(pworld_query->randomNumber);
 
-#  ifndef AS_cpoll_cppsp_DO
-      U_NEW(UVector<World*>, pvworld_query, UVector<World*>(500));
-#  endif
-      }
+#ifndef AS_cpoll_cppsp_DO
+   U_NEW(UVector<World*>, pvworld_query, UVector<World*>(500));
+#endif
 }
 
 #ifdef DEBUG

+ 14 - 18
frameworks/C++/ulib/src/update.usp

@@ -20,31 +20,27 @@ static void usp_fork_update()
 
    U_NEW(UOrmSession, psql_update, UOrmSession(U_CONSTANT_TO_PARAM("hello_world")));
 
-   if (psql_update->isReady())
+   if (psql_update->isReady() == false)
       {
-      U_NEW(UOrmStatement, pstmt1, UOrmStatement(*psql_update, U_CONSTANT_TO_PARAM("SELECT randomNumber FROM World WHERE id = ?")));
-      U_NEW(UOrmStatement, pstmt2, UOrmStatement(*psql_update, U_CONSTANT_TO_PARAM("UPDATE World SET randomNumber = ? WHERE id = ?")));
+      U_WARNING("usp_fork_update(): we cound't connect to db");
 
-      if (pstmt1 == U_NULLPTR ||
-          pstmt2 == U_NULLPTR)
-         {
-         U_WARNING("usp_fork_update(): we cound't connect to db");
+      return;
+      }
 
-         return;
-         }
+   U_NEW(UOrmStatement, pstmt1, UOrmStatement(*psql_update, U_CONSTANT_TO_PARAM("SELECT randomNumber FROM World WHERE id = ?")));
+   U_NEW(UOrmStatement, pstmt2, UOrmStatement(*psql_update, U_CONSTANT_TO_PARAM("UPDATE World SET randomNumber = ? WHERE id = ?")));
 
-      if (UOrmDriver::isPGSQL()) *psql_update << "SET synchronous_commit TO OFF";
+   if (UOrmDriver::isPGSQL()) *psql_update << "SET synchronous_commit TO OFF";
 
-      U_NEW(World, pworld_update, World);
+   U_NEW(World, pworld_update, World);
 
-      pstmt1->use( pworld_update->id);
-      pstmt1->into(pworld_update->randomNumber);
-      pstmt2->use( pworld_update->randomNumber, pworld_update->id);
+   pstmt1->use( pworld_update->id);
+   pstmt1->into(pworld_update->randomNumber);
+   pstmt2->use( pworld_update->randomNumber, pworld_update->id);
 
-#  ifndef AS_cpoll_cppsp_DO
-      U_NEW(UVector<World*>, pvworld_update, UVector<World*>(500));
-#  endif
-      }
+#ifndef AS_cpoll_cppsp_DO
+   U_NEW(UVector<World*>, pvworld_update, UVector<World*>(500));
+#endif
 }
 
 #ifdef DEBUG

+ 3 - 3
toolset/setup/linux/frameworks/ulib.sh

@@ -101,17 +101,17 @@ fi
 
 # Compile usp pages (no more REDIS)
 cd ../../src/ulib/net/server/plugin/usp
-make json.la plaintext.la db.la query.la update.la fortune.la \
+make json.la plaintext.la db.la query.la update.la fortune.la cached_worlds.la \
 	  mdb.la mquery.la mupdate.la mfortune.la
 #    rdb.la rquery.la rupdate.la rfortune.la
 
 # Check that compilation worked
-if [ ! -e .libs/db.so ]; then
+if [ ! -e .libs/cached_worlds.so ]; then
 	exit 1
 fi
 
 cp .libs/json.so .libs/plaintext.so \
-	.libs/db.so   .libs/query.so  .libs/update.so  .libs/fortune.so \
+	.libs/db.so   .libs/query.so  .libs/update.so  .libs/fortune.so .libs/cached_worlds.so \
 	.libs/mdb.so  .libs/mquery.so .libs/mupdate.so .libs/mfortune.so $ULIB_DOCUMENT_ROOT
 #	.libs/rdb.so  .libs/rquery.so .libs/rupdate.so .libs/rfortune.so \