Bladeren bron

Update wizzardo-http implementation (#5961)

* updated dependency

* updated dependency and improved db-handlers

* cleanup

* cleanup

* updated wizzardo's libs and implementation

* updated dependencies

* updated wizzardo-http implementation

* updated readme
Mikhail Bobrutskov 4 jaren geleden
bovenliggende
commit
b7d0eb79ef

+ 3 - 2
frameworks/Java/wizzardo-http/README.md

@@ -12,9 +12,10 @@ This is the wizzardo-http portion of a [benchmarking test suite](../) comparing
 - [x] JSON serialization
 - [x] Single query
 - [x] Multiple queries
-- [ ] Fortunes
+- [x] Cached queries
+- [x] Fortunes
 - [x] Data updates
 - [x] Plaintext
 
 ## Versions
-wizzardo-http 0.2 (https://github.com/wizzardo/http)
+wizzardo-http 0.4 (https://github.com/wizzardo/webery)

+ 2 - 1
frameworks/Java/wizzardo-http/benchmark_config.json

@@ -8,7 +8,8 @@
         "db_url": "/db",
         "query_url": "/queries?queries=",
         "update_url": "/updates?queries=",
-        "_fortune_url": "/fortunes",
+        "fortune_url": "/fortunes",
+        "cached_query_url": "/cached-worlds?count=",
         "port": 8080,
         "approach": "Realistic",
         "classification": "Micro",

+ 1 - 1
frameworks/Java/wizzardo-http/build.gradle

@@ -18,7 +18,7 @@ repositories {
 }
 
 dependencies {
-    compile 'com.wizzardo:http:0.3'
+    compile 'com.wizzardo:http:0.4'
     compile 'com.wizzardo:reactive-pg-client:0.10.2.1'
 }
 

+ 2 - 0
frameworks/Java/wizzardo-http/src/main/java/com/wizzardo/techempower/App.java

@@ -40,6 +40,8 @@ public class App {
                             .append("/db", DBController.class, "world")
                             .append("/queries", DBController.class, "queries")
                             .append("/updates", DBController.class, "updates")
+                            .append("/fortunes", DBController.class, "fortunes")
+                            .append("/cached-worlds", DBController.class, "cachedWorlds")
                     ;
                 }
         );

+ 7 - 0
frameworks/Java/wizzardo-http/src/main/java/com/wizzardo/techempower/CachedWorld.java

@@ -0,0 +1,7 @@
+package com.wizzardo.techempower;
+
+public class CachedWorld extends World {
+    public CachedWorld(int id, int randomNumber) {
+        super(id, randomNumber);
+    }
+}

+ 36 - 0
frameworks/Java/wizzardo-http/src/main/java/com/wizzardo/techempower/CachedWorldService.java

@@ -0,0 +1,36 @@
+package com.wizzardo.techempower;
+
+import com.wizzardo.http.framework.di.PostConstruct;
+import com.wizzardo.http.framework.di.Service;
+import com.wizzardo.tools.cache.Cache;
+import io.reactiverse.pgclient.PgIterator;
+import io.reactiverse.pgclient.PgPool;
+import io.reactiverse.pgclient.Row;
+import io.reactiverse.pgclient.Tuple;
+
+public class CachedWorldService implements Service, PostConstruct {
+
+    DBService dbService;
+    Cache<Integer, CachedWorld> worldCache = new Cache<>(-1);
+
+
+    @Override
+    public void init() {
+
+        PgPool pool = dbService.getClient();
+        pool.preparedQuery("SELECT * FROM World", res -> {
+            if (res.succeeded()) {
+                for (Row row : res.result()) {
+                    CachedWorld cachedWorld = new CachedWorld(row.getInteger(0), row.getInteger(1));
+                    worldCache.put(cachedWorld.id, cachedWorld);
+                }
+            } else {
+                res.cause().printStackTrace();
+            }
+        });
+    }
+
+    public CachedWorld get(int id) {
+        return worldCache.get(id);
+    }
+}

+ 56 - 21
frameworks/Java/wizzardo-http/src/main/java/com/wizzardo/techempower/DBController.java

@@ -4,17 +4,13 @@ import com.wizzardo.epoll.ByteBufferProvider;
 import com.wizzardo.epoll.ByteBufferWrapper;
 import com.wizzardo.http.HttpConnection;
 import com.wizzardo.http.framework.Controller;
-import com.wizzardo.http.framework.template.Renderer;
 import com.wizzardo.http.request.Header;
 import com.wizzardo.http.response.Status;
 import com.wizzardo.tools.json.JsonTools;
-import io.reactiverse.pgclient.PgIterator;
-import io.reactiverse.pgclient.PgPool;
-import io.reactiverse.pgclient.Tuple;
+import com.wizzardo.tools.misc.Unchecked;
+import io.reactiverse.pgclient.*;
 
-import java.util.ArrayList;
-import java.util.Arrays;
-import java.util.List;
+import java.util.*;
 import java.util.concurrent.ThreadLocalRandom;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicInteger;
@@ -22,6 +18,7 @@ import java.util.concurrent.atomic.AtomicInteger;
 public class DBController extends Controller {
 
     DBService dbService;
+    CachedWorldService cachedWorldService;
 
     public void world() {
         response.async();
@@ -76,6 +73,18 @@ public class DBController extends Controller {
         }
     }
 
+    public void cachedWorlds() {
+        int count = Math.min(Math.max(params().getInt("count", 1), 1), 500);
+        CachedWorld[] worlds = new CachedWorld[count];
+
+        for (int i = 0; i < count; i++) {
+            worlds[i] = cachedWorldService.get(getRandomNumber());
+        }
+
+        response.appendHeader(Header.KV_CONTENT_TYPE_APPLICATION_JSON);
+        response.body(JsonTools.serializeToBytes(worlds));
+    }
+
     public void updates() {
         int queries = Math.min(Math.max(params().getInt("queries", 1), 1), 500);
         World[] worlds = new World[queries];
@@ -122,6 +131,45 @@ public class DBController extends Controller {
         }
     }
 
+    public static final class Fortune {
+        public final int id;
+        public final String message;
+
+        public Fortune(int id, String message) {
+            this.id = id;
+            this.message = Objects.requireNonNull(message);
+        }
+    }
+
+    public void fortunes() {
+        response.async();
+        dbService.getClient().preparedQuery("SELECT * FROM fortune", res -> {
+            try {
+                if (res.succeeded()) {
+                    PgRowSet result = res.result();
+                    ArrayList<Fortune> fortunes = new ArrayList<>(result.size() + 1);
+                    for (Row row : result) {
+                        fortunes.add(new Fortune(row.getInteger(0), row.getString(1)));
+                    }
+
+                    fortunes.add(new Fortune(0, "Additional fortune added at request time."));
+                    fortunes.sort(Comparator.comparing(fortune -> fortune.message));
+
+                    model().append("fortunes", fortunes);
+                    response.setBody(renderView("fortunes").renderReadableData());
+                    response.appendHeader(Header.KV_CONTENT_TYPE_HTML_UTF8);
+                } else {
+                    res.cause().printStackTrace();
+                    response.status(Status._500).body(res.cause().getMessage());
+                }
+
+                commitAsyncResponse();
+            } catch (Exception e) {
+                e.printStackTrace();
+            }
+        });
+    }
+
     static ThreadLocal<ByteBufferProvider> byteBufferProviderThreadLocal = ThreadLocal.<ByteBufferProvider>withInitial(() -> {
         ByteBufferWrapper wrapper = new ByteBufferWrapper(64 * 1024);
         return () -> wrapper;
@@ -133,24 +181,11 @@ public class DBController extends Controller {
         response.commit(connection, bufferProvider);
         connection.flush(bufferProvider);
         response.reset();
+        Unchecked.run(connection::onFinishingHandling);
     }
 
     protected int getRandomNumber() {
         return 1 + ThreadLocalRandom.current().nextInt(10000);
     }
 
-    public static final class World implements Comparable<World> {
-        public int id;
-        public int randomNumber;
-
-        public World(int id, int randomNumber) {
-            this.id = id;
-            this.randomNumber = randomNumber;
-        }
-
-        @Override
-        public int compareTo(World o) {
-            return Integer.compare(id, o.id);
-        }
-    }
 }

+ 16 - 0
frameworks/Java/wizzardo-http/src/main/java/com/wizzardo/techempower/World.java

@@ -0,0 +1,16 @@
+package com.wizzardo.techempower;
+
+public class World implements Comparable<World> {
+    public int id;
+    public int randomNumber;
+
+    public World(int id, int randomNumber) {
+        this.id = id;
+        this.randomNumber = randomNumber;
+    }
+
+    @Override
+    public int compareTo(World o) {
+        return Integer.compare(id, o.id);
+    }
+}

+ 1 - 0
frameworks/Java/wizzardo-http/src/main/resources/Config.groovy

@@ -1,4 +1,5 @@
 server {
+    name = 'W'
     host = '0.0.0.0'
     port = 8080
     ttl = 5 * 60 * 1000

+ 20 - 0
frameworks/Java/wizzardo-http/src/main/resources/views/db/fortunes.gsp

@@ -0,0 +1,20 @@
+<!DOCTYPE html>
+<html>
+<head>
+    <title>Fortunes</title>
+</head>
+<body>
+<table>
+    <tr>
+        <th>id</th>
+        <th>message</th>
+    </tr>
+    <g:each in="${fortunes}">
+        <tr>
+            <td>${it.id}</td>
+            <td>${it.message.encodeAsHTML()}</td>
+        </tr>
+    </g:each>
+</table>
+</body>
+</html>