| 50 | |
| 51 | |
| 52 | class Database: |
| 53 | @staticmethod |
| 54 | def from_file(path: Path, metadata: Optional[Any] = None): |
| 55 | assert path.is_file() |
| 56 | data = load_database_records(path) |
| 57 | return Database(path=path, data=data, metadata=metadata) |
| 58 | |
| 59 | @staticmethod |
| 60 | def empty(path: Path, metadata: Optional[Any] = None): |
| 61 | return Database(path=path, data={}, metadata=metadata) |
| 62 | |
| 63 | def __init__( |
| 64 | self, |
| 65 | path: Path, |
| 66 | data: Optional[Dict[Any, DatabaseRecord]] = None, |
| 67 | metadata: Optional[Dict[str, Any]] = None, |
| 68 | ): |
| 69 | self.path = path |
| 70 | self.data = data if data is not None else {} |
| 71 | self.metadata = metadata or {} |
| 72 | self.metadata["allocation"] = get_slurm_allocation_id() |
| 73 | |
| 74 | @property |
| 75 | def records(self) -> List[DatabaseRecord]: |
| 76 | return list(self.data.values()) |
| 77 | |
| 78 | def __contains__(self, key): |
| 79 | return key in self.data |
| 80 | |
| 81 | def has_record_for(self, identifier: BenchmarkIdentifier) -> bool: |
| 82 | return create_identifier_key(identifier) in self |
| 83 | |
| 84 | def store(self, identifier: BenchmarkIdentifier, result: BenchmarkResultRecord): |
| 85 | key = create_identifier_key(identifier) |
| 86 | assert key not in self |
| 87 | |
| 88 | record = DatabaseRecord( |
| 89 | uuid=uuid.uuid4().hex, |
| 90 | workload=identifier.workload, |
| 91 | workload_params=identifier.workload_params, |
| 92 | environment=identifier.environment, |
| 93 | environment_params=identifier.environment_params, |
| 94 | index=identifier.index, |
| 95 | benchmark_metadata=identifier.metadata, |
| 96 | duration=result.duration or np.nan, |
| 97 | metadata=self.metadata, |
| 98 | timeout=identifier.timeout, |
| 99 | timestamp=int(time.time()), |
| 100 | ) |
| 101 | self.data[key] = record |
| 102 | |
| 103 | def save(self): |
| 104 | ensure_directory(self.path.parent) |
| 105 | |
| 106 | data = defaultdict(list) |
| 107 | for record in self.data.values(): |
| 108 | data[UUID_KEY].append(record.uuid) |
| 109 | data[WORKLOAD_KEY].append(record.workload) |