|
| 1 | +import { LoadTestConfig } from "../types"; |
| 2 | +import { formatEthFromWeiString } from "../utils/formatters"; |
| 3 | +import StatCard from "./StatCard"; |
| 4 | + |
| 5 | +interface ConfigCardProps { |
| 6 | + config: LoadTestConfig; |
| 7 | +} |
| 8 | + |
| 9 | +interface Row { |
| 10 | + label: string; |
| 11 | + value: string; |
| 12 | +} |
| 13 | + |
| 14 | +const formatTransactions = (txs: LoadTestConfig["transactions"]): string => { |
| 15 | + if (!txs || txs.length === 0) return "—"; |
| 16 | + const total = txs.reduce((acc, t) => acc + t.weight, 0); |
| 17 | + if (total === 0) return txs.map((t) => t.type).join(" · "); |
| 18 | + return txs |
| 19 | + .map((t) => `${t.type} (${Math.round((t.weight / total) * 100)}%)`) |
| 20 | + .join(" · "); |
| 21 | +}; |
| 22 | + |
| 23 | +const formatTargetGps = (gps: number): string => { |
| 24 | + if (gps >= 1e9) return `${(gps / 1e9).toFixed(1)}B gas/s`; |
| 25 | + if (gps >= 1e6) return `${(gps / 1e6).toFixed(0)}M gas/s`; |
| 26 | + if (gps >= 1e3) return `${(gps / 1e3).toFixed(0)}k gas/s`; |
| 27 | + return `${gps.toLocaleString()} gas/s`; |
| 28 | +}; |
| 29 | + |
| 30 | +const buildRows = (config: LoadTestConfig): Row[][] => { |
| 31 | + const loadShape: Row[] = [ |
| 32 | + { label: "Senders", value: config.sender_count.toLocaleString() }, |
| 33 | + { |
| 34 | + label: "In-flight / sender", |
| 35 | + value: config.in_flight_per_sender.toLocaleString(), |
| 36 | + }, |
| 37 | + { label: "Batch size", value: config.batch_size.toLocaleString() }, |
| 38 | + { label: "Batch timeout", value: config.batch_timeout }, |
| 39 | + ]; |
| 40 | + if (config.sender_offset !== 0) { |
| 41 | + loadShape.push({ |
| 42 | + label: "Sender offset", |
| 43 | + value: config.sender_offset.toLocaleString(), |
| 44 | + }); |
| 45 | + } |
| 46 | + |
| 47 | + const target: Row[] = [ |
| 48 | + { label: "Duration", value: config.duration }, |
| 49 | + { label: "Target gas/s", value: formatTargetGps(config.target_gps) }, |
| 50 | + ]; |
| 51 | + |
| 52 | + const funding: Row[] = [ |
| 53 | + { |
| 54 | + label: "Funding / sender", |
| 55 | + value: formatEthFromWeiString(config.funding_amount), |
| 56 | + }, |
| 57 | + ]; |
| 58 | + const hasSwapToken = |
| 59 | + config.transactions.some((t) => t.type === "swap") && |
| 60 | + config.swap_token_amount && |
| 61 | + config.swap_token_amount !== "0"; |
| 62 | + if (hasSwapToken) { |
| 63 | + funding.push({ |
| 64 | + label: "Swap token amount", |
| 65 | + value: formatEthFromWeiString(config.swap_token_amount), |
| 66 | + }); |
| 67 | + } |
| 68 | + |
| 69 | + const repro: Row[] = [{ label: "Seed", value: config.seed.toLocaleString() }]; |
| 70 | + if (config.chain_id !== null) { |
| 71 | + repro.push({ label: "Chain ID", value: config.chain_id.toLocaleString() }); |
| 72 | + } |
| 73 | + if (config.looper_contract) { |
| 74 | + repro.push({ label: "Looper contract", value: config.looper_contract }); |
| 75 | + } |
| 76 | + |
| 77 | + return [loadShape, target, funding, repro]; |
| 78 | +}; |
| 79 | + |
| 80 | +const RowGroup = ({ rows }: { rows: Row[] }) => ( |
| 81 | + <div className="grid grid-cols-2 gap-x-6 gap-y-3"> |
| 82 | + {rows.map((r) => ( |
| 83 | + <div key={r.label} className="flex flex-col"> |
| 84 | + <span className="text-xs uppercase tracking-wide text-slate-500"> |
| 85 | + {r.label} |
| 86 | + </span> |
| 87 | + <span className="text-sm text-slate-900 font-mono mt-0.5 break-all"> |
| 88 | + {r.value} |
| 89 | + </span> |
| 90 | + </div> |
| 91 | + ))} |
| 92 | + </div> |
| 93 | +); |
| 94 | + |
| 95 | +const ConfigCard = ({ config }: ConfigCardProps) => { |
| 96 | + const groups = buildRows(config); |
| 97 | + const txLine = formatTransactions(config.transactions); |
| 98 | + |
| 99 | + return ( |
| 100 | + <StatCard title="Run config"> |
| 101 | + <div className="flex flex-col gap-y-5"> |
| 102 | + {groups.map((rows, i) => ( |
| 103 | + <div key={i}> |
| 104 | + {i > 0 && <hr className="border-slate-100 mb-5" />} |
| 105 | + <RowGroup rows={rows} /> |
| 106 | + </div> |
| 107 | + ))} |
| 108 | + <hr className="border-slate-100" /> |
| 109 | + <div className="flex flex-col"> |
| 110 | + <span className="text-xs uppercase tracking-wide text-slate-500"> |
| 111 | + Workload |
| 112 | + </span> |
| 113 | + <span className="text-sm text-slate-900 mt-0.5">{txLine}</span> |
| 114 | + </div> |
| 115 | + </div> |
| 116 | + </StatCard> |
| 117 | + ); |
| 118 | +}; |
| 119 | + |
| 120 | +export default ConfigCard; |
0 commit comments