Skip to content

Commit

Permalink
add d1 roundtrip
Browse files Browse the repository at this point in the history
  • Loading branch information
bruceharrison1984 committed Jan 14, 2024
1 parent b5f0375 commit 1281313
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,22 @@ export default {
}
});

return new Response(JSON.stringify({ kv_insert, kv_read, d1_insert, d1_read, kv_insert_and_propogate: kv_insert_and_propagate }));
const d1_insert_and_propagate = await measureTime(async () => {
const id = crypto.randomUUID();
await measureTime(async () => await env.DB.exec(`INSERT INTO benchmark (NAME) VALUES ('${id}')`));

let result = null;
while (!result) {
result = await await env.DB.exec(`SELECT * FROM benchmark WHERE NAME = '${id}'`);
}
});

return new Response(JSON.stringify({ kv_insert, kv_read, kv_insert_and_propagate, d1_insert, d1_read, d1_insert_and_propagate }));
},
};

const measureTime = async (func: () => any | Promise<any>) => {
const start = performance.now();
await func();
return performance.now() - start;
return `${performance.now() - start}ms`;
};

0 comments on commit 1281313

Please sign in to comment.