Skip to content

Commit

Permalink
添加效率测试,调整测试脚本
Browse files Browse the repository at this point in the history
  • Loading branch information
汪航洋 committed Jul 28, 2023
1 parent 71050ed commit 27acc47
Show file tree
Hide file tree
Showing 11 changed files with 244 additions and 227 deletions.
8 changes: 6 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,9 @@ jobs:
- name: Build
run: |
npm install
npm test -- --noOpen
npm run build
npm coverage
npm run build
- name: Coveralls
run: |
npm install coveralls
nyc report --reporter=text-lcov | coveralls
218 changes: 43 additions & 175 deletions demo/demo.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { openDB, wrap, unwrap } from 'idb';
// import { idbOpen, idbDelete } from 'idb-open';
const { idbOpen, idbDelete } = window.idbOpen;
class OsCmd1 {
constructor(dbName, store) {
import { openDB, wrap, unwrap } from "idb";
import { idbOpen, idbDelete } from "../src/";
// const { idbOpen, idbDelete } = window.idbOpen;

window.idbOpen = idbOpen;

/**
* 实现给我和简单人kv库
*/
class OsCmd {
constructor(dbName, storeName) {
this.options = {
dbName,
store,
store: storeName,
};
this.open = () => idbOpen(dbName, { store });
this.open = () => idbOpen(dbName, { store: storeName });
}
getos = async (module = "readwrite") => {
let { store } = this.options;
Expand All @@ -34,13 +40,16 @@ class OsCmd1 {
};
}

class OsCmd {
constructor(dbName, store) {
/**
* 搭配 idb 库使用变得更丝滑
*/
class OsCmd1 {
constructor(dbName, storeName) {
this.options = {
dbName,
store,
store: storeName,
};
this.open = () => idbOpen(dbName, { store });
this.open = () => idbOpen(dbName, { store: storeName });
}
getDB = async () => {
const db = await this.open();
Expand All @@ -56,19 +65,11 @@ class OsCmd {

class IndexOsCmd {
dbName = "db1";
store = "my-store";
upgradeneeded = (db) => {
if (!db.objectStoreNames.contains("my-store")) {
var objectStore = db.createObjectStore("my-store", {
keyPath: "id",
});
}
};
store = "my-store|id";
constructor() {
this.open = () =>
idbOpen(this.dbName, {
store: this.store,
upgradeneeded: this.upgradeneeded,
});
}
getDB = async () => {
Expand All @@ -86,52 +87,22 @@ class IndexOsCmd {
};
}

class IndexOsCmd1 {
dbName = "db1";
store = function (db, tc) {
if (!db.objectStoreNames.contains("my-store")) {
return false;
}
var transaction = tc || db.transaction("my-store", "readwrite");
var myStore = transaction.objectStore("my-store");
if (!myStore.indexNames.contains("idx_name")) {
return false;
}
return true;
};
upgradeneeded = function (db, transaction) {
if (!db.objectStoreNames.contains("my-store")) {
db.createObjectStore("my-store", { keyPath: "id" });
}
var myStore = transaction.objectStore("my-store");
myStore.createIndex("idx_name", "index_this", { unique: false });
};
class IndexOsCmd1 extends IndexOsCmd {
storeName = "my-store";
store = this.storeName + "|id,name";
constructor() {
this.open = () =>
idbOpen(this.dbName, {
store: this.store,
upgradeneeded: this.upgradeneeded,
});
super();
}
getDB = async () => {
const db = await this.open();
return wrap(db);
// return db;
};
get = async (id) => {
let db = await this.getDB();
return db.get("my-store", id);
};
set = async (obj) => {
findAll = async (name) => {
let db = await this.getDB();
return db.put("my-store", obj);
return db.getAllFromIndex("my-store", "name", IDBKeyRange.only(name));
};
}

async function main() {
async function demo1() {
await idbDelete("db1");
const cmd1 = new OsCmd("db1", "os1");
const cmd2 = new OsCmd("db1", "os2");
const cmd2 = new OsCmd1("db1", "os2");
Promise.all([cmd1.get("key"), cmd2.get("key")]).then(console.log);
cmd1.set(["cmd1"], "key");
cmd2.set(["cmd2"], "key");
Expand All @@ -154,10 +125,10 @@ async function main() {
await idbDelete("db1");
console.log("cmd1>key", await cmd1.get("key"));
console.log("cmd2>key", await cmd2.get("key"));
console.log("main end");
console.log("demo1 end");
}

async function main1() {
async function demo2() {
await idbDelete("db1");
// const cmd = new IndexOsCmd();
// console.log(await cmd.getDB());
Expand All @@ -172,125 +143,22 @@ async function main1() {
// }
// console.log(await cmd.get('id'));
const cmd = new IndexOsCmd();
let k1 = cmd.set({ id: "id", index_this: "val" });
let k1 = cmd.set({ id: "id", name: "val" });
const cmd1 = new IndexOsCmd1();
let C1 = cmd1.get("id");
let k2 = cmd.set({ id: "id1", index_this: "val1" });
let k3 = cmd.set({ id: "id2", index_this: "val2" });
let k2 = cmd.set({ id: "id1", name: "val1" });
let k3 = cmd.set({ id: "id2", name: "val2" });
k1;
console.log(await C1);
await k1;
await k2;
await k3;
k2;
k3;
console.log(await cmd1.findAll("val2"));
console.log(await cmd1.get("id2"));
console.log(await idbOpen("dbdbdb1"));
console.log("main1 end");
cmd.set({ id: "id3", name: "val2" });
console.log(await cmd1.findAll("val2"));
console.log("demo2 end");
}

class Friended {
dbName = "db1";
store = "my-friend";
indexes = "++,name";
constructor(indexes) {
this.open = () => {
const [primary, ...other] = (indexes || this.indexes).split(",");
const store = this.store;
return idbOpen(this.dbName, {
// store: {
// [this.store]: indexes || this.indexes,
// },
store: `${this.store}|${indexes || this.indexes}`,
// store: (db, transaction) => {
// if (!db.objectStoreNames.contains(store)) {
// return function () {
// const name = primary.replace(/\+\+/g, "");
// const options = {
// autoIncrement: /^\+\+/.test(primary),
// };
// if (name) options.keyPath = name;
// const ost = db.createObjectStore(store, options);
// other.forEach((idx) => {
// ost.createIndex(idx, idx, {
// unique: false,
// multiEntry: true,
// });
// });
// };
// } else {
// function updateIndex() {
// let ost = transaction.objectStore(store);
// let indexNames = [...ost.indexNames];
// other.forEach((idx) => {
// if (!indexNames.includes(idx)) {
// ost.createIndex(idx, idx, {
// unique: false,
// multiEntry: true,
// });
// }
// });
// }
// let ost = transaction.objectStore(store);
// let indexNames = [...ost.indexNames];
// // if(other)
// let useUp = other.some(idx=>{
// return !indexNames.includes(idx);
// })
// if(useUp) return updateIndex;
// }
// },
});
};
}
getDB = async () => {
const db = await this.open();
return wrap(db);
// return db;
};
get = async (id) => {
let db = await this.getDB();
return db.get(this.store, id);
};
set = async (obj) => {
let db = await this.getDB();
return db.put(this.store, obj);
};
find = async (name) => {
let db = await this.getDB();
return db
.transaction(this.store)
.objectStore(this.store)
.index("name")
.getAll(IDBKeyRange.only(name));
};
getAll = async (name) => {
let db = await this.getDB();
return db.getAll(this.store);
};
}
await demo1();

async function main2() {
try {
await idbDelete("db1");
let f1 = new Friended();
await f1.set({ name: "why" });
console.log(await f1.getAll());
await f1.set({ name: "why1" });
await f1.set({ name: "why" });
await f1.set({ name: "why" });
let f2 = new Friended("++,name,sex");
await f2.set({ name: "why", sex: 1 });
console.log("f2 all", await f2.getAll());
console.log("f1 all", await f1.getAll());
console.log("f1 why", await f1.find("why"));
console.log("f2 why", await f2.find("why"));
let f3 = new Friended("++,old");
await f3.set({ name: "why", old: 36 });
console.log("f3 why", await f3.getAll());
console.log("f3 why", await f3.find("why"));
} catch (e) {
console.error(e);
}
}
await main2();
// console.log(await idbOpen('dbdbdb1'))
// await main();
// await main1();
await demo2();
1 change: 0 additions & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<!DOCTYPE html>
<script src="./dist/idb-open.umd.js"></script>
<div id="mocha"></div>
<script type="module" src="demo.js"></script>
15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,17 @@
"dist"
],
"scripts": {
"clean": "rimraf lib dist",
"prebuild": "npm run clean",
"build": "npm run build:vite",
"build:rollup": "tsc && rollup -c --bundleConfigAsCjs",
"build:vite": "tsc && vite build",
"dev": "tsc && concurrently \"tsc --watch --preserveWatchOutput\" \"npm run server\" ",
"server": "vite",
"test": "node ./test/test.js",
"coverage": "npm test -- --coverage",
"clean": "rimraf lib dist",
"demo": "node ./script/puppeteer ./demo",
"prebuild": "npm run clean",
"test": "node ./script/test.js",
"test:speed": "node ./script/puppeteer ./test/speedTest",
"coverage": "npm test -- --coverage --noOpen",
"coverage:auto": "npm test -- --coverage",
"test-server": "vite ./test --port 3300",
"demo": "vite ./demo",
"test-polendina": "polendina --coverage test/bdd.js",
"prepack": "npm test && npm run build"
},
"keywords": [
Expand Down
56 changes: 56 additions & 0 deletions script/puppeteer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
const path = require("path");
const puppeteer = require("puppeteer");
const { createServer } = require("vite");
let args = process.argv.slice(2) || [];
let root = process.cwd();
let argPath = args.find(str=>{
return !/^\-\-/.test(str);
})
if(argPath){
root = path.join(process.cwd(),argPath)
}
console.log(root);

const startServer = async () => {
const server = await createServer({
configFile: false,
root: root,
// server: {
// port: port,
// },
});
await server.listen();
server.printUrls();
return server;
};

let outLog = Promise.resolve();
const onConsole = async (e) => {
const args = Promise.all(e.args().map((a) => a.jsonValue()));
outLog = outLog.then(() => args);
runConsole(args);
};
const runConsole = async (args) => {
await outLog;
console.log(...(await args));
};

async function main() {
const server = await startServer();
const serverPort = server.config.server.port || port;
const browser = await puppeteer.launch({
args: ["--no-sandbox"],
headless: "new",
});
const page = await browser.newPage();
page.on("error", (error) => console.error(error));
page.on("pageerror", (error) => console.error(error));
page.on("console", onConsole);
await page.exposeFunction("__testEnd__", async (errors) => {
browser.close();
server.close();
});
await page.goto(`http://localhost:${serverPort}`, { waitUntil: "load" });
}

main();

0 comments on commit 27acc47

Please sign in to comment.