comparison test/org/tmatesoft/hg/test/TestRevlog.java @ 584:ed243b668502

Conditionally enable effective patch merge alternative for revlog reading
author Artem Tikhomirov <tikhomirov.artem@gmail.com>
date Thu, 25 Apr 2013 16:08:17 +0200
parents 47dfa0ec7e35
children 73c20c648c1f
comparison
equal deleted inserted replaced
583:47dfa0ec7e35 584:ed243b668502
49 new TestRevlog().run(indexFile); 49 new TestRevlog().run(indexFile);
50 } 50 }
51 51
52 private void run(File indexFile) throws Exception { 52 private void run(File indexFile) throws Exception {
53 final boolean shallDumpDiff = Boolean.TRUE.booleanValue(); 53 final boolean shallDumpDiff = Boolean.TRUE.booleanValue();
54 final boolean thoroughCheck = Boolean.TRUE.booleanValue(); 54 final boolean thoroughCheck = Boolean.FALSE.booleanValue();
55 // 55 //
56 RevlogReader rr = new RevlogReader(indexFile); 56 RevlogReader rr = new RevlogReader(indexFile);
57 rr.init(true); 57 rr.init(true);
58 rr.needData(true); 58 rr.needData(true);
59 int startEntryIndex = 76507; // 150--87 59 int startEntryIndex = 76507 + 100; // 150--87
60 rr.startFrom(startEntryIndex); 60 rr.startFrom(startEntryIndex);
61 rr.readNext(); 61 rr.readNext();
62 final long s0 = System.currentTimeMillis();
62 ByteBuffer baseRevision = null; 63 ByteBuffer baseRevision = null;
63 if (rr.isPatch()) { 64 if (rr.isPatch()) {
64 byte[] cc = getRevisionTrueContent(indexFile.getParentFile(), rr.entryIndex, rr.linkRevision); 65 byte[] cc = getRevisionTrueContent(indexFile.getParentFile(), rr.entryIndex, rr.linkRevision);
65 baseRevision = ByteBuffer.wrap(cc); 66 baseRevision = ByteBuffer.wrap(cc);
66 } else { 67 } else {
70 } 71 }
71 ByteArrayDataAccess baseRevisionContent = new ByteArrayDataAccess(baseRevision.array(), baseRevision.arrayOffset(), baseRevision.remaining()); 72 ByteArrayDataAccess baseRevisionContent = new ByteArrayDataAccess(baseRevision.array(), baseRevision.arrayOffset(), baseRevision.remaining());
72 // 73 //
73 final long start = System.currentTimeMillis(); 74 final long start = System.currentTimeMillis();
74 int n = 1419; 75 int n = 1419;
75 Patch seqPatch = null, normalizedPatch = null; 76 Patch seqPatch = new Patch(false), normalizedPatch = new Patch(true);
76 while (rr.hasMore() && n-- > 0) { 77 while (rr.hasMore() && n-- > 0) {
77 rr.readNext(); 78 rr.readNext();
78 if (!rr.isPatch()) { 79 if (!rr.isPatch()) {
79 break; 80 break;
80 } 81 }
81 if (rr.getDataLength() == 0) { 82 if (rr.getDataLength() == 0) {
82 System.out.printf("Empty content of revision %d\n", rr.entryIndex); 83 System.out.printf("Empty content of revision %d\n", rr.entryIndex);
83 continue; 84 continue;
84 } 85 }
85 Patch p1 = createPatch(rr); 86 Patch p1 = createPatch(rr);
86 if (seqPatch != null) { 87 if (n < 1) {
87 if (n < 1) { 88 System.out.println("+" + p1);
88 System.out.println("+" + p1); 89 System.currentTimeMillis();
89 System.currentTimeMillis(); 90 }
90 }
91 seqPatch = seqPatch.apply(p1); 91 seqPatch = seqPatch.apply(p1);
92 Patch ppp = normalizedPatch.apply(p1); 92 normalizedPatch = normalizedPatch.apply(p1);
93 normalizedPatch = ppp.normalize(); 93 // if (n <= 1) {
94 if (n <= 1) { 94 // System.out.println("=" + seqPatch);
95 System.out.println("=" + seqPatch); 95 // }
96 } 96 // if (n == 0) {
97 if (n == 0) { 97 // System.out.println("A" + ppp);
98 System.out.println("A" + ppp); 98 // System.out.println("N" + normalizedPatch);
99 System.out.println("N" + normalizedPatch); 99 // normalizedPatch = ppp;
100 normalizedPatch = ppp; 100 // }
101 }
102 // 101 //
103 if (!thoroughCheck) { 102 if (!thoroughCheck) {
104 if (baseRevisionContent.length() + seqPatch.patchSizeDelta() != rr.actualLen) { 103 if (baseRevisionContent.length() + seqPatch.patchSizeDelta() != rr.actualLen) {
104 System.out.printf("Sequential patches:\tPatchRevision #%d (+%d, cset:%d) failed\n", rr.entryIndex, rr.entryIndex - startEntryIndex, rr.linkRevision);
105 }
106 if (baseRevisionContent.length() + normalizedPatch.patchSizeDelta() != rr.actualLen) {
107 System.out.printf("Normalized patches:\tPatchRevision #%d (+%d, cset:%d) failed\n", rr.entryIndex, rr.entryIndex - startEntryIndex, rr.linkRevision);
108 }
109 } else {
110 byte[] origin = getRevisionTrueContent(indexFile.getParentFile(), rr.entryIndex, rr.linkRevision);
111 try {
112 byte[] result1 = seqPatch.apply(baseRevisionContent, rr.actualLen);
113 if (!Arrays.equals(result1, origin)) {
105 System.out.printf("Sequential patches:\tPatchRevision #%d (+%d, cset:%d) failed\n", rr.entryIndex, rr.entryIndex - startEntryIndex, rr.linkRevision); 114 System.out.printf("Sequential patches:\tPatchRevision #%d (+%d, cset:%d) failed\n", rr.entryIndex, rr.entryIndex - startEntryIndex, rr.linkRevision);
106 } 115 }
107 if (baseRevisionContent.length() + normalizedPatch.patchSizeDelta() != rr.actualLen) { 116 } catch (ArrayIndexOutOfBoundsException ex) {
108 System.out.printf("Normalized patches:\tPatchRevision #%d (+%d, cset:%d) failed\n", rr.entryIndex, rr.entryIndex - startEntryIndex, rr.linkRevision); 117 System.err.printf("Failure at entry %d (+%d)\n", rr.entryIndex, rr.entryIndex - startEntryIndex);
109 } 118 ex.printStackTrace();
110 } else { 119 }
111 byte[] origin = getRevisionTrueContent(indexFile.getParentFile(), rr.entryIndex, rr.linkRevision); 120 // try {
112 try { 121 // byte[] result2 = normalizedPatch.apply(baseRevisionContent, rr.actualLen);
113 byte[] result1 = seqPatch.apply(baseRevisionContent, rr.actualLen); 122 // if (!Arrays.equals(result2, origin)) {
114 if (!Arrays.equals(result1, origin)) { 123 // System.out.printf("Normalized patches:\tPatchRevision #%d (+%d, cset:%d) failed\n", rr.entryIndex, rr.entryIndex - startEntryIndex, rr.linkRevision);
115 System.out.printf("Sequential patches:\tPatchRevision #%d (+%d, cset:%d) failed\n", rr.entryIndex, rr.entryIndex - startEntryIndex, rr.linkRevision); 124 // }
116 } 125 // } catch (ArrayIndexOutOfBoundsException ex) {
117 } catch (ArrayIndexOutOfBoundsException ex) { 126 // System.err.printf("Failure at entry %d (+%d)\n", rr.entryIndex, rr.entryIndex - startEntryIndex);
118 System.err.printf("Failure at entry %d (+%d)\n", rr.entryIndex, rr.entryIndex - startEntryIndex); 127 // ex.printStackTrace();
119 ex.printStackTrace(); 128 // }
120 }
121 try {
122 byte[] result2 = normalizedPatch.apply(baseRevisionContent, rr.actualLen);
123 if (!Arrays.equals(result2, origin)) {
124 System.out.printf("Normalized patches:\tPatchRevision #%d (+%d, cset:%d) failed\n", rr.entryIndex, rr.entryIndex - startEntryIndex, rr.linkRevision);
125 }
126 } catch (ArrayIndexOutOfBoundsException ex) {
127 System.err.printf("Failure at entry %d (+%d)\n", rr.entryIndex, rr.entryIndex - startEntryIndex);
128 ex.printStackTrace();
129 }
130 }
131 } else {
132 seqPatch = p1;
133 normalizedPatch = p1.normalize();
134 } 129 }
135 } 130 }
136 final long end1 = System.currentTimeMillis(); 131 final long end1 = System.currentTimeMillis();
137 // 132 //
138 // byte[] result = seqPatch.apply(baseRevisionContent, rr.actualLen); 133 byte[] result = seqPatch.apply(baseRevisionContent, rr.actualLen);
139 byte[] result = normalizedPatch.apply(baseRevisionContent, rr.actualLen); 134 // byte[] result = normalizedPatch.apply(baseRevisionContent, rr.actualLen);
140 final long end2 = System.currentTimeMillis(); 135 final long end2 = System.currentTimeMillis();
141 byte[] origin = getRevisionTrueContent(indexFile.getParentFile(), rr.entryIndex, rr.linkRevision); 136 byte[] origin = getRevisionTrueContent(indexFile.getParentFile(), rr.entryIndex, rr.linkRevision);
142 final long end3 = System.currentTimeMillis(); 137 final long end3 = System.currentTimeMillis();
143 rr.done(); 138 rr.done();
144 System.out.printf("Collected patches up to revision %d. Patches total: %d, last contains %d elements\n", rr.entryIndex, rr.entryIndex - startEntryIndex + 1, seqPatch.count()); 139 System.out.printf("Collected patches up to revision %d. Patches total: %d, sequentialPatch contains %d elements, normalized: %d\n", rr.entryIndex, rr.entryIndex - startEntryIndex + 1, seqPatch.count(), normalizedPatch.count());
145 if (!Arrays.equals(result, origin)) { 140 if (!Arrays.equals(result, origin)) {
146 if (shallDumpDiff) { 141 if (shallDumpDiff) {
147 diff(result, origin); 142 diff(result, origin);
148 dumpLineDifference(result, origin); 143 dumpLineDifference(result, origin);
149 } else { 144 } else {
150 System.out.println("FAILURE!"); 145 System.out.println("FAILURE!");
151 } 146 }
152 } else { 147 } else {
153 System.out.println("OK!"); 148 System.out.println("OK!");
154 System.out.printf("Iterate: %d ms, apply collected: %d ms, total=%d ms; Conventional: %d ms\n", (end1-start), (end2-end1), (end2-start), (end3-end2)); 149 System.out.printf("Iterate: %d ms, read base:%d, apply collected: %d ms, total=%d ms; Conventional: %d ms\n", (end1-start), (start-s0), (end2-end1), (end2-s0), (end3-end2));
155 } 150 }
156 Patch normalized = normalizedPatch; //seqPatch.normalize(); 151 Patch normalized = seqPatch.normalize();
157 System.out.printf("N%s\n%d => %d patch elements\n", normalized, seqPatch.count(), normalized.count()); 152 System.out.printf("N%s\n%d => %d patch elements\n", normalized, seqPatch.count(), normalized.count());
158 // System.out.println(rs); 153 // System.out.println(rs);
159 } 154 }
160 155
161 private void dumpLineDifference(byte[] result, byte[] origin) { 156 private void dumpLineDifference(byte[] result, byte[] origin) {