|
| 1 | +import { faker } from "@faker-js/faker"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Generate realistic user data using Faker |
| 5 | + * This is used for benchmark testing with data that resembles real-world usage |
| 6 | + */ |
| 7 | +export function generateRealisticData(targetSize: number): any { |
| 8 | + faker.seed(123); // Fixed seed for consistent benchmarks |
| 9 | + |
| 10 | + const users = []; |
| 11 | + const comments = []; |
| 12 | + const posts = []; |
| 13 | + |
| 14 | + let currentSize = 0; |
| 15 | + let id = 1; |
| 16 | + |
| 17 | + while (currentSize < targetSize) { |
| 18 | + // Generate realistic user |
| 19 | + const user = { |
| 20 | + id: faker.string.uuid(), |
| 21 | + firstName: faker.person.firstName(), |
| 22 | + lastName: faker.person.lastName(), |
| 23 | + email: faker.internet.email(), |
| 24 | + age: faker.number.int({ min: 18, max: 80 }), |
| 25 | + phone: faker.phone.number(), |
| 26 | + city: faker.location.city(), |
| 27 | + country: faker.location.country(), |
| 28 | + address: { |
| 29 | + street: faker.location.streetAddress(), |
| 30 | + zipCode: faker.location.zipCode(), |
| 31 | + state: faker.location.state(), |
| 32 | + }, |
| 33 | + company: faker.company.name(), |
| 34 | + jobTitle: faker.person.jobTitle(), |
| 35 | + registeredAt: faker.date.past({ years: 2 }).toISOString(), |
| 36 | + active: faker.datatype.boolean(), |
| 37 | + avatar: faker.image.avatar(), |
| 38 | + bio: faker.lorem.paragraph(), |
| 39 | + preferences: { |
| 40 | + notifications: faker.datatype.boolean(), |
| 41 | + newsletter: faker.datatype.boolean(), |
| 42 | + theme: faker.helpers.arrayElement(["dark", "light", "auto"]), |
| 43 | + language: faker.helpers.arrayElement(["en", "es", "fr", "de", "it"]), |
| 44 | + }, |
| 45 | + }; |
| 46 | + users.push(user); |
| 47 | + |
| 48 | + // Generate realistic post |
| 49 | + const post = { |
| 50 | + id: faker.string.uuid(), |
| 51 | + authorId: users[faker.number.int({ min: 0, max: users.length - 1 })]?.id, |
| 52 | + title: faker.lorem.sentence(), |
| 53 | + content: faker.lorem.paragraphs({ min: 2, max: 5 }), |
| 54 | + excerpt: faker.lorem.paragraph(), |
| 55 | + createdAt: faker.date.past({ years: 1 }).toISOString(), |
| 56 | + updatedAt: faker.date.recent({ days: 30 }).toISOString(), |
| 57 | + publishedAt: faker.date.recent({ days: 60 }).toISOString(), |
| 58 | + likes: faker.number.int({ min: 0, max: 1000 }), |
| 59 | + views: faker.number.int({ min: 0, max: 10000 }), |
| 60 | + shares: faker.number.int({ min: 0, max: 500 }), |
| 61 | + tags: faker.helpers.arrayElements( |
| 62 | + [ |
| 63 | + "technology", |
| 64 | + "science", |
| 65 | + "business", |
| 66 | + "health", |
| 67 | + "travel", |
| 68 | + "food", |
| 69 | + "sports", |
| 70 | + "entertainment", |
| 71 | + ], |
| 72 | + { min: 1, max: 4 }, |
| 73 | + ), |
| 74 | + category: faker.helpers.arrayElement([ |
| 75 | + "blog", |
| 76 | + "news", |
| 77 | + "tutorial", |
| 78 | + "review", |
| 79 | + ]), |
| 80 | + featured: faker.datatype.boolean(), |
| 81 | + metadata: { |
| 82 | + readTime: faker.number.int({ min: 1, max: 20 }), |
| 83 | + difficulty: faker.helpers.arrayElement([ |
| 84 | + "beginner", |
| 85 | + "intermediate", |
| 86 | + "advanced", |
| 87 | + ]), |
| 88 | + }, |
| 89 | + }; |
| 90 | + posts.push(post); |
| 91 | + |
| 92 | + // Generate realistic comment |
| 93 | + const comment = { |
| 94 | + id: faker.string.uuid(), |
| 95 | + postId: posts[faker.number.int({ min: 0, max: posts.length - 1 })]?.id, |
| 96 | + userId: users[faker.number.int({ min: 0, max: users.length - 1 })]?.id, |
| 97 | + text: faker.lorem.paragraph({ min: 1, max: 3 }), |
| 98 | + createdAt: faker.date.recent({ days: 7 }).toISOString(), |
| 99 | + upvotes: faker.number.int({ min: 0, max: 100 }), |
| 100 | + downvotes: faker.number.int({ min: 0, max: 50 }), |
| 101 | + edited: faker.datatype.boolean(), |
| 102 | + editedAt: faker.datatype.boolean() |
| 103 | + ? faker.date.recent({ days: 3 }).toISOString() |
| 104 | + : null, |
| 105 | + replies: faker.number.int({ min: 0, max: 10 }), |
| 106 | + }; |
| 107 | + comments.push(comment); |
| 108 | + |
| 109 | + currentSize = JSON.stringify({ users, posts, comments }).length; |
| 110 | + id++; |
| 111 | + } |
| 112 | + |
| 113 | + return { |
| 114 | + users, |
| 115 | + posts, |
| 116 | + comments, |
| 117 | + metadata: { |
| 118 | + totalRecords: id - 1, |
| 119 | + generatedAt: new Date().toISOString(), |
| 120 | + version: "1.0.0", |
| 121 | + }, |
| 122 | + }; |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * Generate realistic task/todo items using Faker |
| 127 | + */ |
| 128 | +export function generateRealisticTasks(count: number) { |
| 129 | + faker.seed(456); |
| 130 | + const tasks = []; |
| 131 | + |
| 132 | + for (let i = 0; i < count; i++) { |
| 133 | + const task = { |
| 134 | + id: faker.string.uuid(), |
| 135 | + title: faker.hacker.phrase(), |
| 136 | + description: faker.lorem.sentence(), |
| 137 | + status: faker.helpers.arrayElement([ |
| 138 | + "todo", |
| 139 | + "in-progress", |
| 140 | + "done", |
| 141 | + "blocked", |
| 142 | + ]), |
| 143 | + priority: faker.helpers.arrayElement(["low", "medium", "high", "urgent"]), |
| 144 | + assignee: faker.person.fullName(), |
| 145 | + tags: faker.helpers.arrayElements( |
| 146 | + ["bug", "feature", "improvement", "documentation"], |
| 147 | + { min: 0, max: 3 }, |
| 148 | + ), |
| 149 | + createdAt: faker.date.past({ years: 1 }).toISOString(), |
| 150 | + dueDate: faker.date.future({ years: 2 }).toISOString(), |
| 151 | + estimatedHours: faker.number.int({ min: 1, max: 40 }), |
| 152 | + completed: faker.datatype.boolean(), |
| 153 | + }; |
| 154 | + tasks.push(task); |
| 155 | + } |
| 156 | + |
| 157 | + return tasks; |
| 158 | +} |
| 159 | + |
| 160 | +/** |
| 161 | + * Generate realistic article text using Faker |
| 162 | + */ |
| 163 | +export function generateRealisticArticle(paragraphs: number = 10): string { |
| 164 | + faker.seed(789); |
| 165 | + return faker.lorem.paragraphs(paragraphs); |
| 166 | +} |
| 167 | + |
| 168 | +/** |
| 169 | + * Measure the size of content in bytes |
| 170 | + */ |
| 171 | +export function measureContentSize(content: any[]): number { |
| 172 | + return new TextEncoder().encode(JSON.stringify(content)).length; |
| 173 | +} |
0 commit comments