Skip to content
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

[RFC] compile createSourceEventStream #153

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/__benchmarks__/benchmarks.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import Benchmark from "benchmark";
import {
createSourceEventStream,
DocumentNode,
execute,
getIntrospectionQuery,
GraphQLSchema,
parse
} from "graphql";
import { compileQuery, isCompiledQuery, isPromise } from "../execution";
import { benchmarkCreateSourceEventStream } from "./createSourceEventStream";
import {
query as fewResolversQuery,
schema as fewResolversSchema
Expand Down Expand Up @@ -51,8 +53,8 @@ const benchmarks: { [key: string]: BenchmarkMaterial } = {
async function runBenchmarks() {
const skipJS = process.argv[2] === "skip-js";
const skipJSON = process.argv[2] === "skip-json";
const benchs = await Promise.all(
Object.entries(benchmarks).map(
const benchs = await Promise.all([
...Object.entries(benchmarks).map(
async ([bench, { query, schema, variables }]) => {
const compiledQuery = compileQuery(schema, query, undefined, {
debug: true
Expand Down Expand Up @@ -145,7 +147,9 @@ async function runBenchmarks() {
});
return suite;
}
)
),
benchmarkCreateSourceEventStream(),
]
);

const benchsToRun = benchs.filter(isNotNull);
Expand Down
153 changes: 153 additions & 0 deletions src/__benchmarks__/createSourceEventStream.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
import Benchmark from "benchmark";
import {
createSourceEventStream,
GraphQLBoolean,
GraphQLID,
GraphQLList,
GraphQLNonNull,
GraphQLObjectType,
GraphQLSchema,
GraphQLString,
parse

} from "graphql";
import { isPromise } from "../execution";
import { compileSourceEventStream } from "..";

const schema = function schema() {
const BlogArticle: GraphQLObjectType = new GraphQLObjectType({
name: "Article",
fields: {
id: { type: new GraphQLNonNull(GraphQLID) },
isPublished: { type: GraphQLBoolean },
title: { type: GraphQLString },
body: { type: GraphQLString },
keywords: { type: new GraphQLList(GraphQLString) }
}
});

const BlogQuery = new GraphQLObjectType({
name: "Query",
fields: {
article: {
type: BlogArticle,
args: { id: { type: GraphQLID } },
resolve: (_, { id }) => article(id)
},
}
});
const BlogSubscription = new GraphQLObjectType({
name: "Subscription",
fields: {
news: {
type: BlogArticle,
args: {},
subscribe: async () => {
const it: AsyncIterable<any> = {
[Symbol.asyncIterator]() {
return {
next: async () => ({done: true, value: undefined})
}
},
}
return it;
}
},
}
});

function article(id: number): any {
return {
id,
isPublished: true,
title: "My Article " + id,
body: "This is a post",
hidden: "This data is not exposed in the schema",
keywords: ["foo", "bar", 1, true, null]
};
}

return new GraphQLSchema({
query: BlogQuery,
subscription: BlogSubscription
});
}()

const subscription = parse(`
subscription {
news {
...articleFields,
}
}

fragment articleFields on Article {
__typename
id,
isPublished,
title,
body,
hidden,
notdefined
}
`);

// TODO
const skipJS = false;

export function benchmarkCreateSourceEventStream() {
const compiledQuery = compileSourceEventStream(schema, subscription, undefined, {
debug: true
} as any);
if (!compiledQuery) {
// eslint-disable-next-line no-console
console.error(`failed to compile`);
return null;
}
const suite = new Benchmark.Suite('createSourceEventStream');
if (!skipJS) {
suite.add("graphql-js", {
minSamples: 150,
defer: true,
fn(deferred: any) {
const stream = createSourceEventStream(
schema,
subscription,
{},
);
if (isPromise(stream)) {
return stream.then((res) =>
deferred.resolve(res)
);
}
return deferred.resolve()
}
});
}
suite
.add("graphql-jit", {
minSamples: 150,
defer: true,
fn(deferred: any) {
const stream = compiledQuery(
{},
undefined
);
if (isPromise(stream)) {
return stream.then((res) =>
deferred.resolve(res)
);
}
return deferred.resolve()
}
})
// add listeners
.on("cycle", (event: any) => {
// eslint-disable-next-line no-console
console.log(String(event.target));
})
.on("start", () => {
// eslint-disable-next-line no-console
console.log("Starting createSourceEventStream");
});
return suite;
}
Loading