/
tynisearch-performance.spec.ts
69 lines (55 loc) · 2.24 KB
/
tynisearch-performance.spec.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { TyniSearch } from "../src";
import { readFile } from "node:fs/promises";
// number of total dataset is about 700k
// if you want to tests the performance of this library
// change the variable numberOfWordsForTesting to what you want
describe("performance testing for trie", () => {
let tyniSearch = new TyniSearch();
let numberOfWordsForTesting = 5_000;
let partialSet: string[];
beforeAll(async () => {
const data = await readFile(`${__dirname}/dataset.txt`, "utf-8");
partialSet = data.split(/\r?\n/).slice(0, numberOfWordsForTesting);
console.log(
`Number of words pushed into trie: ${new Intl.NumberFormat().format(
numberOfWordsForTesting,
)}`,
);
});
it(`performance check - insert ${new Intl.NumberFormat().format(
numberOfWordsForTesting,
)} words`, () => {
tyniSearch.insert(partialSet);
console.log(
`performance check - insert ${new Intl.NumberFormat().format(
numberOfWordsForTesting,
)} words into trie, serialize`,
);
expect(tyniSearch.getAllKeywords().length).toEqual(numberOfWordsForTesting);
});
it("performance check - serde", () => {
const serialized = tyniSearch.serialize();
tyniSearch = TyniSearch.deserialize(serialized);
expect(tyniSearch).toBeInstanceOf(TyniSearch);
});
it("performance check - add a new word, build failure links again", () => {
tyniSearch.insert(["나랑드", "사이다"]);
});
it("performance check - search the word just before added", () => {
const result = tyniSearch.searchInSentence("나랑드 사이다 특가");
expect(result.sort()).toEqual(["나랑드", "사이다"].sort());
});
it("performance check - search keywords from random string", () => {
const result = tyniSearch.searchInSentence(
"agostino, al bud, and, alameda",
);
console.log(result);
expect(result.sort()).toEqual(["agostino", "al bud", "alameda"].sort());
});
it("performance check - delete, build failure link, and search the deleted keyword", () => {
tyniSearch.delete(["나랑드", "사이다"]);
const result = tyniSearch.searchInSentence("나랑드 사이다 특가");
expect(result).not.toContain("나랑드");
expect(result).not.toContain("사이다");
});
});