Skip to content

Commit 4af4805

Browse files
authored
Add Shelf Framework (#10025)
1 parent 7455a4a commit 4af4805

File tree

7 files changed

+168
-0
lines changed

7 files changed

+168
-0
lines changed

frameworks/Dart/shelf/.dockerignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# From https://hub.docker.com/_/dart
2+
.dockerignore
3+
Dockerfile
4+
build/
5+
.dart_tool/
6+
.git/
7+
.github/
8+
.gitignore
9+
.packages

frameworks/Dart/shelf/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# https://dart.dev/guides/libraries/private-files
2+
# Created by `dart pub`
3+
.dart_tool/
4+
*.lock
5+
!bin

frameworks/Dart/shelf/README.md

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Shelf Benchmarking Test
2+
3+
### Test Type Implementation Source Code
4+
5+
- [JSON](server.dart)
6+
- [PLAINTEXT](server.dart)
7+
8+
## Important Libraries
9+
10+
The tests were run with:
11+
12+
- [pkg:shelf](https://pub.dev/packages/shelf)
13+
- [Dart](https://dart.dev/)
14+
15+
## Test URLs
16+
17+
### JSON
18+
19+
http://localhost:8080/json
20+
21+
### PLAINTEXT
22+
23+
http://localhost:8080/plaintext
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
{
2+
"framework": "shelf",
3+
"tests": [
4+
{
5+
"default": {
6+
"json_url": "/json",
7+
"plaintext_url": "/plaintext",
8+
"port": 8080,
9+
"approach": "Realistic",
10+
"classification": "Micro",
11+
"database": "None",
12+
"framework": "shelf",
13+
"language": "Dart",
14+
"flavor": "None",
15+
"orm": "None",
16+
"platform": "shelf",
17+
"webserver": "None",
18+
"os": "Linux",
19+
"database_os": "Linux",
20+
"display_name": "shelf",
21+
"notes": "",
22+
"versus": "None"
23+
}
24+
}
25+
]
26+
}

frameworks/Dart/shelf/bin/server.dart

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import 'dart:convert';
2+
import 'dart:io';
3+
import 'dart:isolate';
4+
5+
import 'package:shelf/shelf.dart';
6+
import 'package:shelf/shelf_io.dart' as shelf_io;
7+
8+
void main(List<String> args) async {
9+
/// Create an [Isolate] containing an [HttpServer]
10+
/// for each processor after the first
11+
for (var i = 1; i < Platform.numberOfProcessors; i++) {
12+
await Isolate.spawn(_startServer, args);
13+
}
14+
15+
/// Create a [HttpServer] for the first processor
16+
await _startServer(args);
17+
}
18+
19+
/// Create a request handler
20+
Response _handler(Request request) {
21+
switch (request.url.path) {
22+
case 'json':
23+
return _jsonTest();
24+
case 'plaintext':
25+
return _plaintextTest();
26+
default:
27+
return _sendResponse(HttpStatus.notFound);
28+
}
29+
}
30+
31+
/// Creates and setup a [HttpServer].
32+
Future<void> _startServer(List<String> _) async {
33+
final server = await shelf_io.serve(
34+
_handler,
35+
InternetAddress.anyIPv4,
36+
8080,
37+
shared: true,
38+
);
39+
40+
/// Sets [HttpServer]'s [serverHeader].
41+
server
42+
..defaultResponseHeaders.clear()
43+
..serverHeader = 'shelf';
44+
}
45+
46+
/// Completes the given [request] by writing the [bytes] with the given
47+
/// [statusCode] and [type].
48+
Response _sendResponse(
49+
int statusCode, {
50+
ContentType? type,
51+
List<int> bytes = const [],
52+
}) {
53+
return Response(
54+
statusCode,
55+
headers: {
56+
HttpHeaders.contentLengthHeader: '${bytes.length}',
57+
HttpHeaders.dateHeader: '${HttpDate.format(DateTime.now())}',
58+
if (type != null) HttpHeaders.contentTypeHeader: type.mimeType,
59+
},
60+
body: bytes,
61+
);
62+
}
63+
64+
/// Completes the given [request] by writing the [response] as JSON.
65+
Response _sendJson(Object response) => _sendResponse(
66+
HttpStatus.ok,
67+
type: ContentType.json,
68+
bytes: _jsonEncoder.convert(response),
69+
);
70+
71+
/// Completes the given [request] by writing the [response] as plain text.
72+
Response _sendText(String response) => _sendResponse(
73+
HttpStatus.ok,
74+
type: ContentType.text,
75+
bytes: utf8.encode(response),
76+
);
77+
78+
/// Responds with the JSON test to the [request].
79+
Response _jsonTest() => _sendJson(const {'message': 'Hello, World!'});
80+
81+
/// Responds with the plaintext test to the [request].
82+
Response _plaintextTest() => _sendText('Hello, World!');
83+
84+
final _jsonEncoder = JsonUtf8Encoder();

frameworks/Dart/shelf/pubspec.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
name: shelfbenchmark
2+
description: A benchmark of pkg:shelf
3+
environment:
4+
sdk: ^3.8.0
5+
6+
dependencies:
7+
shelf: ^1.0.0
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
FROM dart:3.8 AS builder
3+
4+
COPY . /app
5+
WORKDIR /app
6+
RUN mkdir build
7+
RUN dart compile exe ./bin/server.dart -o build/server
8+
9+
FROM scratch
10+
COPY --from=builder /runtime/ /
11+
COPY --from=builder /app/build /bin
12+
13+
EXPOSE 8080
14+
CMD ["server"]

0 commit comments

Comments
 (0)