Przeglądaj źródła

[Java] Add ActiveJ framework (#6280)

eduard-vasinskyi 4 lat temu
rodzic
commit
30193d1f77

+ 28 - 0
frameworks/Java/activej/README.md

@@ -0,0 +1,28 @@
+# ActiveJ Benchmarking Test
+
+This is the ActiveJ portion of a [benchmarking test suite](../) comparing a variety of web development platforms.
+
+### Plaintext Test
+
+* [Plaintext test source](src/main/java/io/activej/http/benchmark/Main.java)
+
+### JSON Serialization Test
+
+* [JSON test source](src/main/java/io/activej/http/benchmark/Main.java)
+
+
+## Versions
+
+* [Java OpenJDK 1.8](http://openjdk.java.net/)
+* [ActiveJ 4.0-SNAPSHOT](http://activej.io/)
+* [DSL-JSON 1.9.7](https://github.com/ngs-doo/dsl-json)
+
+## Test URLs
+
+### Plaintext Test
+
+    http://localhost:8080/plaintext
+
+### JSON Encoding Test
+
+    http://localhost:8080/json

+ 11 - 0
frameworks/Java/activej/activej.dockerfile

@@ -0,0 +1,11 @@
+FROM maven:3.6.1-jdk-11-slim as maven
+
+WORKDIR /activej
+COPY pom.xml pom.xml
+COPY src src
+RUN mvn compile assembly:single -q
+
+FROM openjdk:11.0.3-jdk-slim
+WORKDIR /activej
+COPY --from=maven /activej/target/activej-server-benchmark-0.0.1-SNAPSHOT-jar-with-dependencies.jar app.jar
+CMD ["java", "-Xms2G", "-Xmx2G", "-server", "-XX:+UseNUMA", "-XX:+UseParallelGC", "-XX:+AggressiveOpts", "-DHttpHeadersMultimap.initialSize=16", "-jar", "app.jar"]

+ 23 - 0
frameworks/Java/activej/benchmark_config.json

@@ -0,0 +1,23 @@
+{
+  "framework": "activej",
+  "tests": [
+    {
+      "default": {
+        "json_url": "/json",
+        "plaintext_url": "/plaintext",
+        "port": 8080,
+        "approach": "Realistic",
+        "classification": "Micro",
+        "framework": "activej",
+        "language": "Java",
+        "flavor": "None",
+        "platform": "activej",
+        "webserver": "None",
+        "os": "Linux",
+        "display_name": "activej",
+        "notes": "",
+        "versus": "None"
+      }
+    }
+  ]
+}

+ 15 - 0
frameworks/Java/activej/config.toml

@@ -0,0 +1,15 @@
+[framework]
+name = "activej"
+
+[main]
+urls.plaintext = "/plaintext"
+urls.json = "/json"
+approach = "Realistic"
+classification = "Micro"
+database = "None"
+database_os = "Linux"
+os = "Linux"
+orm = "None"
+platform = "activej"
+webserver = "None"
+versus = ""

+ 58 - 0
frameworks/Java/activej/pom.xml

@@ -0,0 +1,58 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+		 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+	<modelVersion>4.0.0</modelVersion>
+
+	<groupId>io.activej</groupId>
+	<artifactId>activej-server-benchmark</artifactId>
+	<version>0.0.1-SNAPSHOT</version>
+	<build>
+		<plugins>
+			<plugin>
+				<groupId>org.apache.maven.plugins</groupId>
+				<artifactId>maven-compiler-plugin</artifactId>
+				<configuration>
+					<source>8</source>
+					<target>8</target>
+				</configuration>
+			</plugin>
+			<plugin>
+				<artifactId>maven-assembly-plugin</artifactId>
+				<version>3.1.0</version>
+				<configuration>
+					<archive>
+						<manifest>
+							<mainClass>io.activej.http.benchmark.Main</mainClass>
+						</manifest>
+					</archive>
+					<descriptorRefs>
+						<descriptorRef>jar-with-dependencies</descriptorRef>
+					</descriptorRefs>
+				</configuration>
+				<executions>
+					<execution>
+						<id>make-assembly</id>
+						<phase>package</phase>
+						<goals>
+							<goal>single</goal>
+						</goals>
+					</execution>
+				</executions>
+			</plugin>
+		</plugins>
+	</build>
+
+	<dependencies>
+		<dependency>
+			<groupId>io.activej</groupId>
+			<artifactId>activej-launchers-http</artifactId>
+			<version>4.0-beta1</version>
+		</dependency>
+		<dependency>
+			<groupId>com.dslplatform</groupId>
+			<artifactId>dsl-json-java8</artifactId>
+			<version>1.9.7</version>
+		</dependency>
+	</dependencies>
+
+</project>

+ 13 - 0
frameworks/Java/activej/src/main/java/io/activej/http/benchmark/HelloWorldObject.java

@@ -0,0 +1,13 @@
+package io.activej.http.benchmark;
+
+public final class HelloWorldObject {
+	private final String message;
+
+	public HelloWorldObject(String message) {
+		this.message = message;
+	}
+
+	public String getMessage() {
+		return message;
+	}
+}

+ 128 - 0
frameworks/Java/activej/src/main/java/io/activej/http/benchmark/Main.java

@@ -0,0 +1,128 @@
+package io.activej.http.benchmark;
+
+import com.dslplatform.json.DslJson;
+import com.dslplatform.json.JsonWriter;
+import com.dslplatform.json.runtime.Settings;
+import io.activej.async.service.EventloopTaskScheduler;
+import io.activej.bytebuf.ByteBuf;
+import io.activej.config.Config;
+import io.activej.eventloop.Eventloop;
+import io.activej.http.*;
+import io.activej.inject.annotation.Eager;
+import io.activej.inject.annotation.Named;
+import io.activej.inject.annotation.Provides;
+import io.activej.inject.module.AbstractModule;
+import io.activej.inject.module.Module;
+import io.activej.launchers.http.MultithreadedHttpServerLauncher;
+import io.activej.promise.Promise;
+import io.activej.worker.annotation.Worker;
+
+import java.net.InetSocketAddress;
+import java.time.Duration;
+import java.time.ZonedDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.concurrent.atomic.AtomicReference;
+
+import static io.activej.bytebuf.ByteBufStrings.encodeAscii;
+import static io.activej.config.Config.ofClassPathProperties;
+import static io.activej.config.Config.ofSystemProperties;
+import static io.activej.config.converter.ConfigConverters.ofInetSocketAddress;
+import static java.nio.charset.StandardCharsets.UTF_8;
+
+public final class Main extends MultithreadedHttpServerLauncher {
+	private static final HttpHeaderValue SERVER_HEADER_VALUE = HttpHeaderValue.ofBytes(encodeAscii("X"));
+	private static final HttpHeaderValue JSON_CONTENT_TYPE_HEADER_VALUE = HttpHeaderValue.ofBytes(encodeAscii("application/json"));
+	private static final HttpHeaderValue TEXT_CONTENT_TYPE_HEADER_VALUE = HttpHeaderValue.ofBytes(encodeAscii("text/plain"));
+
+	private static final DateTimeFormatter FORMATTER = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss z");
+	private static final AtomicReference<HttpHeaderValue> dateRef = new AtomicReference<>(HttpHeaderValue.ofBytes(encodeAscii(getServerTime())));
+
+	private static final DslJson<Object> DSL_JSON = new DslJson<>(Settings.basicSetup());
+	private static final byte[] PLAINTEXT_BYTES = "Hello, World!".getBytes(UTF_8);
+
+	@Provides
+	@Eager
+	EventloopTaskScheduler scheduler(Eventloop eventloop) {
+		return EventloopTaskScheduler.create(eventloop,
+				() -> {
+					dateRef.set(HttpHeaderValue.ofBytes(encodeAscii(getServerTime())));
+					return Promise.complete();
+				})
+				.withInterval(Duration.ofSeconds(1));
+	}
+
+	@Provides
+	@Worker
+	AsyncServlet mainServlet(@Named("json") AsyncServlet jsonServlet, @Named("plaintext") AsyncServlet plaintextServlet) {
+		return request -> {
+			String path = request.getPath();
+			if ("/json".equals(path)) {
+				return jsonServlet.serve(request);
+			} else if ("/plaintext".equals(path)) {
+				return plaintextServlet.serve(request);
+			} else {
+				return Promise.ofException(HttpError.ofCode(400));
+			}
+		};
+	}
+
+	@Provides
+	@Worker
+	JsonWriter jsonWriter() {
+		return DSL_JSON.newWriter();
+	}
+
+	@Provides
+	@Worker
+	@Named("json")
+	AsyncServlet jsonServlet(JsonWriter writer) {
+		return request -> {
+			try {
+				writer.reset();
+				DSL_JSON.serialize(writer, new HelloWorldObject("Hello, world!"));
+			} catch (Exception e) {
+				return Promise.ofException(HttpError.ofCode(400, "Failed to serialize JSON", e));
+			}
+			return HttpResponse.ok200()
+					.withBody(ByteBuf.wrapForReading(writer.toByteArray()))
+					.withHeader(HttpHeaders.CONTENT_TYPE, JSON_CONTENT_TYPE_HEADER_VALUE)
+					.withHeader(HttpHeaders.SERVER, SERVER_HEADER_VALUE)
+					.withHeader(HttpHeaders.DATE, dateRef.get());
+		};
+	}
+
+	@Provides
+	@Worker
+	@Named("plaintext")
+	AsyncServlet plaintextServlet() {
+		return request -> HttpResponse.ok200()
+				.withBody(ByteBuf.wrap(PLAINTEXT_BYTES, 0, 13))
+				.withHeader(HttpHeaders.CONTENT_TYPE, TEXT_CONTENT_TYPE_HEADER_VALUE)
+				.withHeader(HttpHeaders.SERVER, SERVER_HEADER_VALUE)
+				.withHeader(HttpHeaders.DATE, dateRef.get());
+	}
+
+	static String getServerTime() {
+		return ZonedDateTime
+				.now()
+				.format(FORMATTER);
+	}
+
+	@Override
+	protected Module getOverrideModule() {
+		return new AbstractModule() {
+			@Provides
+			Config config() {
+				return Config.create()
+						.with("http.listenAddresses", Config.ofValue(ofInetSocketAddress(), new InetSocketAddress(PORT)))
+						.with("workers", "" + Integer.toString(2 * Runtime.getRuntime().availableProcessors()))
+						.overrideWith(ofClassPathProperties(PROPERTIES_FILE, true))
+						.overrideWith(ofSystemProperties("config"));
+			}
+		};
+	}
+
+	public static void main(String[] args) throws Exception {
+		new Main().launch(args);
+	}
+}

+ 2 - 0
frameworks/Java/activej/src/main/resources/http-server.properties

@@ -0,0 +1,2 @@
+eventloop.worker.idleInterval=10 millis
+eventloop.primary.idleInterval=10 millis