-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprobe.go
198 lines (164 loc) · 4.67 KB
/
probe.go
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
/*
The main functioning of the probe cli. Spawns multiple goroutines to asynchronously
run test cases against the solutions.
Author: Shravan Asati
Originially Written: 30 June 2021
Last Edited: 6 October 2021
*/
package main
import (
"fmt"
"os"
"runtime"
"sync"
serve "github.com/Ardent-Community/probe/services"
"github.com/olekukonko/tablewriter"
"github.com/thatisuday/commando"
)
const (
NAME string = "probe"
VERSION string = "0.1.0"
)
type processEntry struct {
username string
lang string
code string
testCasesFile string
}
type winner struct {
Username string `json:"username"`
Language string `json:"language"`
}
type winnerDB struct {
winners []*winner
sync.Mutex
}
func processor(entryCh *chan *processEntry, winners *winnerDB, doneCh chan struct{}) {
for entry := range *entryCh {
serve.Log("info", fmt.Sprintf("running %v's solution written in %v", entry.username, entry.lang))
t := serve.Tester{
Lang: entry.lang,
Code: entry.code,
TestCasesFile: entry.testCasesFile,
}
passed := t.PerformTests()
if passed {
serve.Log("success", fmt.Sprintf("%v's code passed", entry.username))
winners.Lock()
winners.winners = append(winners.winners, &winner{
Username: entry.username,
Language: entry.lang,
})
winners.Unlock()
} else {
serve.Log("failure", fmt.Sprintf("%v's code failed", entry.username))
}
doneCh <- struct{}{}
}
}
func tabulate(winners *winnerDB) {
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"Username", "Language"})
for _, winner := range winners.winners {
table.Append([]string{winner.Username, winner.Language})
}
table.Render()
}
func run(challengeNumber, testCasesFile string) {
// test solutions
solutions := map[string]map[string]string{
"username1": {
"language": "python",
"code": "def solution(n):print(n * n)",
},
"username2": {
"language": "javascript",
"code": "const solution = (n) => {console.log(n * n)}",
},
"wrong_username1": {
"language": "python",
"code": "def solution(n): print(n * 2)",
},
"wrong_username2": {
"language": "javascript",
"code": "const solution = (n) => {console.log(return n * n * 1)}",
},
"wrong_username3": {
"language": "python",
"code": "import time\nprint('haha')",
},
"wrong_username4": {
"language": "javascript",
"code": "import {time} from datetime;",
},
}
// todo uncomment this when the API is ready, and delete above solutions map
// solutions := serve.GetSolutions(challengeNumber).Solutions
// initialize winners db
winners := &winnerDB{
winners: []*winner{},
}
// initialize channels
entryCh := make(chan *processEntry)
// empty struct because it occupies zero memory, and this channel doesnt contain
// any information but only signifies that a message is passed
doneCh := make(chan struct{})
// spawn processor goroutines
maxProcs := runtime.NumCPU()
for i := 0; i < maxProcs; i++ {
go processor(&entryCh, winners, doneCh)
}
var wg sync.WaitGroup
wg.Add(1)
// listener goroutine
go func() {
for i := 0; i < len(solutions); i++ {
// fmt.Println("waiting")
<-doneCh
}
wg.Done()
}()
// send entries to be processed
for username, data := range solutions {
entryCh <- &processEntry{
username: username,
lang: data["language"],
code: data["code"],
testCasesFile: testCasesFile,
}
}
// closing entry channel
close(entryCh)
wg.Wait()
serve.ClearClutter()
serve.Log("info", "\nThe winners are:\n")
tabulate(winners)
}
func main() {
run("1", `./examples/testcases.json`)
// ? tune this setting to improve performance
// runtime.GOMAXPROCS(4)
fmt.Println(NAME, VERSION)
commando.
SetExecutableName(NAME).
SetVersion(VERSION).
SetDescription("probe is a CLI tool made to automate the process of solution validation for weekly challenges conducted in the Ardent-Community discord server.\n")
commando.
Register(nil).
SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
commando.Parse([]string{"help"})
})
commando.
Register("run").
SetShortDescription("Validates solutions.").
SetDescription("The `run` command does, in order, makes a request to the API, grabs the solutions, and concurrently runs all the solutions against the test cases.").
AddArgument("challengeNumber", "The challenge number to validate the solutions for.", "").
AddArgument("testCasesFile", "The path to the test cases file.", "").
SetAction(func(args map[string]commando.ArgValue, flags map[string]commando.FlagValue) {
run(
args["challengeNumber"].Value,
args["testCasesFile"].Value,
)
})
commando.Parse(nil)
}