forked from fastify/benchmarks
-
Notifications
You must be signed in to change notification settings - Fork 88
Add .NET benchmarks and update process management for go #33
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Shane32
wants to merge
6
commits into
benawad:master
Choose a base branch
from
Shane32:dotnet
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f101c5f
Add .NET benchmarks and update process management for go
Shane32 715b2cc
refactor: optimize MD5 hashing method for Author record
Shane32 951124d
refactor: remove fixed seed from random number generator in DataGener…
Shane32 2cb4cec
refactor: remove unused Microsoft.Extensions.Logging namespace
Shane32 ac7d57a
refactor: remove graceful shutdown handling from server setup
Shane32 6d94f0a
refactor: update AuthorType to use ToMd5Hash method directly
Shane32 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| "use strict"; | ||
| const { spawn } = require("child_process"); | ||
| const path = require("path"); | ||
|
|
||
| const forked = spawn("dotnet", ["run", "server.cs"], { | ||
| cwd: path.join(__dirname, "..", "other-benchmarks/dotnet-gql/"), | ||
| stdio: ["ignore", "pipe", "pipe"], | ||
| shell: false, | ||
| }); | ||
|
|
||
| // Handle stdout/stderr | ||
| forked.stdout.on("data", (data) => { | ||
| console.log(`stdout: ${data}`); | ||
| }); | ||
|
|
||
| forked.stderr.on("data", (data) => { | ||
| console.log(`stderr: ${data}`); | ||
| }); | ||
|
|
||
| forked.on("error", (error) => { | ||
| console.log(`error: ${error.message}`); | ||
| }); | ||
|
|
||
| // Forward signals to the child process | ||
| process.on("SIGINT", () => { | ||
| forked.kill("SIGINT"); | ||
| }); | ||
|
|
||
| process.on("SIGTERM", () => { | ||
| forked.kill("SIGTERM"); | ||
| }); | ||
|
|
||
| forked.on("exit", () => console.log("dotnet-graphql exited")); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,21 +1,47 @@ | ||
| "use strict"; | ||
| const { exec } = require("child_process"); | ||
| const { spawn, execSync } = require("child_process"); | ||
| const path = require("path"); | ||
| const fs = require("fs"); | ||
|
|
||
| const forked = exec( | ||
| "go run server.go", | ||
| { cwd: path.join(__dirname, "..", "other-benchmarks/go-gql/") }, | ||
| (error, stdout, stderr) => { | ||
| if (error) { | ||
| console.log(`error: ${error.message}`); | ||
| return; | ||
| } | ||
| if (stderr) { | ||
| console.log(`stderr: ${stderr}`); | ||
| return; | ||
| } | ||
| console.log(`stdout: ${stdout}`); | ||
| }, | ||
| ); | ||
| console.log(path.join(__dirname, "..", "other-benchmarks/go-gql/server.go")); | ||
| forked.on("exit", () => console.log("exited")); | ||
| const cwd = path.join(__dirname, "..", "other-benchmarks/go-gql/"); | ||
| const binaryName = process.platform === "win32" ? "go-gql.exe" : "go-gql"; | ||
| const binaryPath = path.join(cwd, "target", binaryName); | ||
|
|
||
| // Build the binary | ||
| try { | ||
| console.log("Building Go binary..."); | ||
| execSync(`go build -o target/${binaryName} .`, { cwd, stdio: "inherit" }); | ||
| } catch (error) { | ||
| console.log(`Build error: ${error.message}`); | ||
| process.exit(1); | ||
| } | ||
|
|
||
| const forked = spawn(binaryPath, [], { | ||
| cwd, | ||
| stdio: ["ignore", "pipe", "pipe"], | ||
| shell: false, | ||
| }); | ||
|
|
||
| // Handle stdout/stderr | ||
| forked.stdout.on("data", (data) => { | ||
| console.log(`stdout: ${data}`); | ||
| }); | ||
|
|
||
| forked.stderr.on("data", (data) => { | ||
| console.log(`stderr: ${data}`); | ||
| }); | ||
|
|
||
| forked.on("error", (error) => { | ||
| console.log(`error: ${error.message}`); | ||
| }); | ||
|
|
||
| // Forward signals to the child process | ||
| process.on("SIGINT", () => { | ||
| forked.kill("SIGINT"); | ||
| }); | ||
|
|
||
| process.on("SIGTERM", () => { | ||
| forked.kill("SIGTERM"); | ||
| }); | ||
|
|
||
| forked.on("exit", () => console.log("go-graphql exited")); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| #!/usr/bin/dotnet run | ||
| #:sdk Microsoft.NET.Sdk.Web | ||
| #:package GraphQL@8.8.2 | ||
| #:package GraphQL.SystemTextJson@8.8.2 | ||
| #:package GraphQL.Server.Transports.AspNetCore@8.3.3 | ||
| #:package GraphQL.Server.Ui.GraphiQL@8.3.3 | ||
| #:package Faker.Net@2.0.163 | ||
| #:property PublishAot=false | ||
|
|
||
| using System.Security.Cryptography; | ||
| using System.Text; | ||
| using Faker; | ||
| using GraphQL; | ||
| using GraphQL.Types; | ||
|
|
||
| var builder = WebApplication.CreateBuilder(args); | ||
|
|
||
| // Set logging to only show warnings and errors, output to stderr | ||
| builder.Logging.ClearProviders(); | ||
| builder.Logging.AddConsole(options => | ||
| { | ||
| options.LogToStandardErrorThreshold = LogLevel.Trace; | ||
| }); | ||
| builder.Logging.SetMinimumLevel(LogLevel.Warning); | ||
|
|
||
| // Add GraphQL services | ||
| builder.Services.AddGraphQL(b => b | ||
| .AddSystemTextJson() | ||
| .AddSchema<AppSchema>() | ||
| .AddGraphTypes(typeof(AppSchema).Assembly)); | ||
|
|
||
| var app = builder.Build(); | ||
|
|
||
| // GraphQL endpoint | ||
| app.UseGraphQL<AppSchema>("/graphql"); | ||
| app.UseGraphQLGraphiQL("/"); | ||
|
|
||
| Console.WriteLine("GraphQL server listening on http://localhost:4001/graphql"); | ||
| Console.WriteLine("GraphiQL playground available at http://localhost:4001/"); | ||
| app.Run("http://localhost:4001"); | ||
|
|
||
| // GraphQL Types | ||
| public class BookType : ObjectGraphType<Book> | ||
| { | ||
| public BookType() | ||
| { | ||
| Field(x => x.Id); | ||
| Field(x => x.Name); | ||
| Field(x => x.NumPages); | ||
| } | ||
| } | ||
|
|
||
| public class AuthorType : ObjectGraphType<Author> | ||
| { | ||
| public AuthorType() | ||
| { | ||
| Field(x => x.Id); | ||
| Field(x => x.Name); | ||
| Field(x => x.Company); | ||
| Field("md5", x => x.Name.ToMd5Hash()); | ||
| Field<ListGraphType<BookType>>("books") | ||
| .Resolve(context => context.Source.Books); | ||
| } | ||
| } | ||
|
|
||
| public class AppQuery : ObjectGraphType | ||
| { | ||
| public AppQuery() | ||
| { | ||
| Field<ListGraphType<AuthorType>>("authors") | ||
| .Resolve(context => DataGenerator.GetAuthors()); | ||
| } | ||
| } | ||
|
|
||
| public class AppSchema : Schema | ||
| { | ||
| public AppSchema(IServiceProvider serviceProvider) : base(serviceProvider) | ||
| { | ||
| Query = serviceProvider.GetRequiredService<AppQuery>(); | ||
| } | ||
| } | ||
|
|
||
| // Data Models | ||
| public record Book(string Id, string Name, int NumPages); | ||
|
|
||
| public record Author(string Id, string Name, string Company, List<Book> Books); | ||
|
|
||
| // MD5 Hashing Extension Method | ||
| static class Md5Extensions | ||
| { | ||
| public static string ToMd5Hash(this string input) | ||
| { | ||
| int bytes = Encoding.UTF8.GetMaxByteCount(input.Length); | ||
| Span<byte> utf8 = bytes <= 1024 ? stackalloc byte[bytes] : new byte[bytes]; | ||
| int actualBytes = Encoding.UTF8.GetBytes(input.AsSpan(), utf8); | ||
| Span<byte> hash = stackalloc byte[16]; | ||
| MD5.HashData(utf8[..actualBytes], hash); | ||
| return Convert.ToHexString(hash); | ||
| } | ||
| } | ||
|
|
||
| // Data Generator using Faker.Net | ||
| public static class DataGenerator | ||
| { | ||
| private static readonly List<Author> _authors = GenerateAuthors(); | ||
|
|
||
| public static List<Author> GetAuthors() => _authors; | ||
|
|
||
| private static List<Author> GenerateAuthors() | ||
| { | ||
| var authors = new List<Author>(); | ||
|
|
||
| for (int i = 0; i < 20; i++) | ||
| { | ||
| var books = new List<Book>(); | ||
| for (int k = 0; k < 3; k++) | ||
| { | ||
| books.Add(new Book( | ||
| Id: Guid.NewGuid().ToString(), | ||
| Name: Internet.DomainName(), | ||
| NumPages: RandomNumber.Next() | ||
| )); | ||
| } | ||
|
|
||
| authors.Add(new Author( | ||
| Id: Guid.NewGuid().ToString(), | ||
| Name: Name.FullName(), | ||
| Company: Company.BS(), | ||
| Books: books | ||
| )); | ||
| } | ||
|
|
||
| return authors; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This comment was marked as outdated.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.