Browse Source

First implementation with the silicon C++ framework.

Matthieu Garrigues 10 years ago
parent
commit
fe0f135227

+ 12 - 0
frameworks/C++/silicon/CMakeLists.txt

@@ -0,0 +1,12 @@
+cmake_minimum_required(VERSION 2.8)
+
+project(silicon)
+
+include_directories($ENV{IROOT}/include
+                    $ENV{HOME}/projects/silicon2
+                    $ENV{HOME}/projects/iod)
+
+add_definitions(-std=c++14  -ftemplate-depth=512 -DNDEBUG -O3)
+
+add_executable(silicon_app main.cc)
+target_link_libraries(silicon_app microhttpd mysqlclient)

+ 28 - 0
frameworks/C++/silicon/benchmark_config

@@ -0,0 +1,28 @@
+{
+  "framework": "silicon",
+  "tests": [{
+    "default": {
+      "setup_file": "setup",
+      "json_url"       : "/json",
+      "db_url"         : "/db",
+      "query_url"      : "/queries?queries=",
+      "fortune_url"    : "/fortunes",
+      "update_url"     : "/updates?queries=",
+      "plaintext_url"  : "/plaintext",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Micro",
+      "database": "MySQL",
+      "framework": "silicon",
+      "language": "C++",
+      "orm": "Full",
+      "platform": "Silicon",
+      "webserver": "None",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "silicon",
+      "notes": "",
+      "versus": ""
+    }
+  }]
+}

+ 12 - 0
frameworks/C++/silicon/install.sh

@@ -0,0 +1,12 @@
+#!/bin/bash
+
+sudo apt-get install -yq libmicrohttpd-dev libboost-dev
+
+rm -fr silicon;
+git clone https://github.com/matt-42/silicon.git
+cd silicon;
+CXX=/usr/bin/g++-4.9 ./install.sh $IROOT
+
+cd $TROOT
+mkdir -p build
+cd build; cmake .. -DCMAKE_CXX_COMPILER=g++-4.9 && make silicon_app

+ 135 - 0
frameworks/C++/silicon/main.cc

@@ -0,0 +1,135 @@
+#include <unistd.h>
+#include <iostream>
+#include <silicon/backends/mhd.hh>
+#include <silicon/api.hh>
+#include <silicon/middlewares/mysql_connection.hh>
+#include <silicon/middlewares/mysql_orm.hh>
+#include "symbols.hh"
+
+using namespace s;
+using namespace sl;
+
+typedef decltype(D(_id(_auto_increment, _primary_key) = int(),
+                   _randomNumber = int())) random_number;
+
+typedef decltype(D(_id(_auto_increment, _primary_key) = int(),
+                   _message = std::string())) fortune;
+
+typedef mysql_orm_factory<random_number> rn_orm_factory;
+typedef mysql_orm<random_number> rn_orm;
+
+typedef mysql_orm_factory<fortune> fortune_orm_factory;
+typedef mysql_orm<fortune> fortune_orm;
+
+
+std::string escape_html_entities(std::string& data)
+{
+    std::string buffer;
+    buffer.reserve(data.size());
+    for(size_t pos = 0; pos != data.size(); ++pos) {
+        switch(data[pos]) {
+            case '&':  buffer.append("&amp;");       break;
+            case '\"': buffer.append("&quot;");      break;
+            case '\'': buffer.append("&apos;");      break;
+            case '<':  buffer.append("&lt;");        break;
+            case '>':  buffer.append("&gt;");        break;
+            default:   buffer.append(&data[pos], 1); break;
+        }
+    }
+    return std::move(buffer);
+}
+
+int main(int argc, char* argv[])
+{
+
+  if (argc != 4)
+  {
+    std::cerr << "Usage: " << argv[0] << " mysql_host port pid_file" << std::endl;
+    return 1;
+  }
+  
+  auto hello_api = make_api(
+
+    _plaintext = [] () { return "Hello, World!"; },
+    _json = [] () { return D(_message = "Hello, World!"); },
+
+    _db = [] (rn_orm& orm) {
+      random_number r;
+      orm.find_by_id(1245, r);
+      return r;
+    },
+
+    _queries = [] (rn_orm& orm, get_parameters& get_params) {
+      int N = atoi(get_params["queries"].c_str());
+      N = std::max(1, std::min(N, 500));
+
+      std::vector<random_number> qs(N);
+      for (int i = 0; i < N; i++)
+        orm.find_by_id(1 + rand() % 9999, qs[i]);
+      return std::move(qs);
+    },
+
+    _updates = [] (rn_orm& orm, get_parameters& get_params) {
+      int N = atoi(get_params["queries"].c_str());
+      N = std::max(1, std::min(N, 500));
+
+      std::vector<random_number> qs(N);
+      for (int i = 0; i < N; i++)
+      {
+        orm.find_by_id(1 + rand() % 9999, qs[i]);
+        qs[i].randomNumber = 1 + rand() % 9999;
+        orm.update(qs[i]);
+      }
+      return std::move(qs);
+    },
+  
+    _fortunes = [] (fortune_orm& orm) {
+      std::vector<fortune> table;
+      orm.forall([&] (fortune& f) { table.push_back(f); });
+      table.push_back(fortune(0, "Additional fortune added at request time."));
+
+      std::sort(table.begin(), table.end(),
+                [] (const fortune& a, const fortune& b) { return a.message < b.message; });
+
+      std::stringstream ss;
+
+      ss << "<!DOCTYPE html><html><head><title>Fortunes</title></head><body><table><tr><th>id</th><th>message</th></tr>";
+      for(auto& f : table)
+        ss << "<tr><td>" << f.id << "</td><td>" << escape_html_entities(f.message) << "</td></tr>";
+      ss << "</table></body></html>";
+
+      return ss.str();
+    }
+  
+    ).bind_factories(
+      mysql_connection_factory(argv[1], "benchmarkdbuser", "benchmarkdbpass", "hello_world"),
+      fortune_orm_factory("Fortune"),
+      rn_orm_factory("World")
+      );
+  
+  try
+  {
+    // Demonize the process.
+    if (daemon(0,0))
+    {
+      std::cerr << "Cannot start the daemon." << std::endl;
+      exit(1);
+    }
+
+    // Write the pid.
+    std::ofstream pidfile(argv[3]);
+    pidfile << getpid() << std::endl;
+    pidfile.close();
+
+    // Start the server.
+    sl::mhd_json_serve(hello_api, atoi(argv[2]));
+  }
+  catch (std::exception& e)
+  {
+    std::cerr << e.what() << std::endl;
+  }
+  catch (sl::error::error& e)
+  {
+    std::cerr << e.what() << std::endl;
+  }
+}

+ 3 - 0
frameworks/C++/silicon/setup.sh

@@ -0,0 +1,3 @@
+#! /bin/bash
+
+$TROOT/build/silicon_app ${DBHOST} 8080 /tmp/silicon_pid_file &

+ 47 - 0
frameworks/C++/silicon/symbols.hh

@@ -0,0 +1,47 @@
+// Generated by iod_generate_symbols.
+#include <iod/symbol.hh>
+#ifndef IOD_SYMBOL_db
+#define IOD_SYMBOL_db
+    iod_define_symbol(db)
+#endif
+
+#ifndef IOD_SYMBOL_fortunes
+#define IOD_SYMBOL_fortunes
+    iod_define_symbol(fortunes)
+#endif
+
+#ifndef IOD_SYMBOL_id
+#define IOD_SYMBOL_id
+    iod_define_symbol(id)
+#endif
+
+#ifndef IOD_SYMBOL_json
+#define IOD_SYMBOL_json
+    iod_define_symbol(json)
+#endif
+
+#ifndef IOD_SYMBOL_message
+#define IOD_SYMBOL_message
+    iod_define_symbol(message)
+#endif
+
+#ifndef IOD_SYMBOL_plaintext
+#define IOD_SYMBOL_plaintext
+    iod_define_symbol(plaintext)
+#endif
+
+#ifndef IOD_SYMBOL_queries
+#define IOD_SYMBOL_queries
+    iod_define_symbol(queries)
+#endif
+
+#ifndef IOD_SYMBOL_randomNumber
+#define IOD_SYMBOL_randomNumber
+    iod_define_symbol(randomNumber)
+#endif
+
+#ifndef IOD_SYMBOL_updates
+#define IOD_SYMBOL_updates
+    iod_define_symbol(updates)
+#endif
+