Эх сурвалжийг харах

[OCaml/Dream] Add initial tests for Dream (#9278)

Jonathan Cooper 10 сар өмнө
parent
commit
d96bcdc36e

+ 35 - 0
frameworks/OCaml/dream/README.md

@@ -0,0 +1,35 @@
+# Dream
+
+## Overview
+
+Most of all of the code is inside of `test_dream/bin/main.ml` file. 
+
+## Implemented tests
+
+| Test Name  | Endpoint                      |
+|------------|-------------------------------|
+| Plain text | http://0.0.0.0:8080/plaintext |
+| Json       | http://0.0.0.0:8080/json      |
+
+## Headers
+
+A simple middleware was added that adds the required headers for the Techempower Benchmarks.
+The date header is refreshed only once per second as allowed by the rules for performance.
+
+## Dependencies
+
+The `test_dream/dune-project` and `test_dream/bin/dune` are where dependencies are managed 
+for this project. If you add a dependency to those locations and then run the following:
+
+```
+cd test_dream
+dune build test_dream.opam
+opam install --yes --deps-only .
+```
+
+You will update the opam package list and install your changes locally.
+
+## Running tests
+
+$ tfb --mode verify --test dream
+

+ 26 - 0
frameworks/OCaml/dream/benchmark_config.json

@@ -0,0 +1,26 @@
+{
+  "framework": "dream",
+  "tests": [
+    {
+      "default": {
+        "json_url": "/json",
+        "plaintext_url": "/plaintext",
+        "port": 8080,
+        "approach": "Realistic",
+        "classification": "Fullstack",
+        "database": "postgres",
+        "framework": "Dream",
+        "language": "OCaml",
+        "flavor": "None",
+        "orm": "Micro",
+        "platform": "http/af",
+        "webserver": "None",
+        "os": "Linux",
+        "database_os": "Linux",
+        "display_name": "Dream",
+        "notes": "",
+        "versus": "httpaf"
+      }
+    }
+  ]
+}

+ 12 - 0
frameworks/OCaml/dream/dream.dockerfile

@@ -0,0 +1,12 @@
+FROM ocaml/opam:debian-ocaml-5.1
+COPY ./dream_test/ /app
+WORKDIR /app
+USER root
+RUN apt install -y libgmp-dev libev-dev pkg-config libssl-dev
+RUN opam install --yes --deps-only .
+RUN opam install dune
+RUN eval $(opam env)
+RUN opam exec -- dune build --profile release
+CMD [ "./_build/default/bin/main.exe" ]
+EXPOSE 8080
+

+ 5 - 0
frameworks/OCaml/dream/dream_test/bin/dune

@@ -0,0 +1,5 @@
+(executable
+ (public_name dream_test)
+ (name main)
+ (preprocess (pps lwt_ppx ppx_yojson_conv))
+ (libraries dream_test dream ppx_yojson_conv calendar))

+ 53 - 0
frameworks/OCaml/dream/dream_test/bin/main.ml

@@ -0,0 +1,53 @@
+open Ppx_yojson_conv_lib.Yojson_conv.Primitives
+
+type message_object = {
+  message : string;
+} [@@deriving yojson]
+
+let time_cache = Atomic.make false;;
+let time_ref = ref("None");;
+
+let time () = 
+    if not @@ Atomic.get time_cache 
+    then begin
+        time_ref := CalendarLib.Printer.Calendar.sprint "%a, %d %b %Y %H:%M:%S UTC" @@ CalendarLib.Calendar.now ();
+        Atomic.set time_cache true;
+        (!time_ref)
+        end 
+    else 
+        (!time_ref);;
+
+let tech_empower_headers (inner_handler: Dream.handler) =
+    (fun (req) ->
+        let%lwt res = inner_handler req in
+        Dream.set_header res "Server" "dream";
+        Dream.set_header res "Date" @@ time (); 
+        Lwt.return res
+    );;
+
+let rec timer () =
+    Unix.sleepf 0.9;
+    Atomic.set time_cache false;
+    timer();;
+
+let () =
+    let time_invalidator = Domain.spawn(fun () -> timer ()) in
+    Dream.run ~interface: "0.0.0.0"
+    @@ tech_empower_headers
+    @@ Dream.router [
+        Dream.get "/" (fun _ ->
+            Dream.html "Hello, world!"
+        );
+        Dream.get "/plaintext" (fun _ ->
+            Dream.response ~headers: [("Content-Type", "text/plain")]
+                "Hello, world!"
+            |> Lwt.return
+        );
+        Dream.get "/json" (fun _ ->
+            { message = "Hello, world!" }
+            |> yojson_of_message_object
+            |> Yojson.Safe.to_string
+            |> Dream.json
+        );
+    ];
+    Domain.join time_invalidator;;

+ 35 - 0
frameworks/OCaml/dream/dream_test/dream_test.opam

@@ -0,0 +1,35 @@
+# This file is generated by dune, edit dune-project instead
+opam-version: "2.0"
+synopsis: "A short synopsis"
+description: "A longer description"
+maintainer: ["Maintainer Name"]
+authors: ["Author Name"]
+license: "LICENSE"
+tags: ["topics" "to describe" "your" "project"]
+homepage: "https://github.com/username/reponame"
+doc: "https://url/to/documentation"
+bug-reports: "https://github.com/username/reponame/issues"
+depends: [
+  "ocaml"
+  "dune" {>= "3.14"}
+  "dream"
+  "lwt"
+  "ppx_yojson_conv"
+  "calendar"
+  "odoc" {with-doc}
+]
+build: [
+  ["dune" "subst"] {dev}
+  [
+    "dune"
+    "build"
+    "-p"
+    name
+    "-j"
+    jobs
+    "@install"
+    "@runtest" {with-test}
+    "@doc" {with-doc}
+  ]
+]
+dev-repo: "git+https://github.com/username/reponame.git"

+ 26 - 0
frameworks/OCaml/dream/dream_test/dune-project

@@ -0,0 +1,26 @@
+(lang dune 3.14)
+
+(name dream_test)
+
+(generate_opam_files true)
+
+(source
+ (github username/reponame))
+
+(authors "Author Name")
+
+(maintainers "Maintainer Name")
+
+(license LICENSE)
+
+(documentation https://url/to/documentation)
+
+(package
+ (name dream_test)
+ (synopsis "A short synopsis")
+ (description "A longer description")
+ (depends ocaml dune dream lwt ppx_yojson_conv calendar)
+ (tags
+  (topics "to describe" your project)))
+
+; See the complete stanza docs at https://dune.readthedocs.io/en/stable/dune-files.html#dune-project

+ 2 - 0
frameworks/OCaml/dream/dream_test/lib/dune

@@ -0,0 +1,2 @@
+(library
+ (name dream_test))

+ 2 - 0
frameworks/OCaml/dream/dream_test/test/dune

@@ -0,0 +1,2 @@
+(test
+ (name test_dream_test))

+ 0 - 0
frameworks/OCaml/dream/dream_test/test/test_dream_test.ml