|
| 1 | +package convex.core.examples; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | + |
| 5 | +import convex.core.cvm.State; |
| 6 | +import convex.core.data.ACell; |
| 7 | +import convex.core.data.Blob; |
| 8 | +import convex.core.data.Format; |
| 9 | +import convex.core.data.Refs; |
| 10 | +import convex.core.exceptions.BadFormatException; |
| 11 | +import convex.core.init.Init; |
| 12 | +import convex.core.text.Text; |
| 13 | +import convex.test.Samples; |
| 14 | + |
| 15 | +/** |
| 16 | + * Test class for encoding speed |
| 17 | + */ |
| 18 | +public class EncodingTrials { |
| 19 | + |
| 20 | + // Data file (printout of a genesis CVM state) |
| 21 | + static State s=Init.createState(List.of(Samples.KEY_PAIR.getAccountKey())); |
| 22 | + |
| 23 | + public static void main(String[] args) throws BadFormatException { |
| 24 | + System.out.println("State dumps"); |
| 25 | + runTrial(s,100); |
| 26 | + runTrial(s,100); |
| 27 | + runTrial(s,100); |
| 28 | + runTrial(s,100); |
| 29 | + runTrial(s,100); |
| 30 | + |
| 31 | + System.out.println("State reaps"); |
| 32 | + runTrial2(s,100); |
| 33 | + runTrial2(s,100); |
| 34 | + runTrial2(s,100); |
| 35 | + runTrial2(s,100); |
| 36 | + runTrial2(s,100); |
| 37 | + |
| 38 | + } |
| 39 | + |
| 40 | + @SuppressWarnings("unused") |
| 41 | + private static void runTrial(ACell data, long REPS) { |
| 42 | + long start=System.nanoTime(); |
| 43 | + Blob warmUp=Format.encodeMultiCell(data,true); |
| 44 | + long len=warmUp.count(); |
| 45 | + long cellCount=Refs.totalRefCount(s); |
| 46 | + for (int i=0; i<REPS; i++) { |
| 47 | + Blob b=Format.encodeMultiCell(data,true);; |
| 48 | + } |
| 49 | + long end=System.nanoTime(); |
| 50 | + |
| 51 | + double bps=(REPS*len)/(0.000000001*(end-start)); |
| 52 | + System.out.println("bytes/s: " +Text.toFriendlyNumber((long)bps)); |
| 53 | + } |
| 54 | + |
| 55 | + @SuppressWarnings("unused") |
| 56 | + private static void runTrial2(ACell data, long REPS) throws BadFormatException { |
| 57 | + long start=System.nanoTime(); |
| 58 | + Blob encoded=Format.encodeMultiCell(data,true); |
| 59 | + State warmUp=Format.decodeMultiCell(encoded); |
| 60 | + long len=encoded.count(); |
| 61 | + for (int i=0; i<REPS; i++) { |
| 62 | + State s2=Format.decodeMultiCell(encoded); |
| 63 | + } |
| 64 | + long end=System.nanoTime(); |
| 65 | + |
| 66 | + double bps=(REPS*len)/(0.000000001*(end-start)); |
| 67 | + System.out.println("bytes/s: " +Text.toFriendlyNumber((long)bps)); |
| 68 | + } |
| 69 | + |
| 70 | + |
| 71 | +} |
0 commit comments