File size: 6,213 Bytes
23a7a20 | 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 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | #!/usr/bin/env python3
"""Compute crawler workload statistics for evaluation."""
import os
import csv
import numpy as np
from transformers import AutoTokenizer
import argparse
from pathlib import Path
from tqdm import tqdm
from functools import partial
import multiprocessing as mp
def tokenize_text(text: str, tokenizer_model: str) -> int:
"""Tokenize text and return token count."""
try:
tokenizer = AutoTokenizer.from_pretrained(tokenizer_model,
local_files_only=True)
except:
tokenizer = AutoTokenizer.from_pretrained(tokenizer_model)
tokens = tokenizer.encode(text, truncation=False, add_special_tokens=True)
return len(tokens)
def process_query_trace(csv_file: str, tokenizer_model: str):
"""Process a single query trace file and return statistics."""
try:
tokenizer = AutoTokenizer.from_pretrained(tokenizer_model,
local_files_only=True)
except:
tokenizer = AutoTokenizer.from_pretrained(tokenizer_model)
total_tokens = 0
start_time = None
end_time = None
page_count = 0
try:
with open(csv_file, 'r') as f:
reader = csv.DictReader(f)
for row in reader:
if not row:
continue
# Count tokens in page content
if 'content' in row and row['content']:
tokens = tokenizer.encode(row['content'],
truncation=False,
add_special_tokens=True)
total_tokens += len(tokens)
# Track start and end times
if 'startTime' in row and row['startTime']:
try:
if start_time is None:
start_time = float(row['startTime'])
except:
pass
if 'endTime' in row and row['endTime']:
try:
end_time = float(row['endTime'])
except:
pass
page_count += 1
if page_count > 0:
total_time = 0.0
if start_time is not None and end_time is not None:
total_time = end_time - start_time
return {'total_tokens': total_tokens, 'total_time': total_time}
except Exception as e:
pass
return None
def main():
parser = argparse.ArgumentParser(
description="Compute crawler workload statistics")
parser.add_argument("--input-dir",
"-i",
default="traces/simpleQA_ALL",
help="Directory containing crawler trace CSV files")
parser.add_argument("--tokenizer-model",
"-t",
default="meta-llama/Llama-3.1-8B-Instruct",
help="HuggingFace tokenizer model")
parser.add_argument("--cores",
type=int,
default=100,
help="Number of CPU cores to use")
parser.add_argument("--max-queries",
type=int,
default=None,
help="Maximum number of queries to process")
parser.add_argument("--output-dir",
default="tables",
help="Output directory for statistics file")
args = parser.parse_args()
# Find all CSV files
input_dir = Path(args.input_dir)
csv_files = list(input_dir.glob("*.csv"))
if not csv_files:
print(f"No CSV files found in {args.input_dir}")
return
if args.max_queries:
csv_files = csv_files[:args.max_queries]
print(f"Found {len(csv_files)} query files")
print(f"Processing with {args.cores} cores...")
# Process files
worker_func = partial(process_query_trace,
tokenizer_model=args.tokenizer_model)
total_tokens_list = []
total_time_list = []
if args.cores == 1:
for csv_file in tqdm(csv_files, desc="Processing"):
result = worker_func(str(csv_file))
if result:
total_tokens_list.append(result['total_tokens'])
total_time_list.append(result['total_time'])
else:
with mp.Pool(args.cores) as pool:
results = list(
tqdm(pool.imap_unordered(worker_func,
[str(f) for f in csv_files]),
total=len(csv_files),
desc="Processing"))
for result in results:
if result:
total_tokens_list.append(result['total_tokens'])
total_time_list.append(result['total_time'])
# Compute statistics and save to file
os.makedirs(args.output_dir, exist_ok=True)
output_file = os.path.join(args.output_dir, "workload_stats_crawler.txt")
with open(output_file, 'w') as f:
f.write("\n" + "=" * 70 + "\n")
f.write("CRAWLER WORKLOAD STATISTICS\n")
f.write("=" * 70 + "\n")
if total_tokens_list:
total_tokens = np.array(total_tokens_list)
f.write(f"\nQuery Total Tokens (n={len(total_tokens)})\n")
f.write(f" Mean: {total_tokens.mean():.0f} tokens\n")
f.write(f" P50: {np.percentile(total_tokens, 50):.0f} tokens\n")
f.write(f" P75: {np.percentile(total_tokens, 75):.0f} tokens\n")
f.write(f" P95: {np.percentile(total_tokens, 95):.0f} tokens\n")
if total_time_list:
total_time = np.array(total_time_list)
f.write(f"\nTotal Collection Time (n={len(total_time)})\n")
f.write(f" Mean: {total_time.mean():.3f} seconds\n")
f.write(f" P50: {np.percentile(total_time, 50):.3f} seconds\n")
f.write(f" P75: {np.percentile(total_time, 75):.3f} seconds\n")
f.write(f" P95: {np.percentile(total_time, 95):.3f} seconds\n")
f.write("=" * 70 + "\n")
if __name__ == "__main__":
main()
|