|
6 | 6 | "fmt" |
7 | 7 | "math/rand/v2" |
8 | 8 | "os" |
| 9 | + "runtime" |
9 | 10 | "strconv" |
10 | 11 | "strings" |
11 | 12 | "testing" |
@@ -94,21 +95,60 @@ func TestPoseidon2Bench(t *testing.T) { |
94 | 95 | t.FailNow() |
95 | 96 | } |
96 | 97 |
|
97 | | - results := make([]g.GoldilocksField, 0, 4*len(inputs)) |
98 | | - start := time.Now() |
99 | | - for _, input := range inputs { |
100 | | - res := poseidon2_plonky2.HashNToHashNoPad(input) |
101 | | - results = append(results, res[:]...) |
| 98 | + for i := 0; i < 10; i++ { |
| 99 | + PrintMemUsage() |
| 100 | + |
| 101 | + results := make([]g.GoldilocksField, 0, 4*len(inputs)) |
| 102 | + start := time.Now() |
| 103 | + for _, input := range inputs { |
| 104 | + res := poseidon2_plonky2.HashNToHashNoPad(input) |
| 105 | + results = append(results, res[:]...) |
| 106 | + } |
| 107 | + duration := time.Since(start) |
| 108 | + t.Logf("HashNToHashNoPad plonky2 took %s for %d inputs", duration, totalInputs) |
| 109 | + |
| 110 | + sha2 := sha256.New() |
| 111 | + for _, res := range results { |
| 112 | + sha2.Write(g.ToLittleEndianBytesF(res)) |
| 113 | + } |
| 114 | + t.Logf("Hash: %x\n", sha2.Sum(nil)) |
102 | 115 | } |
103 | | - duration := time.Since(start) |
104 | | - t.Logf("HashNToHashNoPad plonky2 took %s for %d inputs", duration, totalInputs) |
| 116 | +} |
105 | 117 |
|
106 | | - sha2 := sha256.New() |
107 | | - for _, res := range results { |
108 | | - sha2.Write(g.ToLittleEndianBytesF(res)) |
| 118 | +func TestPoseidon2HasherBench(t *testing.T) { |
| 119 | + inputs, err := readBenchInputsBytes("bench_vector") |
| 120 | + totalInputs := len(inputs) |
| 121 | + if err != nil { |
| 122 | + t.Logf("Error: %v\n", err) |
| 123 | + t.FailNow() |
109 | 124 | } |
110 | | - hash := sha2.Sum(nil) |
111 | | - t.Logf("Hash: %x\n", hash) |
| 125 | + |
| 126 | + hasher := poseidon2_plonky2.NewPoseidon2() |
| 127 | + start1 := time.Now() |
| 128 | + |
| 129 | + for i := 0; i < 10; i++ { |
| 130 | + PrintMemUsage() |
| 131 | + |
| 132 | + results := make([]byte, 0, 4*8*len(inputs)) |
| 133 | + start := time.Now() |
| 134 | + for _, input := range inputs { |
| 135 | + for _, b := range input { |
| 136 | + hasher.Write(b) |
| 137 | + } |
| 138 | + res := hasher.Sum(nil) |
| 139 | + hasher.Reset() |
| 140 | + results = append(results, res...) |
| 141 | + } |
| 142 | + duration := time.Since(start) |
| 143 | + t.Logf("Hasher plonky2 took %s for %d inputs", duration, totalInputs) |
| 144 | + |
| 145 | + sha2 := sha256.New() |
| 146 | + sha2.Write(results) |
| 147 | + t.Logf("Hash: %x\n", sha2.Sum(nil)) |
| 148 | + } |
| 149 | + |
| 150 | + duration := time.Since(start1) |
| 151 | + t.Logf("===> Hasher plonky2 took %s", duration) |
112 | 152 | } |
113 | 153 |
|
114 | 154 | func TestPoseidon2BenchOld(t *testing.T) { |
@@ -167,6 +207,37 @@ func readBenchInputs(filename string) ([][]g.GoldilocksField, error) { |
167 | 207 | return inputs, nil |
168 | 208 | } |
169 | 209 |
|
| 210 | +func readBenchInputsBytes(filename string) ([][][]byte, error) { |
| 211 | + file, err := os.Open(filename) |
| 212 | + if err != nil { |
| 213 | + return nil, fmt.Errorf("failed to open file: %v", err) |
| 214 | + } |
| 215 | + defer file.Close() |
| 216 | + |
| 217 | + scanner := bufio.NewScanner(file) |
| 218 | + var inputs [][][]byte |
| 219 | + |
| 220 | + for scanner.Scan() { |
| 221 | + line := scanner.Text() |
| 222 | + strVals := strings.Split(line, ",") |
| 223 | + var input [][]byte |
| 224 | + for _, strVal := range strVals { |
| 225 | + val, err := strconv.ParseUint(strVal, 10, 64) |
| 226 | + if err != nil { |
| 227 | + return nil, fmt.Errorf("failed to parse uint64: %v", err) |
| 228 | + } |
| 229 | + input = append(input, g.ToLittleEndianBytesF(g.GoldilocksField(val))) |
| 230 | + } |
| 231 | + inputs = append(inputs, input) |
| 232 | + } |
| 233 | + |
| 234 | + if err := scanner.Err(); err != nil { |
| 235 | + return nil, fmt.Errorf("failed to read file: %v", err) |
| 236 | + } |
| 237 | + |
| 238 | + return inputs, nil |
| 239 | +} |
| 240 | + |
170 | 241 | func readBenchInputsOld(filename string) ([][]g.Element, error) { |
171 | 242 | file, err := os.Open(filename) |
172 | 243 | if err != nil { |
@@ -197,3 +268,13 @@ func readBenchInputsOld(filename string) ([][]g.Element, error) { |
197 | 268 |
|
198 | 269 | return inputs, nil |
199 | 270 | } |
| 271 | + |
| 272 | +func PrintMemUsage() { |
| 273 | + var m runtime.MemStats |
| 274 | + runtime.ReadMemStats(&m) |
| 275 | + // For info on each, see: https://golang.org/pkg/runtime/#MemStats |
| 276 | + fmt.Printf("Alloc = %v Bytes", m.Alloc) |
| 277 | + fmt.Printf("\tTotalAlloc = %v Bytes", m.TotalAlloc) |
| 278 | + fmt.Printf("\tSys = %v Bytes", m.Sys) |
| 279 | + fmt.Printf("\tNumGC = %v\n", m.NumGC) |
| 280 | +} |
0 commit comments