feat(assessment): add immutable family versions and the demo bank loader
This commit is contained in:
1 parent
94488e2b57
commit
204c6bae62
28 files changed
+6269
-9
No files matched your search
@@ -0,0 +1,246 @@
|
||||
package domain
|
||||
|
||||
import (
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// Registered seed algorithms. Each one draws an input from the seed and
|
||||
// computes the reference answer with its solver. The names carry a version;
|
||||
// see assessmentAlgorithm.
|
||||
var assessmentAlgorithms = map[string]assessmentAlgorithm{
|
||||
"rate_limit_window.v1": {
|
||||
answerFormat: AnswerFormatInteger,
|
||||
parameters: []assessmentParameterSpec{
|
||||
{name: "requests", lowest: 1, highest: 500},
|
||||
{name: "window", lowest: 1, highest: 1000},
|
||||
{name: "limit", lowest: 1, highest: 100},
|
||||
{name: "max_gap", lowest: 0, highest: 100},
|
||||
},
|
||||
dataNames: []string{"timestamps"},
|
||||
requiredNames: []string{"timestamps", "window", "limit"},
|
||||
generate: generateRateLimitWindow,
|
||||
},
|
||||
"dedup_window.v1": {
|
||||
answerFormat: AnswerFormatInteger,
|
||||
parameters: []assessmentParameterSpec{
|
||||
{name: "events", lowest: 1, highest: 500},
|
||||
{name: "keys", lowest: 1, highest: 26},
|
||||
{name: "ttl", lowest: 1, highest: 1000},
|
||||
{name: "max_gap", lowest: 0, highest: 100},
|
||||
},
|
||||
dataNames: []string{"stream"},
|
||||
requiredNames: []string{"stream", "ttl"},
|
||||
generate: generateDedupWindow,
|
||||
},
|
||||
"lru_hits.v1": {
|
||||
answerFormat: AnswerFormatInteger,
|
||||
parameters: []assessmentParameterSpec{
|
||||
{name: "accesses", lowest: 1, highest: 500},
|
||||
{name: "keys", lowest: 1, highest: 100},
|
||||
{name: "capacity", lowest: 1, highest: 50},
|
||||
},
|
||||
dataNames: []string{"sequence"},
|
||||
requiredNames: []string{"sequence", "capacity"},
|
||||
generate: generateLRUHits,
|
||||
},
|
||||
"dependency_order.v1": {
|
||||
answerFormat: AnswerFormatIntegerSequence,
|
||||
parameters: []assessmentParameterSpec{
|
||||
{name: "tasks", lowest: 2, highest: 20},
|
||||
{name: "links", lowest: 0, highest: 60},
|
||||
},
|
||||
dataNames: []string{"dependencies"},
|
||||
requiredNames: []string{"dependencies", "tasks"},
|
||||
generate: generateDependencyOrder,
|
||||
},
|
||||
}
|
||||
|
||||
// keyLetters names the keys of the duplicate-window stream; the algorithm
|
||||
// allows at most this many keys.
|
||||
const keyLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
|
||||
// ascendingTimes returns count non-decreasing times. The first is a draw from
|
||||
// 0 to maxGap and every next one adds another such draw to its predecessor.
|
||||
func ascendingTimes(random *seededRandom, count, maxGap int32) []int {
|
||||
times := make([]int, count)
|
||||
current := 0
|
||||
for index := range times {
|
||||
current += int(random.between(0, maxGap))
|
||||
times[index] = current
|
||||
}
|
||||
return times
|
||||
}
|
||||
|
||||
// joinInts writes values as decimal numbers separated by single spaces.
|
||||
func joinInts(values []int) string {
|
||||
parts := make([]string, len(values))
|
||||
for index, value := range values {
|
||||
parts[index] = strconv.Itoa(value)
|
||||
}
|
||||
return strings.Join(parts, " ")
|
||||
}
|
||||
|
||||
// Data names: timestamps.
|
||||
func generateRateLimitWindow(random *seededRandom, parameters map[string]int32) (map[string]string, string) {
|
||||
times := ascendingTimes(random, parameters["requests"], parameters["max_gap"])
|
||||
rejected := rateLimitRejected(times, int(parameters["window"]), int(parameters["limit"]))
|
||||
return map[string]string{"timestamps": joinInts(times)}, strconv.Itoa(rejected)
|
||||
}
|
||||
|
||||
// rateLimitRejected counts requests that a sliding-window limiter rejects. A
|
||||
// request at time t is accepted when fewer than limit accepted requests lie
|
||||
// in the half-open interval (t-window, t]; rejected requests are not
|
||||
// remembered. Times are non-decreasing.
|
||||
func rateLimitRejected(times []int, window, limit int) int {
|
||||
accepted := make([]int, 0, len(times))
|
||||
oldest, rejected := 0, 0
|
||||
for _, moment := range times {
|
||||
for oldest < len(accepted) && accepted[oldest] <= moment-window {
|
||||
oldest++
|
||||
}
|
||||
if len(accepted)-oldest < limit {
|
||||
accepted = append(accepted, moment)
|
||||
} else {
|
||||
rejected++
|
||||
}
|
||||
}
|
||||
return rejected
|
||||
}
|
||||
|
||||
// Data names: stream.
|
||||
func generateDedupWindow(random *seededRandom, parameters map[string]int32) (map[string]string, string) {
|
||||
times := ascendingTimes(random, parameters["events"], parameters["max_gap"])
|
||||
keys := make([]int, len(times))
|
||||
events := make([]string, len(times))
|
||||
for index, moment := range times {
|
||||
keys[index] = random.below(int(parameters["keys"]))
|
||||
events[index] = keyLetters[keys[index]:keys[index]+1] + "@" + strconv.Itoa(moment)
|
||||
}
|
||||
processed := dedupProcessed(keys, times, int(parameters["ttl"]))
|
||||
return map[string]string{"stream": strings.Join(events, " ")}, strconv.Itoa(processed)
|
||||
}
|
||||
|
||||
// dedupProcessed counts events that an idempotent consumer processes. An
|
||||
// event is dropped as a duplicate when its key was processed less than ttl
|
||||
// time units earlier; a dropped event does not extend that period. Times are
|
||||
// non-decreasing.
|
||||
func dedupProcessed(keys, times []int, ttl int) int {
|
||||
lastProcessed := make(map[int]int, len(keys))
|
||||
processed := 0
|
||||
for index, key := range keys {
|
||||
last, seen := lastProcessed[key]
|
||||
if seen && times[index]-last < ttl {
|
||||
continue
|
||||
}
|
||||
lastProcessed[key] = times[index]
|
||||
processed++
|
||||
}
|
||||
return processed
|
||||
}
|
||||
|
||||
// Data names: sequence.
|
||||
func generateLRUHits(random *seededRandom, parameters map[string]int32) (map[string]string, string) {
|
||||
accesses := make([]int, parameters["accesses"])
|
||||
for index := range accesses {
|
||||
accesses[index] = 1 + random.below(int(parameters["keys"]))
|
||||
}
|
||||
hits := lruHits(accesses, int(parameters["capacity"]))
|
||||
return map[string]string{"sequence": joinInts(accesses)}, strconv.Itoa(hits)
|
||||
}
|
||||
|
||||
// lruHits counts the accesses that find their key in a least-recently-used
|
||||
// cache of the given capacity. A hit and a miss both make the key the most
|
||||
// recently used one; a miss on a full cache evicts the least recently used
|
||||
// key first.
|
||||
func lruHits(accesses []int, capacity int) int {
|
||||
// recent holds the cached keys from least to most recently used.
|
||||
recent := make([]int, 0, capacity)
|
||||
hits := 0
|
||||
for _, key := range accesses {
|
||||
position := -1
|
||||
for index, cached := range recent {
|
||||
if cached == key {
|
||||
position = index
|
||||
break
|
||||
}
|
||||
}
|
||||
switch {
|
||||
case position >= 0:
|
||||
hits++
|
||||
recent = append(recent[:position], recent[position+1:]...)
|
||||
case len(recent) == capacity:
|
||||
recent = recent[1:]
|
||||
}
|
||||
recent = append(recent, key)
|
||||
}
|
||||
return hits
|
||||
}
|
||||
|
||||
// Data names: dependencies.
|
||||
func generateDependencyOrder(random *seededRandom, parameters map[string]int32) (map[string]string, string) {
|
||||
tasks := int(parameters["tasks"])
|
||||
// A hidden shuffle fixes a valid order; links only point forward in it,
|
||||
// so the graph never has a cycle.
|
||||
hidden := make([]int, tasks)
|
||||
for index := range hidden {
|
||||
hidden[index] = index + 1
|
||||
}
|
||||
for index := tasks - 1; index > 0; index-- {
|
||||
swap := random.below(index + 1)
|
||||
hidden[index], hidden[swap] = hidden[swap], hidden[index]
|
||||
}
|
||||
seen := make(map[[2]int]bool)
|
||||
var links [][2]int
|
||||
var described []string
|
||||
for range parameters["links"] {
|
||||
first, second := random.below(tasks), random.below(tasks)
|
||||
if first == second {
|
||||
continue
|
||||
}
|
||||
if first > second {
|
||||
first, second = second, first
|
||||
}
|
||||
link := [2]int{hidden[first], hidden[second]}
|
||||
if seen[link] {
|
||||
continue
|
||||
}
|
||||
seen[link] = true
|
||||
links = append(links, link)
|
||||
described = append(described, strconv.Itoa(link[0])+"->"+strconv.Itoa(link[1]))
|
||||
}
|
||||
dependencies := "none"
|
||||
if len(described) > 0 {
|
||||
dependencies = strings.Join(described, " ")
|
||||
}
|
||||
return map[string]string{"dependencies": dependencies}, joinInts(smallestDependencyOrder(tasks, links))
|
||||
}
|
||||
|
||||
// smallestDependencyOrder returns the lexicographically smallest order of
|
||||
// tasks 1..tasks in which every link {before, after} has before first. The
|
||||
// links must not contain a cycle.
|
||||
func smallestDependencyOrder(tasks int, links [][2]int) []int {
|
||||
pending := make([]int, tasks+1)
|
||||
for _, link := range links {
|
||||
pending[link[1]]++
|
||||
}
|
||||
done := make([]bool, tasks+1)
|
||||
order := make([]int, 0, tasks)
|
||||
for len(order) < tasks {
|
||||
next := 0
|
||||
for task := 1; task <= tasks; task++ {
|
||||
if !done[task] && pending[task] == 0 {
|
||||
next = task
|
||||
break
|
||||
}
|
||||
}
|
||||
done[next] = true
|
||||
order = append(order, next)
|
||||
for _, link := range links {
|
||||
if link[0] == next {
|
||||
pending[link[1]]--
|
||||
}
|
||||
}
|
||||
}
|
||||
return order
|
||||
}
|
||||
Reference in new issue
Block a user