-
Notifications
You must be signed in to change notification settings - Fork 21
examples(latency): Add example to calculate client and server latency. #50
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,151 @@ | ||
const dgraph = require("dgraph-js-http"); | ||
const minimist = require("minimist"); | ||
|
||
// Create a client stub. | ||
function newClientStub(addr) { | ||
return new dgraph.DgraphClientStub(addr); | ||
} | ||
|
||
// Create a client. | ||
function newClient(clientStub, apiKey) { | ||
c = new dgraph.DgraphClient(clientStub); | ||
if (apiKey != "") { | ||
c.setSlashApiKey(apiKey); | ||
} | ||
return c; | ||
} | ||
|
||
// Drop All - discard all data and start from a clean slate. | ||
async function dropAll(dgraphClient) { | ||
await dgraphClient.alter({ dropAll: true }); | ||
} | ||
|
||
// Set schema. | ||
async function setSchema(dgraphClient) { | ||
const schema = ` | ||
name: string @index(exact) . | ||
age: int . | ||
married: bool . | ||
loc: geo . | ||
dob: datetime . | ||
`; | ||
await dgraphClient.alter({ schema: schema }); | ||
} | ||
|
||
// Create data using JSON. | ||
async function createData(dgraphClient) { | ||
// Create a new transaction. | ||
const txn = dgraphClient.newTxn(); | ||
try { | ||
// Create data. | ||
const p = { | ||
uid: "_:blank-0", | ||
name: "Alice", | ||
age: 26, | ||
married: true, | ||
loc: { | ||
type: "Point", | ||
coordinates: [1.1, 2], | ||
}, | ||
dob: new Date(1980, 1, 1, 23, 0, 0, 0), | ||
friend: [ | ||
{ | ||
name: "Bob", | ||
age: 24, | ||
}, | ||
{ | ||
name: "Charlie", | ||
age: 29, | ||
} | ||
], | ||
school: [ | ||
{ | ||
name: "Crown Public School", | ||
} | ||
] | ||
}; | ||
|
||
// Run mutation. | ||
const assigned = await txn.mutate({ setJson: p }); | ||
|
||
// Commit transaction. | ||
await txn.commit(); | ||
|
||
// Get uid of the outermost object (person named "Alice"). | ||
// Assigned#getUidsMap() returns a map from blank node names to uids. | ||
// For a json mutation, blank node names "blank-0", "blank-1", ... are used | ||
// for all the created nodes. | ||
console.log(`Created person named "Alice" with uid = ${assigned.data.uids["blank-0"]}\n`); | ||
|
||
console.log("All created nodes (map from blank node names to uids):"); | ||
Object.keys(assigned.data.uids).forEach((key) => console.log(`${key} => ${assigned.data.uids[key]}`)); | ||
console.log(); | ||
} finally { | ||
// Clean up. Calling this after txn.commit() is a no-op | ||
// and hence safe. | ||
await txn.discard(); | ||
} | ||
} | ||
|
||
// Query for data. | ||
// This function also logs the client-side and server-side latency for running the query. | ||
async function queryData(dgraphClient) { | ||
// Run query. | ||
const query = `query all($a: string) { | ||
all(func: eq(name, $a)) { | ||
uid | ||
name | ||
age | ||
married | ||
loc | ||
dob | ||
friend { | ||
name | ||
age | ||
} | ||
school { | ||
name | ||
} | ||
} | ||
}`; | ||
const vars = { $a: "Alice" }; | ||
|
||
console.log("Query:", query.replace(/[\t\n ]+/gm, " "), "Vars:", vars); | ||
console.time('Query client latency'); | ||
const res = await dgraphClient.newTxn().queryWithVars(query, vars); | ||
console.timeEnd('Query client latency'); | ||
danielmai marked this conversation as resolved.
Show resolved
Hide resolved
|
||
console.log("Query server latency:", JSON.stringify(res.extensions.server_latency)); | ||
|
||
const ppl = res.data; | ||
|
||
// Print results. | ||
console.log(`Number of people named "Alice": ${ppl.all.length}`); | ||
ppl.all.forEach((person) => console.log(person)); | ||
return res; | ||
} | ||
|
||
async function main() { | ||
const args = minimist(process.argv.slice(2), { | ||
string: 'addr', | ||
string: 'api-key', | ||
boolean: 'drop-all', | ||
default: { | ||
'addr': 'http://localhost:8080' | ||
}, | ||
alias: { apiKey: 'api-key', dropAll: 'drop-all' } | ||
}); | ||
const dgraphClientStub = newClientStub(args.addr); | ||
const dgraphClient = newClient(dgraphClientStub, args.apiKey); | ||
if (args.dropAll) { | ||
await dropAll(dgraphClient); | ||
} | ||
await setSchema(dgraphClient); | ||
await createData(dgraphClient); | ||
await queryData(dgraphClient); | ||
} | ||
|
||
main().then(() => { | ||
console.log("\nDONE!"); | ||
}).catch((e) => { | ||
console.log("ERROR: ", e); | ||
}); |
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,7 @@ | ||
{ | ||
"name": "latency", | ||
"dependencies": { | ||
"dgraph-js-http": "^21.3.0", | ||
"minimist": "^1.2.5" | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.