aboutsummaryrefslogtreecommitdiffstats
path: root/p2p/discv5/testimg/testimg.go
blob: dab918a77e24901867372e4c8a99f4b9cec91d13 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
package main

import (
    "bufio"
    "encoding/binary"
    "fmt"
    "image"
    "image/png"
    "math"
    "os"
    "sort"
    "strconv"

    "github.com/ethereum/go-ethereum/crypto"
)

var xs, ys, maxTime int
var minAbsTime, maxAbsTime int64

func set(pic *image.NRGBA, x, y, c, v int) {
    if v > 255 {
        v = 255
    }
    if x >= 0 && x < xs && y >= 0 && y < ys {
        pic.Pix[y*pic.Stride+x*4+c] = uint8(v)
    }
}

type nodeStats []struct{ wpSum, wpCnt, wpXcnt, regCnt, regXcnt uint64 }

type nodeInfo struct {
    maxMR  int
    topics map[string]struct{}
}

const (
    regStatDiv  = 60
    regStatYdiv = 30
)

type topicInfo struct {
    prefix    uint64
    nodes     uint64Slice
    nodeStats nodeStats
    nodeIdx   map[uint64]int
    pic, pic2 *image.NRGBA
    nodeRad   map[uint64]int
    regStats  []int
}

func main() {
    var nodes uint64Slice
    topics := make(map[string]*topicInfo)

    inputFile := "test.out"
    if len(os.Args) > 1 {
        inputFile = os.Args[1]
    }

    f, _ := os.Open(inputFile)
    scanner := bufio.NewScanner(f)
    scanner.Split(bufio.ScanWords)
    minAbsTime = math.MaxInt64
    for scanner.Scan() {
        w := scanner.Text()
        if w == "*N" {
            scanner.Scan()
            prefix, _ := strconv.ParseUint(scanner.Text(), 16, 64)
            nodes = append(nodes, prefix)
        }
        if w == "*R" {
            scanner.Scan()
            time, _ := strconv.ParseInt(scanner.Text(), 10, 64)
            if time > maxAbsTime {
                maxAbsTime = time
            }
            if time < minAbsTime {
                minAbsTime = time
            }
            scanner.Scan()
            topic := scanner.Text()
            if _, ok := topics[topic]; !ok {
                fmt.Println(topic)
                topicHash := crypto.Keccak256Hash([]byte(topic))
                topics[topic] = &topicInfo{prefix: binary.BigEndian.Uint64(topicHash[:8])}
            }
        }
    }
    f.Close()

    maxTime = int(maxAbsTime - minAbsTime)
    xs = maxTime / 10000
    ys = len(nodes)
    nodeIdx := make(map[uint64]int)
    for i, v := range nodes {
        nodeIdx[v] = i
    }
    nodeInfo := make([]nodeInfo, len(nodes))

    for _, t := range topics {
        t.nodes = make(uint64Slice, len(nodes))
        t.nodeStats = make(nodeStats, len(nodes))
        for i, v := range nodes {
            t.nodes[i] = v ^ t.prefix
        }
        sort.Sort(t.nodes)
        t.nodeIdx = make(map[uint64]int)
        for i, v := range t.nodes {
            t.nodeIdx[v^t.prefix] = i
        }

        t.pic = image.NewNRGBA(image.Rect(0, 0, xs, ys))
        for y := 0; y < ys; y++ {
            for x := 0; x < xs; x++ {
                set(t.pic, x, y, 3, 255)
            }
        }

        t.pic2 = image.NewNRGBA(image.Rect(0, 0, xs, ys))
        for y := 0; y < ys; y++ {
            for x := 0; x < xs; x++ {
                set(t.pic2, x, y, 3, 255)
            }
        }
        t.nodeRad = make(map[uint64]int)
        t.regStats = make([]int, xs/regStatDiv+1)
    }

    f, _ = os.Open(inputFile)
    scanner = bufio.NewScanner(f)
    scanner.Split(bufio.ScanWords)
    statBegin := int64(40000000)
    statEnd := int64(maxTime - 10000000)

    for scanner.Scan() {
        w := scanner.Text()
        if w == "*R" {
            scanner.Scan()
            time, _ := strconv.ParseInt(scanner.Text(), 10, 64)
            time -= minAbsTime
            scanner.Scan()
            t := topics[scanner.Text()]
            scanner.Scan()
            prefix, _ := strconv.ParseUint(scanner.Text(), 16, 64)
            scanner.Scan()
            rad, _ := strconv.ParseInt(scanner.Text(), 10, 64)
            if int(rad) != t.nodeRad[prefix] {
                t.nodeRad[prefix] = int(rad)
                radUint := uint64(rad) * ((^uint64(0)) / 1000000)
                x := int(time * int64(xs) / int64(maxTime))
                y := sort.Search(ys, func(i int) bool {
                    return t.nodes[i] > radUint
                })
                set(t.pic, x, y, 1, 255)
            }
        }
        if w == "*MR" {
            scanner.Scan()
            time, _ := strconv.ParseInt(scanner.Text(), 10, 64)
            time -= minAbsTime
            scanner.Scan()
            topic := scanner.Text()
            t := topics[topic]
            scanner.Scan()
            prefix, _ := strconv.ParseUint(scanner.Text(), 16, 64)
            scanner.Scan()
            rad, _ := strconv.ParseInt(scanner.Text(), 10, 64)
            radUint := uint64(rad) * ((^uint64(0)) / 1000000)
            x := int(time * int64(xs) / int64(maxTime))
            y := sort.Search(ys, func(i int) bool {
                return t.nodes[i] > radUint
            })
            set(t.pic, x, y, 0, 255)
            ni := nodeInfo[nodeIdx[prefix]]
            if int(rad) > ni.maxMR {
                ni.maxMR = int(rad)
                if ni.topics == nil {
                    ni.topics = make(map[string]struct{})
                }
                ni.topics[topic] = struct{}{}
            }
            nodeInfo[nodeIdx[prefix]] = ni
        }
        if w == "*W" {
            scanner.Scan()
            time, _ := strconv.ParseInt(scanner.Text(), 10, 64)
            time -= minAbsTime
            scanner.Scan()
            t := topics[scanner.Text()]
            scanner.Scan()
            prefix, _ := strconv.ParseUint(scanner.Text(), 16, 64)
            scanner.Scan()
            wp, _ := strconv.ParseInt(scanner.Text(), 10, 64)
            x := int(time * int64(xs) / int64(maxTime))
            y := t.nodeIdx[prefix]
            if time >= statBegin && time < statEnd {
                t.nodeStats[y].wpSum += uint64(wp)
                if wp >= 600000 {
                    t.nodeStats[y].wpXcnt++
                }
                t.nodeStats[y].wpCnt++
            }
            /*set(t.pic2, x, y, 0, int(wp/100000))
            set(t.pic2, x, y, 1, int(wp/10000))
            set(t.pic2, x, y, 2, int(wp/1000))*/
            if wp >= 1800000 {
                set(t.pic2, x, y, 0, 255)
            }
            if wp >= 600000 {
                set(t.pic2, x, y, 1, 255)
            }
            if wp >= 60000 {
                set(t.pic2, x, y, 2, 255)
            }
        }
        if w == "*+" {
            scanner.Scan()
            time, _ := strconv.ParseInt(scanner.Text(), 10, 64)
            time -= minAbsTime
            scanner.Scan()
            t := topics[scanner.Text()]
            scanner.Scan()
            prefix, _ := strconv.ParseUint(scanner.Text(), 16, 64)
            x := int(time * int64(xs) / int64(maxTime))
            if x < xs {
                t.regStats[x/regStatDiv]++
            }
            y := t.nodeIdx[prefix]
            set(t.pic, x, y, 2, 255)
            scanner.Scan()
            prefix2, _ := strconv.ParseUint(scanner.Text(), 16, 64)
            y2 := t.nodeIdx[prefix2]
            if time >= statBegin && time < statEnd {
                t.nodeStats[y].regCnt++
                t.nodeStats[y2].regXcnt++
            }
        }
    }
    f.Close()

    for tt, t := range topics {
        f, _ = os.Create("test_" + tt + ".png")
        w := bufio.NewWriter(f)
        png.Encode(w, t.pic)
        w.Flush()
        f.Close()

        for x := 0; x < xs; x++ {
            yy := t.regStats[x/regStatDiv] / regStatYdiv
            if yy > ys {
                yy = ys
            }
            for y := 0; y < yy; y++ {
                set(t.pic2, x, ys-1-y, 1, 255)
            }
        }

        f, _ = os.Create("test2_" + tt + ".png")
        w = bufio.NewWriter(f)
        png.Encode(w, t.pic2)
        w.Flush()
        f.Close()

        if statEnd > statBegin {
            xxs := len(t.nodeStats)
            yys := 1000
            yyh := yys / 2
            pic3 := image.NewNRGBA(image.Rect(0, 0, xxs, yys))
            for y := 0; y < yys; y++ {
                for x := 0; x < xxs; x++ {
                    set(pic3, x, y, 3, 255)
                }
            }
            for x := 0; x < xxs; x++ {
                wpy := 0
                if t.nodeStats[x].wpCnt > 0 {
                    //                  wpy = int(t.nodeStats[x].wpSum / t.nodeStats[x].wpCnt / 10000)
                    wpy = int(uint64(yyh) * t.nodeStats[x].wpXcnt / t.nodeStats[x].wpCnt)
                }
                if wpy > yyh {
                    wpy = yyh
                }
                for y := 0; y < wpy; y++ {
                    set(pic3, x, yys-1-y, 1, 255)
                }
                regy := int(t.nodeStats[x].regCnt * 2400000 / uint64(statEnd-statBegin))
                if regy > yyh {
                    regy = yyh
                }
                for y := 0; y < regy; y++ {
                    set(pic3, x, yyh-1-y, 2, 255)
                }
                regy2 := int(t.nodeStats[x].regXcnt * 2400000 / uint64(statEnd-statBegin))
                if regy2 > yyh {
                    regy2 = yyh
                }
                for y := 0; y < regy2; y++ {
                    set(pic3, x, yyh-1-y, 0, 255)
                }
            }

            f, _ = os.Create("test3_" + tt + ".png")
            w = bufio.NewWriter(f)
            png.Encode(w, pic3)
            w.Flush()
            f.Close()
        }
    }

    for i, ni := range nodeInfo {
        fmt.Printf("%d %016x  maxMR = %d  ", i, nodes[i], ni.maxMR)
        for t, _ := range ni.topics {
            fmt.Printf(" %s", t)
        }
        fmt.Println()
    }
}

type uint64Slice []uint64

// Len is the number of elements in the collection.
func (s uint64Slice) Len() int {
    return len(s)
}

// Less reports whether the element with
// index i should sort before the element with index j.
func (s uint64Slice) Less(i, j int) bool {
    return s[i] < s[j]
}

// Swap swaps the elements with indexes i and j.
func (s uint64Slice) Swap(i, j int) {
    s[i], s[j] = s[j], s[i]
}